# How to check if coordinate is within polygon

• 2013-08-21, 12:08
JonUK
How to check if coordinate is within polygon
Using the type [B]nokia.maps.map.Polygon[/B], is there a way to determine if a given coordinate sits within the polygon? I’ve looked at the methods [B]getNearest [/B]and [B]getNearestIndex [/B]but neither provide this functionality.
• 2013-08-22, 08:44
jasfox
Re: How to check if coordinate is within polygon
How you do this will depend on how accurate you need to be. A rough-and-ready answer can be achieved by using the [B]Polygon.getBoundingBox().contains()[/B], and passing in a [B]BoundingBox [/B]of a single location. This will however reduce the [B]polygons[/B] to rectangular areas. A more accurate method would be to use [URL="http://developer.here.com/docs/maps_js/topics_api_pub/nokia.maps.map.Display.html#topic-apiref__Display-getObjectsAt"][B]Display.getObjectsAt()[/B][/URL], and check to see if a [B]Polygon [/B]is returned.

[Code]map.addListener("mouseover", function (evt) {
var coord = map.pixelToGeo(evt.targetX, evt.targetY);
var objects = map.getObjectsAt( evt.targetX, evt.targetY) ;
for(var i=0;i<objects.length;i++){
var object=objects[i];
if(object instanceof nokia.maps.map.Polygon ){
// object is my Polygon.
// Do stuff with it.
evt.stopImmediatePropagation();
}
}
});[/Code]