×
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 - Bot addition of Template:ArticleMetaData)
avnee.nathani (Talk | contribs)
(Avnee.nathani - Updating)
Line 1: Line 1:
 +
[[Category:Web]][[Category:Browser]][[Category:Nokia Maps]][[Category:Location]][[Category:Web]][[Category:Nokia Maps]]
 +
{{Abstract|This article explains how to convert a map screen's pixel to a geocoordinate. }}
 +
 
{{ArticleMetaData
 
{{ArticleMetaData
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
Line 4: Line 7:
 
|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=Nokia Maps 2.1.0
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
Line 17: Line 20:
 
|review-by=<!-- After re-review: [[User:username]] -->
 
|review-by=<!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|update-by= <!-- After significant update: [[User:username]]-->
+
|update-by= [[User:avnee.nathani]]
|update-timestamp= <!-- After significant update: YYYYMMDD -->
+
|update-timestamp= 20120106
 
|creationdate=20110621
 
|creationdate=20110621
 
|author=[[User:Maveric]]
 
|author=[[User:Maveric]]
 
}}
 
}}
  
[[Category:Web]][[Category:Browser]][[Category:Nokia Maps]]
 
 
==Introduction==
 
==Introduction==
  
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.
+
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==
  
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".<br /><br />
+
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==
  
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"<br /><br />
+
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 [http://www.developer.nokia.com/Develop/Maps/Quota/ 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 [https://api.developer.nokia.com/ovi-api Nokia Developer API Registration page].
  
 
==Implementation==
 
==Implementation==
Line 40: Line 42:
 
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 pixelToGeo, definition below:
  
<code java>pixelToGeo (coord) : ovi.mapsapi.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 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.
+
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:
 
Parameters:
Line 48: Line 50:
 
{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:Nokia Maps]]
 
 
==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 "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 java>
+
<code javascript>
 
+
 
<html>
 
<html>
 
<head>
 
<head>
 
<title>Pixel to geocoordinate</title>
 
<title>Pixel to geocoordinate</title>
<script src="http://api.maps.ovi.com/jsl.js" type="text/javascript" charset="utf-8"></script>
+
<script type="text/javascript" src="http://api.maps.nokia.com/2.0.0/jsl.js" charset="utf-8"></script>
 
</head>
 
</head>
 
<body>
 
<body>
 
<div id="map" style="z-index: -1; left:0px; top:0px; width: 100%; height: 100%; position: absolute;"></div>
 
<div id="map" style="z-index: -1; left:0px; top:0px; width: 100%; height: 100%; position: absolute;"></div>
 
<script type="text/javascript">
 
<script type="text/javascript">
 
+
 
// Set the authentication token, which is needed by the web-services to authenticate your application.
 
// Set the authentication token, which is needed by the web-services to authenticate your application.
ovi.mapsapi.util.ApplicationContext.set("authenticationToken", "<Token>");
+
nokia.maps.util.ApplicationContext.set("authenticationToken", "<Token>");
 
+
var map = new ovi.mapsapi.map.Display(document.getElementById("map"), {'zoomLevel':4, 'center':[53.1, 15.1]});
+
var map = new nokia.maps.map.Display(document.getElementById("map"), {'zoomLevel':4, 'center':[53.1, 15.1]});
 
+
 
var clicked_location = "";
 
var clicked_location = "";
 
var myContainer;
 
var myContainer;
 
+
 
map.addListener("click", eventListener);  // add the listener for mouse click
 
map.addListener("click", eventListener);  // add the listener for mouse click
 
+
 
map.set("baseMapType", map.NORMAL);
 
map.set("baseMapType", map.NORMAL);
 
+
 
function eventListener(event){
 
function eventListener(event){
 
alert("in EventListener");
 
alert("in EventListener");
 
     clicked_location = map.pixelToGeo(event.displayX, event.displayY);
 
     clicked_location = map.pixelToGeo(event.displayX, event.displayY);
 
+
 
     alert(clicked_location.toString());
 
     alert(clicked_location.toString());
 
     alert("Adding a Marker there");
 
     alert("Adding a Marker there");
 
     add_Marker_where_clicked();
 
     add_Marker_where_clicked();
 
}
 
}
 
+
 
+
 
function add_Marker_where_clicked(){
 
function add_Marker_where_clicked(){
 
+
     myMarker = new ovi.mapsapi.map.Marker(clicked_location);
+
     myMarker = new nokia.maps.map.Marker(clicked_location);
 
+
 
     map.objects.add(myMarker);
 
     map.objects.add(myMarker);
 
+
 
}
 
}
 
</script>
 
</script>
 
</body>
 
</body>
 
</html>
 
</html>
 
 
</code>
 
</code>
  
==For more on Ovi Maps API==
+
==For more on Nokia Maps API==
  
Please check out the Ovi Maps API full documentation and API reference here:
+
Please check out the Nokia Maps API full documentation and API reference here:
 +
* http://api.maps.nokia.com/
  
http://api.maps.ovi.com
+
You may also access the interactive Nokia Maps API playground,
 +
* http://api.maps.nokia.com/2.1.0/playground/index.html
  
 
==Tested with==
 
==Tested with==
  
Google Chrome 11.0x
+
* Google Chrome 11.0x
 
+
* Mozilla Firefox 5.0
Mozilla Firefox 5.0
+
 
+
==For more on Ovi Maps API==
+
 
+
Please check out the Ovi Maps API full documentation and API reference here:
+
 
+
http://api.maps.ovi.com
+
 
+
==Tested on==
+
 
+
Google Chrome 11.0x
+
 
+
Mozilla Firefox 5.0
+

Revision as of 19:30, 6 January 2012

This article explains how to convert a map screen's pixel to a geocoordinate.

Article Metadata
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: avnee.nathani (06 Jan 2012)

Contents

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

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
168 page views in the last 30 days.
×