(Difference between revisions)

HERE Maps API - Converting a map screen pixel to a geo coordinate

From Nokia Developer Wiki
Jump to: navigation, search
Maveric (Talk | contribs)
m (Maveric - - Example code)
hamishwillee (Talk | contribs)
m (Hamishwillee - Automated change of category from Ovi Maps to Nokia Maps)
Line 1: Line 1:
[[Category:Web]][[Category:Browser]][[Category:Ovi Maps]]
[[Category:Web]][[Category:Browser]][[Category:Nokia Maps]]
Line 24: Line 24:
{Function}        listener        The function to be called if the event occurs.
{Function}        listener        The function to be called if the event occurs.
[[Category:Location]][[Category:Web]][[Category:Ovi Maps]]
[[Category:Location]][[Category:Web]][[Category:Nokia Maps]]
==Example code==
==Example code==

Revision as of 02:00, 6 December 2011



In this article we will go thru how to convert map screen to a geocoordinate. This is useful when we want to know the geocoordinates of the point user had clicked.


Ovi Maps API supported web browser (basically any modern web browser)The example assumes you have already added the Ovi Maps to your web page as explained in the previous article "Ovi Maps - Add Map To Any Page".

Important about Maps credentials

With Ovi Maps API you can start without having any credentials given, but you might face a performance gap. In order to get the full potential out of the offering, you must get the credentials that authenticate your application against the Services. Please read through the Location API.For more information on how to obtain the credentials, please start with the Ovi Maps API Developers Guide section "Acquiring API credentials"


To achieve what we are about to do, we will need to use a method called pixelToGeo, definition below:

pixelToGeo (coord) : ovi.mapsapi.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 Ovi 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 itstarget 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 pixel projection coordinate to a geocoordinate. The result is sent to screen with JavaScript Alert function.

<title>Pixel to geocoordinate</title>
<script src="http://api.maps.ovi.com/jsl.js" type="text/javascript" charset="utf-8"></script>
<div id="map" style="z-index: -1; left:0px; top:0px; width: 100%; height: 100%; position: absolute;"></div>
<script type="text/javascript">
// Set the authentication token, which is needed by the web-services to authenticate your application.
ovi.mapsapi.util.ApplicationContext.set("authenticationToken", "<Token>");
var map = new ovi.mapsapi.map.Display(document.getElementById("map"), {'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("Adding a Marker there");
function add_Marker_where_clicked(){
myMarker = new ovi.mapsapi.map.Marker(clicked_location);

For more on Ovi Maps API

Please check out the Ovi Maps API full documentation and API reference here:


Tested with

Google Chrome 11.0x

Mozilla Firefox 5.0

For more on Ovi Maps API

Please check out the Ovi Maps API full documentation and API reference here:


Tested on

Google Chrome 11.0x

Mozilla Firefox 5.0

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