HERE Maps API - Converting a map screen pixel to a geo coordinate
This article explains how to convert a map screen's pixel to a geocoordinate. This is useful when we want to know the geocoordinates of the point user had clicked.
Article Metadata
Tested with
Compatibility
Article
See Also
Contents |
Prerequisites
HERE Maps API supported web browser (basically any modern web browser). The example assumes you have already added the HERE Maps to your web page as explained in the previous article HERE Maps API - Add Maps To Any Web Page
Important note about maps credentials
Nokia provides several services options within the Maps API offering. The service is free to use, but you must obtain and use authentication and authorization credentials to use the services. Please read the Terms and Conditions and check the Pricing Plans page to decide which business model best fits your needs. Authentication requires unique Maps API credentials, namely an AppId and a token. You can get these credentials free for free following the instructions here
Implementation
To achieve what we are about to do, we will need to use a method called pixelToGeo(), definition below:
pixelToGeo (coord) : nokia.maps.geo.Coordinate - this will translate a projected pixel coordinate into a GEO Coordinate.
We also need to use the EventListener() and addListener() method, which is defined by the HERE Maps API reference as follows: addListener (type, listener, useCapture) ObjectRegisters an event listener, depending on the useCapture parameter, on the capture phase of the event flow or its target and bubbling phases.
Parameters: {String} type Specifies the event type associated with the event for which the user is registering. {Function} listener The function to be called if the event occurs.
Example code
This example will add a new eventListener on the map and register the "click" event to fire the function eventListener(). We will get the position user had clicked and this will be converted from a pixel projection coordinate to a geo.Coordinate. The result is sent to screen with JavaScript Alert() method. Remember to add in your own AppId and Token.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Pixel to geocoordinate</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript"
src="http://api.maps.nokia.com/2.2.4/jsl.js" charset="utf-8">
</script>
</head>
<body>
<div id="mapContainer" style="z-index: -1; left:0px; top:0px; width: 100%; height: 80%; position: absolute;"></div>
<script type="text/javascript">
/////////////////////////////////////////////////////////////////////////////////////
// Don't forget to set your API credentials
//
// Replace with your appId and token which you can obtain when you
// register on http://api.developer.nokia.com/
//
nokia.Settings.set( "appId", "YOUR APP ID GOES HERE");
nokia.Settings.set( "authenticationToken", "YOUR AUTHENTICATION TOKEN GOES HERE");
//
/////////////////////////////////////////////////////////////////////////////////////
var map = new nokia.maps.map.Display(document.getElementById("mapContainer"), {'zoomLevel':4, 'center':[53.1, 15.1]});
var clicked_location = "";
var myContainer;
map.addListener("click", eventListener); // add the listener for mouse click
map.set("baseMapType", map.NORMAL);
function eventListener(event){
alert("in EventListener");
clicked_location = map.pixelToGeo(event.displayX, event.displayY);
alert(clicked_location.toString());
alert("Adding a Marker there");
add_Marker_where_clicked();
}
function add_Marker_where_clicked(){
myMarker = new nokia.maps.map.Marker(clicked_location);
map.objects.add(myMarker);
}
</script>
</body>
</html>
For more on the HERE Maps API
Please check out the HERE Maps API full documentation and API reference here:
You may also access the interactive API explorer


Spacemunkay - Not using PixelProjection class?
Example was helpful but looks like it uses pixelToGeo from the Display class, not the PixelProjection class. Could you give an example of using the PixelProjection class too? Thanks for your help so far.spacemunkay 22:42, 18 August 2011 (EEST)
Jasfox - PixelProjection class has been deprecated
There is no PixelProjection example since the PixelProjection class has been deprecated and removed from the API since version 2.2.1 I assume the class was removed since the API currently only supports one projection (Normalized Mercator).
Display.pixelToGeo() does the job anyway.jasfox 18:32, 20 December 2012 (EET)