Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

HERE Maps API - How to find the nearest Marker

From Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - links)
jasfox (Talk | contribs)
m (Jasfox - links)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Nokia Maps]][[Category:Code Examples]][[Category:JavaScript]]
+
[[Category:HERE Maps]][[Category:Code Examples]][[Category:JavaScript]]
 
{{Abstract|This code example shows how to find the nearest {{Icode|Marker}} from any point on the map. The code is useful for finding the {{Icode|Markers}} that  are located close to the user's current position or destination.}}  
 
{{Abstract|This code example shows how to find the nearest {{Icode|Marker}} from any point on the map. The code is useful for finding the {{Icode|Markers}} that  are located close to the user's current position or destination.}}  
  
 
{{ArticleMetaData
 
{{ArticleMetaData
|sourcecode= [[Media:NearestMarkerExample.zip|NearestMarkerExample.zip]]  
+
|sourcecode= [[https://github.com/heremaps/examples/blob/master/maps_api_for_javascript/simple-examples/find-nearest-marker.html find-nearest-marker.html]]
 
|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= Firefox , Internet Explorer, Google Chrome, Opera
 
|devices= Firefox , Internet Explorer, Google Chrome, Opera
Line 9: Line 9:
 
|platform= Web Browser
 
|platform= Web Browser
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies= Nokia Maps API 2.2.3
+
|dependencies= HERE Maps API 2.5.3
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= Nokia Maps, JavaScript, Marker
+
|keywords= HERE Maps, JavaScript, Marker
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
Line 26: Line 26:
 
}}
 
}}
 
{{SeeAlso|
 
{{SeeAlso|
* [http://developer.here.net/javascript_api Nokia Maps API]
+
* [http://developer.here.com/javascript_api HERE Maps API]
* [http://developer.here.net/apiexplorer/examples/api-for-js/markers/marker.html Adding a Marker]}}
+
* [http://heremaps.github.io/examples/index.html HERE Maps on Github]
 +
* [http://developer.here.com/apiexplorer/examples/api-for-js/markers/marker.html Adding a Marker]}}
  
 
== Introduction ==
 
== Introduction ==
Line 39: Line 40:
 
After setting up five markers, the code continues by adding a {{Icode|click}} event to the map.
 
After setting up five markers, the code continues by adding a {{Icode|click}} event to the map.
 
<code javascript>
 
<code javascript>
var eventTarget = nokia.maps.dom.EventTarget;       
+
function addClickEventListenerToMap(map){
var eventCheck = document.getElementById("map");               
+
map.addListener("click", function(evt){
eventTarget(eventCheck);       
+
var coords = map.pixelToGeo(evt.targetX , evt.targetY);
eventCheck.addListener("click", function(evt)        
+
findNearestMarker (coords);
  {    var coords = map.pixelToGeo(evt.targetX , evt.targetY);          
+
}, false);
        findNearestMarker (coords);      
+
}       
    }, false);          
+
 
</code>
 
</code>
 
The {{Icode|findNearestMarker()}} method iterates through each {{Icode|marker}} on the map and calculates the distance. The lowest distance so far is kept.
 
The {{Icode|findNearestMarker()}} method iterates through each {{Icode|marker}} on the map and calculates the distance. The lowest distance so far is kept.
Line 67: Line 67:
 
</code>
 
</code>
  
The fully coded example can be found here : [[Media:NearestMarkerExample.zip|NearestMarkerExample.zip]]
+
The fully working example can be found at:
 +
 
 +
http://heremaps.github.io/examples/examples.html#find-nearest-marker

Latest revision as of 15:52, 20 December 2013

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.

Article Metadata
Code ExampleTested with
Devices(s): Firefox , Internet Explorer, Google Chrome, Opera
Compatibility
Platform(s): Web Browser
Dependencies: HERE Maps API 2.5.3
Article
Keywords: HERE Maps, JavaScript, Marker
Created: Maveric (13 Nov 2011)
Last edited: jasfox (20 Dec 2013)

[edit] 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 the Nokia Map 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.

NearestMarkerExample..png

[edit] Example code

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

function addClickEventListenerToMap(map){
map.addListener("click", function(evt){
var coords = map.pixelToGeo(evt.targetX , evt.targetY);
findNearestMarker (coords);
}, false);
}

The findNearestMarker() method 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);
}

The fully working example can be found at:

http://heremaps.github.io/examples/examples.html#find-nearest-marker

This page was last modified on 20 December 2013, at 15:52.
675 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×