(Difference between revisions)

HERE Maps API - How to find the nearest Marker

From Nokia Developer Wiki
Jump to: navigation, search
Maveric (Talk | contribs)
m (Maveric -)
Maveric (Talk | contribs)
m (Maveric - - Example code)
Line 11: Line 11:
This method is useful when you need to determine which of the Markers are located close to e.g. the user's current position.
This method is useful when you need to determine which of the Markers are located close to e.g. the user's current position.
[[Category:Web]][[Category:Location]][[Category:Ovi Maps]]
== Example code ==
== Example code ==

Revision as of 16:03, 24 November 2011

This article explains how to find the nearest Marker on the map.



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 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

<title>Nokia Maps API - Find the nearest Marker example</title>
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.0.0/jsl.js"></script>
<center><div id="map" style="z-index: -1; left:1px; top:100px; width:640px; height:480px;></div></center>
<div style="
z-index: -1; left:40px; top:450px; width: 800px; position: absolute;">
<p>Click anywhere on the Map to find out which of the five Markers is the nearest to that spot.</p>
<p>You can also drag and drop any Marker to a new position.
<p>The nearest marker is: <span id="
<script type="
var nearestMarker = document.getElementById("
var map = new nokia.maps.map.Display(document.getElementById("
{components: [ new nokia.maps.map.component.Behavior() ]});
map.setCenter([60.1697768639186, 24.82923586371288]);
baseMapType", map.SATELLITE);
var marker1 = new nokia.maps.map.StandardMarker([60.1697768639186, 24.82923586371288],
text: "
Marker 1",
draggable: true,
var marker2 = new nokia.maps.map.StandardMarker([60.170497332447866, 24.828592133548653 ],
text: "
Marker 2",
draggable: true,
var marker3 = new nokia.maps.map.StandardMarker([60.17093227431904, 24.827707004572943 ],
text: "
Marker 3",
draggable: true,
var marker4 = new nokia.maps.map.StandardMarker([60.170057048001865, 24.826505374933163 ],
text: "
Marker 4",
draggable: true,
var marker5 = new nokia.maps.map.StandardMarker([60.170025027084705, 24.82839365008141],
text: "
Marker 5",
draggable: true,
var eventTarget = nokia.maps.dom.EventTarget;
var eventCheck = document.getElementById("
click", function(evt)
{ var coords = map.pixelToGeo(evt.targetX , evt.targetY);
findNearestMarker (coords);
}, false);
function findNearestMarker(coords) {
var minDist = 1000;
var nearest_text = "
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
nearestMarker.innerHTML = nearest_text;

Tested with

Firefox 7.0.1

323 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.