×
Namespaces

Variants
Actions
Revision as of 14:26, 21 June 2011 by Maveric (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

From Nokia Developer Wiki
Jump to: navigation, search

Contents

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.

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

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"

Implementation

To achieve what we are about to do, we will use A class to provide translations between projected pixel coordinates and GEO coordinates.

new ovi.mapsapi.geo.PixelProjection (projection, width, height, [offsetX, [offsetY]])

The method will need is:

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". In there, the user clickedposition will be converted from pixel projection coordinate to a geocoordinate. The result is sent to screen with JavaScript Alert function.

<html>
<head>
<title>Polyline on the map</title>
<script src="http://api.maps.ovi.com/jsl.js" type="text/javascript" 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.
ovi.mapsapi.util.ApplicationContext.set("authenticationToken", "<Token>");
 
var map = new ovi.mapsapi.map.Display(document.getElementById("map"), {'zoomLevel':4, 'center':[53.1, 15.1]});
 
map.addListener("click", eventListener); // add the listener for mouse click
 
map.set("baseMapType", map.NORMAL);
 
function eventListener(event){
alert("in EventListener");
//event.preventDefault();
var clicked_location = map.pixelToGeo(event.displayX, event.displayY); // Conversion from the clicked pixel coordinate to geocoordinate.
 
alert(clicked_location.toString());
alert("Adding a Marker there");
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.
}


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

139 page views in the last 30 days.
×