×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - How to find the nearest Marker

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot addition of Template:ArticleMetaData)
jasfox (Talk | contribs)
(Jasfox - Updated, added commentary, ZiP and picture.)
Line 1: Line 1:
 +
[[Category:Web]][[Category:Location]][[Category:Web]][[Category:Location]][[Category:Nokia Maps]]
 
{{ArticleMetaData
 
{{ArticleMetaData
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.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= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|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= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
+
|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= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
Line 23: Line 24:
 
}}
 
}}
  
[[Category:Web]][[Category:Location]]
+
 
 
{{Abstract|This article explains how to find the nearest Marker on the map.}}  
 
{{Abstract|This article explains how to find the nearest Marker on the map.}}  
  
Line 35: Line 36:
 
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:Nokia Maps]]
+
 
 
== Example code ==
 
== Example code ==
  
<code java>
+
After setting up five markers, the code continues by adding a click event to the map.
<html>
+
<code javascript>
<head>
+
<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>
+
</head>
+
<body>   
+
<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="nearestMarker">*None*</span><br></div>
+
<script type="text/javascript">          
+
 
+
var nearestMarker = document.getElementById("nearestMarker");       
+
 
+
var map = new nokia.maps.map.Display(document.getElementById("map"),               
+
{components: [  new nokia.maps.map.component.Behavior() ]});               
+
 
+
map.setCenter([60.1697768639186, 24.82923586371288]);       
+
map.setZoomLevel(16);       
+
map.set("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,                   
+
});                           
+
 
+
map.objects.add(marker1);       
+
map.objects.add(marker2);     
+
map.objects.add(marker3);       
+
map.objects.add(marker4);       
+
map.objects.add(marker5);           
+
 
+
 
var eventTarget = nokia.maps.dom.EventTarget;         
 
var eventTarget = nokia.maps.dom.EventTarget;         
 
var eventCheck = document.getElementById("map");                 
 
var eventCheck = document.getElementById("map");                 
Line 102: Line 48:
 
         findNearestMarker (coords);         
 
         findNearestMarker (coords);         
 
     }, false);             
 
     }, false);             
 
+
</code>
 +
The ''findNearestMarker'' function iterates through each marker on the map and calculates the distance. The lowest distance so far is kept.
 +
<code javascript>
 
function findNearestMarker(coords) {  
 
function findNearestMarker(coords) {  
 
           var minDist = 1000;           
 
           var minDist = 1000;           
Line 117: Line 65:
 
         }             
 
         }             
  
nearestMarker.innerHTML = nearest_text;         
+
alert (nearest_text);         
 
}     
 
}     
</script>
 
</body>
 
</html>
 
 
</code>
 
</code>
 +
== Screenshot ==
 +
The fully coded example can be found here :  [[Media:NearestMarkerExample.zip]]
 +
 +
[[File:NearestMarkerExample..png]]
 +
  
 
== Tested with ==
 
== Tested with ==
  
 
Firefox 7.0.1
 
Firefox 7.0.1

Revision as of 18:59, 5 January 2012

Article Metadata
Code ExampleCompatibility
Platform(s): Web Browser
Article
Created: Maveric (13 Nov 2011)
Last edited: jasfox (05 Jan 2012)


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

Contents

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.


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

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

256 page views in the last 30 days.