×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    3

    Fastest way to remove points

    Lets say i have drawn 10K points on map. Now i want to remove half. Which is the most efficient way to do this?
    I have tried to loop objects and remove by using map.objects.remove(obj) but this is quite slow.

  2. #2
    Regular Contributor
    Join Date
    Aug 2011
    Location
    Berlin
    Posts
    288

    Re: Fastest way to remove points

    Now, the usual way to deal with 10,000 markers on the map would be to use the marker clustering since this stops the map being overwhelmed with markers. However you bring up a valid issue since you may wish to deal with a group of items at once.
    Iterating through markers and removing them one-by-one is not efficient, since each removal will potentially result in an additional map refresh. To initiate an action on a group of markers, you should probably place your markers within a nokia.maps.map.Container, and call functions of the Container instead.

    I can think of at least seven ways to remove a bunch of Markers, each of which may or may not make sense.

    • Place your Markers in a container and remove() the Container from the MapDisplay - display.objects.remove(containers[1]);
    • Place your Markers in a container iterate the through the items in the Container and remove each one.
    • Place your Markers in a container and clear() the Container contents - containers[2].objects.clear();
    • Place your Markers in a container , change the visibility of the container to false - containers[4].set('visibility', false)
    • The same thing, but iterate through the markers, changing the visibility of each one.
    • Destroy the container rather than remove it from the map - containers[6].destroy();
    • Add markers directly to the map and iterate through and destroy them.


    Now I reckon that the choices in red are bad, the others may make sense in certain circumstances - for example, you would usually destroy() an object if you needed to remove any listeners or observers.

    Probably the best way forward would be to try and evaluate your circumstances using a test harness such as the code below and see which option works best for you (note that adding the markers in this way is very slow especially with older browsers):

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html>
    <head>     
    	
    	<meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9"/>
    	
    		<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.3/jsl.js?with=all"></script>
    
    		
    		<title>Test Harness</title>
    </head> 
    <body onload="init()"> 
    	
    
     <div id="map" style="width:70%; height:70%; top:10%;  position: absolute;"></div> 
     <input  type="button" onclick="display.objects.remove(containers[1]);"  value="Remove from Display" /> 	
    <input  type="button" onclick="containers[2].objects.clear();"  value="Clear Container" /> 
    <input  type="button" onclick="removeMarkers(containers[3]);"  value="Iterate Remove Marker" /> 
    
    <input  type="button" onclick="containers[4].set('visibility', false)"  value="Container visibility" /> 
    <input  type="button" onclick="makeMarkersInvisible(containers[5])"  value="Iterate Marker visibility" /> 
    <input  type="button" onclick="destroyMarkers();"  value="Iterate and Destroy Marker" /> 
    <input  type="button" onclick="containers[6].destroy();"  value="Destroy Container" /> 
     
     
     <script type="text/javascript">
            	        	   	
            	
    /////////////////////////////////////////////////////////////////////////////////////
    // Don't forget to set your API credentials
    //
    // Replace with your appId and token which you can obtain when you 
    // register on http://api.developer.nokia.com/ 
    //
    			nokia.Settings.set( "appId", "YOUR APP ID GOES HERE"); 
    			nokia.Settings.set( "authenticationToken", "YOUR AUTHENTICATION TOKEN GOES HERE");			
    			
    //			
    /////////////////////////////////////////////////////////////////////////////////////
    
    
    	
    	
    	
    //initialize a new map 
    
    
    var display = new nokia.maps.map.Display(document.getElementById("map"), 
                         {     "components": [	
                                         new nokia.maps.map.component.ZoomBar(),                 
                                         new nokia.maps.map.component.Behavior(),                 
                                         new nokia.maps.map.component.TypeSelector()], 
                                            
                                         "zoomLevel": 4 ,
                                         "center" : [29.762778, -95.383056] }); 
                                         
    // The following functions add a random series of points  ....
    function randomPointNear (lat, long, walk){
    
      for (j = 0; j  < 3; j++){
    			   lat = Math.max( lat  - (Math.random()*walk), -80);
    			    lat = Math.min( lat   + (Math.random()*walk), 80);
    			   long = Math.max( long  - (Math.random()*walk) , -175);
    			   long = Math.min( long  + (Math.random()*walk), 175);
    		}
    	return  new nokia.maps.geo.Coordinate ( lat, long);
    }
    
    function addMarkersNearCity(lat, long, size, spread){
    		var container = new nokia.maps.map.Container();
    		for (var i = 0; i  < size; i++){
    			var marker = new nokia.maps.map.StandardMarker(randomPointNear (lat, long , spread));
    						markers.push( marker);
    						container.objects.add(marker);
    		}
    		return container;
    }
    
    
    
    var markers = [];
    var containers = [];
    var markersDirect = [];
    // Adds 70 Markers....
    function add70MarkersNearCities(){
    	
    		addCount--;
    		containers.push(addMarkersNearCity (38.895111, -77.036667 , 1000, 1));  // Washington
    		containers.push(addMarkersNearCity (	43, -75 , 1000,  1));  // New York
    		containers.push(addMarkersNearCity(34.05, -118.25 , 1000,  1)) ;  // Los Angeles
    		containers.push(addMarkersNearCity (32.782778, -96.803889 ,1000,  1)) ;  //  Houston
    		containers.push(addMarkersNearCity (	19.433333, -99.133333, 1000, 1)) ; //  Dallas
    		containers.push(addMarkersNearCity(29.762778, -95.383056 , 1000, 1)) ;   // Mexico City
    		containers.push(addMarkersNearCity(39.739167, -104.984722, 1000, 1)) ;   // Denver
    		  
    	 display.objects.addAll(containers);
    								 
    								 
    		for (var i = 0; i  < 1000; i++){
    			var marker = new nokia.maps.map.StandardMarker(randomPointNear (25.787676, -80.224145, 1));
    						markersDirect.push( marker);
    		}
    	 display.objects.addAll(markersDirect);
    
    }
    
    //
    //  Initially add 7000 markers to the map.
    //	To add more markers, increase the value of AddCount.
    //
    function init(){
    		setTimeout( function() { add70MarkersNearCities(); }, 0);
    	  
    }
    
    function removeMarkers(con){
    	len = con.objects.getLength();
     for (var i = len-1; i >= 0; --i) {
          con.objects.remove(con.objects.get(i));
        }
    }
    
    function destroyMarkers(con){
    	len = markersDirect.length;
     for (var i = len-1; i >= 0; --i) {
         markersDirect[i].destroy();
        }
    }
    
    function makeMarkersInvisible(con){
    	len = con.objects.getLength();
     	for (var i = len-1; i >= 0; --i) {
          con.objects.get(i).set('visibility', false);
        }
    }
            </script> 
        </body> 
    </html>

    Note that if you do go down the clustering route, it should be explained, that it is much more efficient to base your data on IPoints rather than dealing with the overhead of Markers , and in the dynamic use case also take care to avoid the memory issue discussed in a previous thread
    Last edited by jasfox; 2013-06-25 at 13:14.

Similar Threads

  1. ***********Fastest way to draw to Image?
    By none800 in forum Mobile Java General
    Replies: 5
    Last Post: 2010-11-19, 11:47
  2. Fastest possible way to autostart on boot
    By giedi1 in forum Symbian
    Replies: 6
    Last Post: 2010-01-27, 20:22
  3. fastest way to read a file
    By ed_welch in forum Symbian
    Replies: 6
    Last Post: 2008-02-13, 11:27
  4. Which one is the fastest (in drawing) ?
    By Smori2002 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 4
    Last Post: 2003-11-12, 15:36

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×