×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Question map.getObjects() doesn't seem to return polygons all the time

    Hi all,

    I have a sample page where a marker can be dragged from one country to another.
    The countries are defined in a KML file which is loaded on opening the page.

    When the marker is put on a country it colors the country. And when the marker is removed the country should get a different color again.
    Coloring the country when the marker is put there is working.
    But changing the color when the marker is removed doesn't seem to work consitently.
    When the marker is put on an ocean the country gets a different color. When the marker is dragged to another country the original country doesn't get a new color.

    It looks like the map.getObjects() method doesn't seem to return the actual polygons at the location.
    But it only happens now and then.

    Any idea's why this would happen?

    The KML file can be found here: http://pastebin.ca/2198764

    And here's the HTML file:
    HTML Code:
    <html>
    <head>
    	<title>KML Country color test</title>
    	<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    	<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.1/jsl.js?with=all"></script>
    </head>
    <body>
    <div style="height:300px" id="mapContainer"></div>
    <script type="text/javascript" id="exampleJsSource">
    
    nokia.Settings.set( "appId", "myappid"); 
    nokia.Settings.set( "authenticationToken", "mytoken");
    
    // Get the DOM node to which we will append the map
    var mapContainer = document.getElementById("mapContainer");
    
    // Create a map inside the map container DOM node
    var map = new nokia.maps.map.Display(mapContainer, {
    	center: [0, 0],
    	zoomLevel: 3,
    	components: [
    		// We add the behavior component to allow panning / zooming of the map
    		new nokia.maps.map.component.Behavior(),new nokia.maps.map.component.TypeSelector(),new nokia.maps.map.component.ZoomBar()
    	]
    });
    
    var listener = function (evt) {
    	//console.debug("listener");
    	var mapDragType = evt.dataTransfer.getData("application/map-drag-type"); 
    	if (mapDragType === "standardmarker") {
    		// Get the marker itself.
    		var marker = evt.dataTransfer.getData("application/map-drag-object");
    		// Get the offset of the mouse relative to the top-left corner of the marker.
    		var offset = evt.dataTransfer.getData("application/map-drag-object-offset");
    		
    		/* Calculate the current coordinate of the marker, so substract the offset from the 
    		 * current displayX/Y position to get the top-left position of the marker and then
    		 * add the anchor to get the pixel position of the anchor of the marker and then 
    		 * query for the coordinate of that pixel position
    		 */
    		var newx=evt.displayX - offset.x + marker.anchor.x;
    		var newy=evt.displayY - offset.y + marker.anchor.y;
    		
    		if(evt.type==="dragend"){
    			colorCountry(newx,newy,"#00000088");
    			if(this.previousMark!=null){
    				//map.objects.add(new nokia.maps.map.StandardMarker(map.pixelToGeo(this.previousMark.x,this.previousMark.y)));
    				colorCountry(this.previousMark.x,this.previousMark.y,"#FFFFFF22");
    			}
    			this.previousMark=new nokia.maps.util.Point(newx,newy);
    		}
    		
    	}
    };
    
    var colorCountry=function(x,y,color){
    	// Stopped dragging, check for country and color it
    	objects = map.getObjectsAt(x, y);
    	console.info("Objects at location: "+objects.length);
    	
    	for(var i=0;i<objects.length;i++){
    		var object=objects[i];
    		var countryobject=null;
    		if(!(typeof object.n === "undefined") && object.n == "polygon"){
    			//a polygon object is found, check the parent to see if there are more polygon's for that country
    			if(object.getParent(map).n == "container" && object.getParent(map).objects.getLength()<=150){
    				//the marker is place on a country with multiple polygons
    				var parent=object.getParent(map);
    				for(var j=0;j<parent.objects.getLength();j++){
    					var otherpolygon=parent.objects.get(j);
    					if(otherpolygon.n == "polygon"){
    						otherpolygon.set("brush",new nokia.maps.util.Brush({color:color,fill: "solid"}));
    					}
    				}
    			}else{
    				object.set("brush",new nokia.maps.util.Brush({color:color,fill: "solid"}));
    			}
    		}
    	}
    }
    
    // Create a new standard marker
    var standardMarker = new nokia.maps.map.StandardMarker(
    	new nokia.maps.geo.Coordinate(-2, -2), {
    		//make the marker draggable
    		draggable: true
    	}
    );
    
    // Add the listener function to the bubbling phase of the "dragend" event
    standardMarker.addListener("dragend", listener, false);
    
    map.objects.add(standardMarker);
    
    var kml = new nokia.maps.kml.Manager();
    var resultSet="test";
    
    // We define a callback function for parsing kml file,
    // and then push the parsing result to map display
    var onParsed = function (kmlManager) {
    	console.debug("onParsed");
    	var container,
    		boundingBox;
    	
    	// KML file was successfully loaded
    	if (kmlManager.state == "finished") {
    		console.debug("kmlManager state finished");
    		// KML file was successfully parsed
    		resultSet = new nokia.maps.kml.component.KMLResultSet(kmlManager.kmlDocument, map);
    		resultSet.addObserver("state", function (resultSet) {
    			if (resultSet.state == "finished") {
    				console.debug("resultSet finished");
    			}
    		});
    		// Add the container to the map's object collection so it will be rendered onto the map.
    		map.objects.add(container = resultSet.create());
    	}
    };
    // Add an observer to kml manager
    kml.addObserver("state", onParsed);
    
    // Start parsing a kml file with given url
    // Note: please adapt the following path to the file you want to parse.
    kml.parseKML("doc-simple-limited.kml");
    </script>
    <body>
    </html>

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

    Re: map.getObjects() doesn't seem to return polygons all the time

    It looks like the setting of the brush is causing the map to update, hence when the second getObjectsAt() is called it may, or may not be empty. There is a workaround for this, by getting all of the mapObjects first and then painting everything afterwards

    This update works for me, I've tidied up the references and I've tried it in FF, Chrome and even IE.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<title>KML Country color test</title>
    	<meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9"/>
    		<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    	<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.1/jsl.js?with=all"></script>
    </head>
    <body>
    <div style="height:300px" id="mapContainer"></div>
    <script type="text/javascript" id="exampleJsSource">
    
    nokia.Settings.set( "appId", "myappid"); 
    nokia.Settings.set( "authenticationToken", "mytoken");
    
    // Get the DOM node to which we will append the map
    var mapContainer = document.getElementById("mapContainer");
    
    // Create a map inside the map container DOM node
    var map = new nokia.maps.map.Display(mapContainer, {
    	center: [0, 0],
    	zoomLevel: 3,
    	components: [
    		// We add the behavior component to allow panning / zooming of the map
    		new nokia.maps.map.component.Behavior(),new nokia.maps.map.component.TypeSelector(),new nokia.maps.map.component.ZoomBar()
    	]
    });
    
    var listener = function (evt) {
    
    	if (evt.target instanceof nokia.maps.map.StandardMarker) {
    		// Get the marker itself.
    		var marker = evt.target;
    		var point = map.geoToPixel(marker.coordinate);
    		// Get the offset of the mouse relative to the top-left corner of the marker.
    		var offset = evt.dataTransfer.getData("application/map-drag-object-offset");
    		
    		/* Calculate the current coordinate of the marker, so substract the offset from the 
    		 * current displayX/Y position to get the top-left position of the marker and then
    		 * add the anchor to get the pixel position of the anchor of the marker and then 
    		 * query for the coordinate of that pixel position
    		 */
    		//var newx=evt.displayX - offset.x + marker.anchor.x;
    		//var newy=evt.displayY - offset.y + marker.anchor.y;
    		
    		if(evt.type==="dragend"){
    			var oldObj = [];
    			if(this.previousMark!=null){
    			    oldObj =  map.getObjectsAt(  this.previousMark.x,this.previousMark.y) ;
    			}
    			var newObj = map.getObjectsAt( point.x,point.y);
    			colorCountry(oldObj,"#FFFFFF22");
    			colorCountry(newObj ,"#00000088");
    			this.previousMark=new nokia.maps.util.Point(point.x,point.y);
    			map.update(-1, 0);
    		}
    	}
    };
    
    var colorCountry=function(objects,color){
    	// Stopped dragging, check for country and color it
     
        
    	for(var i=0;i<objects.length;i++){
    		var object=objects[i];
    		var countryobject=null;
    		if(object instanceof nokia.maps.map.Polygon ){
    			//a polygon object is found, check the parent to see if there are more polygon's for that country
    			if(object.getParent(map) instanceof nokia.maps.map.Container  && object.getParent(map).objects.getLength()<=150){
    				//the marker is place on a country with multiple polygons
    				var parent=object.getParent(map);
    				for(var j=0;j<parent.objects.getLength();j++){
    					var otherpolygon=parent.objects.get(j);
    					if(otherpolygon instanceof nokia.maps.map.Polygon){
    						otherpolygon.set("brush",new nokia.maps.util.Brush({color:color,fill: "solid"}));
    							
    					}
    				}
    			}else{
    				object.set("brush",new nokia.maps.util.Brush({color:color,fill: "solid"}));
    				
    			}
    		}
    	}
    
    }
    
    // Create a new standard marker
    var standardMarker = new nokia.maps.map.StandardMarker(
    	new nokia.maps.geo.Coordinate(-2, -2), {
    		//make the marker draggable
    		draggable: true
    	}
    );
    
    // Add the listener function to the bubbling phase of the "dragend" event
    standardMarker.addListener("dragend", listener, false);
    
    map.objects.add(standardMarker);
    
    var kml = new nokia.maps.kml.Manager();
    var resultSet="test";
    
    // We define a callback function for parsing kml file,
    // and then push the parsing result to map display
    var onParsed = function (kmlManager) {
    	//console.debug("onParsed");
    	var container,
    		boundingBox;
    	
    	// KML file was successfully loaded
    	if (kmlManager.state == "finished") {
    		//console.debug("kmlManager state finished");
    		// KML file was successfully parsed
    		resultSet = new nokia.maps.kml.component.KMLResultSet(kmlManager.kmlDocument, map);
    		resultSet.addObserver("state", function (resultSet) {
    			if (resultSet.state == "finished") {
    				//console.debug("resultSet finished");
    			}
    		});
    		// Add the container to the map's object collection so it will be rendered onto the map.
    		map.objects.add(container = resultSet.create());
    	}
    };
    // Add an observer to kml manager
    kml.addObserver("state", onParsed);
    
    // Start parsing a kml file with given url
    // Note: please adapt the following path to the file you want to parse.
    kml.parseKML("doc-simple-limited.kml");
    </script>
    <body>
    </html>

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Re: map.getObjects() doesn't seem to return polygons all the time

    Hi Jason,

    Thanks for the help.
    It works now.

    Kind regards,
    Pieter

Similar Threads

  1. 6720 return GMT time 1 day ahead past midnight.
    By manishkb@xora.com in forum Mobile Java General
    Replies: 0
    Last Post: 2010-02-19, 11:36
  2. Replies: 1
    Last Post: 2009-08-19, 08:49
  3. Not a return a value of next time calling function.
    By Tanya in forum Symbian User Interface
    Replies: 1
    Last Post: 2007-04-19, 07:33
  4. fillPolygon() and drawPolygon return different polygons!!!
    By ljioannou in forum Mobile Java Media (Graphics & Sounds)
    Replies: 0
    Last Post: 2003-08-29, 09:11

Posting Permissions

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