×
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)
m (Jasfox - Add keywords)
jasfox (Talk | contribs)
m (Jasfox - links)
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Web]][[Category:Location]][[Category:Location]][[Category:Nokia Maps]][[Category:Code Examples]][[Category:How To]]
+
[[Category:Nokia 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.}}
 +
 
 
{{ArticleMetaData
 
{{ArticleMetaData
|sourcecode= [[Media:NearestMarkerExample.zip]]  
+
|sourcecode= [[Media:NearestMarkerExample.zip|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= Firefox 7.0.1
+
|devices= Firefox , Internet Explorer, Google Chrome, Opera
 
|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
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
+
|dependencies= Nokia Maps API 2.2.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. -->
Line 23: Line 25:
 
|author=[[User:Maveric]]
 
|author=[[User:Maveric]]
 
}}
 
}}
 
+
{{SeeAlso|
 
+
* [http://developer.here.net/javascript_api Nokia Maps API]
{{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.}}  
+
* [http://developer.here.net/apiexplorer/examples/api-for-js/markers/marker.html Adding a Marker]}}
  
 
== Introduction ==
 
== 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.
+
This example demonstrates how to find the nearest map {{Icode|Marker}} from any point on the map. In the example there are five {{Icode|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 {{Icode|Marker}} that is located closest to the point of your click. To display the result, the label {{Icode|text}} of the {{Icode|Marker}} is retrieved and shown on the page.
  
 
[[File:NearestMarkerExample..png]]
 
[[File:NearestMarkerExample..png]]
Line 35: Line 37:
 
== Example code ==
 
== Example code ==
  
After setting up five markers, the code continues by adding a 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;         
 
var eventTarget = nokia.maps.dom.EventTarget;         
Line 45: Line 47:
 
     }, false);             
 
     }, false);             
 
</code>
 
</code>
The ''findNearestMarker'' function iterates through each 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.
 
<code javascript>
 
<code javascript>
 
function findNearestMarker(coords) {  
 
function findNearestMarker(coords) {  
Line 65: Line 67:
 
</code>
 
</code>
  
The fully coded example can be found here :  [[Media:NearestMarkerExample.zip]]  
+
The fully coded example can be found here :  [[Media:NearestMarkerExample.zip|NearestMarkerExample.zip]]
 
+
== Tested with ==
+
 
+
Firefox 7.0.1
+

Revision as of 18:49, 3 January 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: Nokia Maps API 2.2.3
Article
Keywords: Nokia Maps, JavaScript, Marker
Created: Maveric (13 Nov 2011)
Last edited: jasfox (03 Jan 2013)

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

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() 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 coded example can be found here : NearestMarkerExample.zip

217 page views in the last 30 days.
×