×
Namespaces

Variants
Actions
(Difference between revisions)

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

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Updated devices ArticleMetaData - browser strictly not a device, but does indicate testing environment)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Icodes to markup inline code)
Line 1: Line 1:
 
[[Category:Web]][[Category:Browser]][[Category:Nokia Maps]][[Category:Location]][[Category:Web]][[Category:Nokia Maps]][[Category:Code Snippet]]
 
[[Category:Web]][[Category:Browser]][[Category:Nokia Maps]][[Category:Location]][[Category:Web]][[Category:Nokia Maps]][[Category:Code Snippet]]
{{Abstract|This article explains how to convert a map screen's pixel to a geocoordinate. }}
+
{{Abstract|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. }}
  
 
{{ArticleMetaData
 
{{ArticleMetaData
Line 25: Line 25:
 
|author=[[User:Maveric]]
 
|author=[[User:Maveric]]
 
}}
 
}}
 
==Introduction==
 
 
In this article we will convert map screen to a geocoordinate. This is useful when we want to know the geocoordinates of the point user had clicked.
 
  
 
==Prerequisites==
 
==Prerequisites==
  
Nokia Maps API supported web browser (basically any modern web browser). The example assumes you have already added the Nokia Maps to your web page as explained in the previous article [[Nokia Maps API - Add Maps To Any Web Page ]]
+
Nokia Maps API supported web browser (basically any modern web browser). The example assumes you have already added the Nokia Maps to your web page as explained in the previous article [[Nokia Maps API - Add Maps To Any Web Page]]
  
 
==Important about Maps credentials==
 
==Important about Maps credentials==
Line 40: Line 36:
 
==Implementation==
 
==Implementation==
  
To achieve what we are about to do, we will need to use a method called pixelToGeo, definition below:
+
To achieve what we are about to do, we will need to use a method called {{Icode|pixelToGeo}}, definition below:
  
 
<code javascript>pixelToGeo (coord) : nokia.maps.geo.Coordinate - this will translate a projected pixel coordinate into a GEO Coordinate.</code>
 
<code javascript>pixelToGeo (coord) : nokia.maps.geo.Coordinate - this will translate a projected pixel coordinate into a GEO Coordinate.</code>
  
We also need to use the EventListener and addListener method, which is defined by the Nokia 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.
+
We also need to use the {{Icode|EventListener()}} and {{Icode|addListener()}} method, which is defined by the Nokia Maps API reference as follows: {{Icode|addListener (type, listener, useCapture)}} ObjectRegisters an event listener, depending on the {{Icode|useCapture}} parameter, on the capture phase of the event flow or itstarget and bubbling phases.
  
 
Parameters:
 
Parameters:
Line 52: Line 48:
 
==Example code==
 
==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.
+
This example will add a new eventListener on the map and register the "click" event to fire the function {{Icode|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.
  
 
<code javascript>
 
<code javascript>

Revision as of 00:51, 9 January 2012

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
Devices(s): Google Chrome 11.0x, Mozilla Firefox 5.0
Compatibility
Platform(s): Web browser
Dependencies: Nokia Maps 2.1.0
Article
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (06 Jan 2012)
Last edited: hamishwillee (09 Jan 2012)

Contents

Prerequisites

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

Important about Maps credentials

Nokia provides several services options within the Maps API offering. The service is free to use, but if you complete the free registration process and obtain authentication and authorization credentials, your application will have priority access to the service and will thus avoid a potential performance penalty. Please read the Location API Business Models and Usage Restrictions 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 from the Nokia Developer API Registration page.

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

<html>
<head>
<title>Pixel to geocoordinate</title>
<script type="text/javascript" src="http://api.maps.nokia.com/2.0.0/jsl.js" charset="utf-8"></script>
</head>
<body>
<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.
nokia.maps.util.ApplicationContext.set("authenticationToken", "<Token>");
 
var map = new nokia.maps.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(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 Nokia Maps API

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

You may also access the interactive Nokia Maps API playground,

Tested with

  • Google Chrome 11.0x
  • Mozilla Firefox 5.0
138 page views in the last 30 days.
×