×
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 - Add Ovi Maps category)
jasfox (Talk | contribs)
m (Jasfox - - Example code)
(25 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:Web]][[Category:Browser]][[Category:Ovi Maps]]
+
[[Category:Nokia Maps]][[Category:Code Snippet]][[Category:JavaScript]]
==Introduction==
+
{{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. }}
  
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.
+
{{ArticleMetaData
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices=Google Chrome, Mozilla Firefox, Internet Explorer, Opera
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= Web browser
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|dependencies=Nokia Maps 2.2.3
 +
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= Nokia Maps, JavaScript, Geocoordinates, onclick
 +
|id= <!-- Article Id (Knowledge base articles only) -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by=<!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= [[User:avnee.nathani]]
 +
|update-timestamp= 20120106
 +
|creationdate=20110621
 +
|author=[[User:Maveric]]
 +
}}
 +
{{SeeAlso|
 +
*  [http://developer.here.net/javascript_api Nokia Maps API]
 +
* [http://developer.here.net/apiexplorer/examples/api-for-js/events/map-coordinate-on-click.html Coordinates on click]  }}
  
 
==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 note 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  you must obtain and use authentication and authorization credentials to use the services. Please read the
 +
[http://developer.here.net/terms_conditions Terms and Conditions] and check the [http://developer.here.net/web/guest/plans 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  [http://developer.here.net/docs/maps_js/topics/credentials.html#acquiring-credentials here]
  
 
==Implementation==
 
==Implementation==
  
To achieve what we are about to do, we will use A class to provide translations between projected pixel coordinates and GEO coordinates.
+
To achieve what we are about to do, we will need to use a method called {{Icode|pixelToGeo()}}, definition below:
  
<code java>
+
<code javascript>pixelToGeo (coord) : nokia.maps.geo.Coordinate - this will translate a projected pixel coordinate into a GEO Coordinate.</code>
new ovi.mapsapi.geo.PixelProjection (projection, width, height, [offsetX, [offsetY]])
+
</code>
+
 
+
The method will need is:
+
 
+
<code java>pixelToGeo (coord) : ovi.mapsapi.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 {{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 its target and bubbling phases.
  
 
Parameters:
 
Parameters:
Line 32: Line 52:
 
==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 a pixel projection coordinate to a {{Icode|geo.Coordinate}}. The result is sent to screen with JavaScript {{Icode|Alert()}} method. Remember to add in your own [[How to Obtain Your Own Nokia appID and Token| AppId and Token]].
  
<code java>
+
<code javascript>
 +
<!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.3/jsl.js" charset="utf-8">
 +
        </script>
 +
    </head>
 +
    <body>
  
<html>
+
        <div id="mapContainer" style="z-index: -1; left:0px; top:0px; width: 100%; height: 80%; position: absolute;"></div>
<head>
+
        <script type="text/javascript">
<title>Pixel to geocoordinate</title>
+
/////////////////////////////////////////////////////////////////////////////////////
<script src="http://api.maps.ovi.com/jsl.js" type="text/javascript" charset="utf-8"></script>
+
// Don't forget to set your API credentials
</head>
+
//
<body>
+
// Replace with your appId and token which you can obtain when you
<div id="map" style="z-index: -1; left:0px; top:0px; width: 100%; height: 100%; position: absolute;"></div>
+
// register on http://api.developer.nokia.com/
<script type="text/javascript">
+
//
 
+
nokia.Settings.set( "appId", "YOUR APP ID GOES HERE");  
// Set the authentication token, which is needed by the web-services to authenticate your application.
+
nokia.Settings.set( "authenticationToken", "YOUR AUTHENTICATION TOKEN GOES HERE");
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 map = new nokia.maps.map.Display(document.getElementById("mapContainer"), {'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();
 
                //Number displayX The horizontal pixel position where the event occurred relative to the top-left corner of the display associated with the event.
 
                //Number displayY The vertical pixel position where the event occurred relative to the to the top-left corner of the display associated with the event.
 
 
}
 
}
 
+
 
+
 
function add_Marker_where_clicked(){
 
function add_Marker_where_clicked(){
 
+
     myMarker = new nokia.maps.map.Marker(clicked_location);
     myMarker = new ovi.mapsapi.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 the Nokia Maps API==
 
+
Please check out the Ovi Maps API full documentation and API reference here:
+
 
+
http://api.maps.ovi.com
+
 
+
==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:
+
 
+
http://api.maps.ovi.com
+
 
+
==Tested on==
+
  
Google Chrome 11.0x
+
Please check out the Nokia Maps API full documentation and API reference here:
 +
* [http://developer.here.net/javascript_api Nokia Maps API]
  
Mozilla Firefox 5.0
+
You may also access the interactive API explorer
 +
* [http://developer.here.net/javascript_api_explorer API explorer]

Revision as of 14:19, 4 January 2013

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, Mozilla Firefox, Internet Explorer, Opera
Compatibility
Platform(s): Web browser
Dependencies: Nokia Maps 2.2.3
Article
Keywords: Nokia Maps, JavaScript, Geocoordinates, onclick
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (06 Jan 2012)
Last edited: jasfox (04 Jan 2013)

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 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 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 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.3/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 Nokia Maps API

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

You may also access the interactive API explorer

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

×