×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - How to find the nearest Marker

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
(Jasfox - Updated, added commentary, ZiP and picture.)
hamishwillee (Talk | contribs)
m (Hamishwillee - Subedit introduction)
Line 1: Line 1:
[[Category:Web]][[Category:Location]][[Category:Web]][[Category:Location]][[Category:Nokia Maps]]
+
[[Category:Web]][[Category:Location]][[Category:Location]][[Category:Nokia Maps]]
 
{{ArticleMetaData
 
{{ArticleMetaData
 
|sourcecode= [[Media:NearestMarkerExample.zip]]  
 
|sourcecode= [[Media:NearestMarkerExample.zip]]  
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
+
|devices= Firefox 7.0.1
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|platform= Web Browser
 
|platform= Web Browser
Line 25: Line 25:
  
  
{{Abstract|This article explains how to find the nearest Marker on the map.}}  
+
{{Abstract|This code example shows how to find the nearest Marker from any point on the map. The code is useful for finding the Markers that  are located close to the user's current position or destination.}}  
  
 
== Introduction ==
 
== Introduction ==
  
This example will demonstrate how to find the nearest map Marker from any point on the map. In the example there are five Markers on Nokia Maps and you can click anywhere on the map. Based on where you clicked, the script will calculate the distance to the Marker that is located closest to the point of your click. To display the result, the label text of the Marker is retrieved and shown on the page.
+
This example demonstrates how to find the nearest map Marker from any point on the map. In the example there are five Markers on Nokia Maps and you can click anywhere on the map. Based on where you clicked, the script will calculate the distance to the Marker that is located closest to the point of your click. To display the result, the label text of the Marker is retrieved and shown on the page.
 
+
 
+
== Summary ==
+
 
+
This method is useful when you need to determine which of the Markers are located close to e.g. the user's current position.
+
 
+
  
 
== Example code ==
 
== Example code ==

Revision as of 01:05, 6 January 2012

Article Metadata
Code ExampleTested with
Devices(s): Firefox 7.0.1
Compatibility
Platform(s): Web Browser
Article
Created: Maveric (13 Nov 2011)
Last edited: hamishwillee (06 Jan 2012)


This code example shows how to find the nearest Marker from any point on the map. The code is useful for finding the Markers that are located close to the user's current position or destination.

Contents

Introduction

This example demonstrates how to find the nearest map Marker from any point on the map. In the example there are five Markers on Nokia Maps and you can click anywhere on the map. Based on where you clicked, the script will calculate the distance to the Marker that is located closest to the point of your click. To display the result, the label text of the Marker is retrieved and shown on the page.

Example code

After setting up five markers, the code continues by adding a click event to the map.

var eventTarget = nokia.maps.dom.EventTarget;        
var eventCheck = document.getElementById("map");
eventTarget(eventCheck);
eventCheck.addListener("click", function(evt)
{ var coords = map.pixelToGeo(evt.targetX , evt.targetY);
findNearestMarker (coords);
}, false);

The findNearestMarker function iterates through each marker on the map and calculates the distance. The lowest distance so far is kept.

function findNearestMarker(coords) { 
var minDist = 1000;
var nearest_text = "*None*";
for (i=0; i< map.objects.getLength(); i++)
{
if ( map.objects.get(i) instanceof nokia.maps.map.StandardMarker ) {
var markerDist = map.objects.get(i).coordinate.distance(coords);
if (markerDist < minDist) {
minDist = markerDist;
nearest_text = map.objects.get(i).text
}
}
}
 
alert (nearest_text);
}

Screenshot

The fully coded example can be found here : Media:NearestMarkerExample.zip

NearestMarkerExample..png


Tested with

Firefox 7.0.1

273 page views in the last 30 days.
×