×
Namespaces

Variants
Actions

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

From Nokia Developer Wiki
Jump to: navigation, search

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
Code ExampleTested with
Devices(s): Google Chrome, Mozilla Firefox, Internet Explorer, Opera
Compatibility
Platform(s): Web browser
Dependencies: HERE Maps 2.5.3
Article
Keywords: HERE Maps, JavaScript, Geocoordinates, onclick
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (06 Jan 2012)
Last edited: jasfox (20 Dec 2013)

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.

function addClickListener(map){
map.addListener("click", function (evt){
alert("in EventListener");
coordinate = map.pixelToGeo(evt.displayX, evt.displayY);
alert("Adding a Marker at: "+ coordinate.toString());
map.objects.add(new nokia.maps.map.Marker(coordinate));
});
}


A working example can be found at:

http://heremaps.github.io/examples/examples.html#geocoordinate-on-click

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

This page was last modified on 20 December 2013, at 18:18.
244 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.

×