×
Namespaces

Variants
Actions
Revision as of 02:40, 2 January 2012 by hamishwillee (Talk | contribs)

HERE Maps API - Using the geocoding service

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to use Nokia Maps search and geocoding service.

Article Metadata
Compatibility
Platform(s): Web browser
Dependencies: Nokia Maps 2.1.0
Article
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: hamishwillee (02 Jan 2012)

Contents

Introduction

In this example we will create a small online tool for the user to search the globe for a city, country and so on.

We will utilize the Nokia Maps Search and geocoding service for this.

The user can enter any string to the text box and if the search text can be geocoded to a physical location, we will put a Marker on the map for each of the found target on the result list.

For example a search for "Berlin" would result in several map Markers, as there are more than one city on the globe with that name.


Prerequisites

Nokia Maps API supported web browser (basically any modern web browser)

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

The full example code is provided below.

Comments on the code

We will utilize the zoomTo to cover all the findings, so the map will be zoomed out far enough for them all to become visible. For the zoomTo we must provide the name of the boundingBox, keepCenter (false by default) and optionally animation name.

If the keepCenter flag is used (true) the size of the viewport will be increased such that the original map center is still located in the center of the viewport.

var searchTerm The object used for the search var searchTerm rs is the search result

The searchManager is instantiated with : var searchManager = new nokia.maps.search.Manager();

The check for if(value == "finished") will define what will happen when the search is finished.

With the rs = (new nokia.maps.search.component.SearchResultSet(observedManager.locations)).container; we will put the Markers on the map.

The bbox = new nokia.maps.geo.BoundingBox... sets a fixed bounding box.


Example code

<html>
<head>
<script type="text/javascript"
src="http://api.maps.nokia.com/2.0.0/jsl.js" charset="utf-8">
</script>
<link rel="stylesheet" href="style.css" />
</head>
<div id="map" style="width:100%; height:80%;"></div>
<body style = "width:80%; height:80%;">
<div id="extra">
<input type=text id="geo_text_box">
<INPUT TYPE=BUTTON VALUE="Geo code with name" ONCLICK="mapStartGeoCode()">
<br>
</div>
</body>
<script type="text/javascript">
 
 
var map;
 
map = new nokia.maps.map.Display(document.getElementById("map"),
{
 
components: [ new nokia.maps.map.component.Behavior(),
new nokia.maps.map.component.ZoomBar(),
new nokia.maps.map.component.Overview(),
new nokia.maps.map.component.TypeSelector(),
new nokia.maps.map.component.ScaleBar()],
'zoomLevel': 3,
});
 
function mapStartGeoCode(){
var searchTerm,
rs,
prox = {
center: new nokia.maps.geo.Coordinate(52.5, 13.3333),
radius: 1200
},
bbox = new nokia.maps.geo.BoundingBox(
new nokia.maps.geo.Coordinate(50.1146125793467, 8.68348503112893),
new nokia.maps.geo.Coordinate(50.07635498046865, 8.517169952392568)
);
var searchManager = new nokia.maps.search.Manager();
searchManager.addObserver("state", function(observedManager, key, value) {
if(value == "finished") {
if (observedManager.locations.length > 0) {
if (rs) map.objects.remove(rs);
rs = (new nokia.maps.search.component.SearchResultSet(observedManager.locations)).container;
 
alert("SEARCH ENDED SUCCESSFULLY");
map.objects.add(rs);
map.zoomTo(rs.getBoundingBox(), true);
}
} else if(value == "failed") {
 
alert("SEARCH FAILED.");
}
});
 
var geocode = document.getElementById("geo_text_box");
alert(geocode.value);
searchTerm = geocode.value;
searchManager.geocode(searchTerm, prox);
}
</script>
</html>
<code java>

Screenshots

NokiaMapsGeocoding.png

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