×
Namespaces

Variants
Actions

HERE Maps API - Using the geocoding service

From Nokia Developer Wiki
Jump to: navigation, search

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

Article Metadata
Code Example
Source file: geocoding.html
Tested with
Devices(s): Internet Explorer, Google Chrome, Firefox
Compatibility
Platform(s): Web
Dependencies: HERE Maps API 2.5.3
Article
Keywords: HERE Maps, JavaScript, search, geocoding
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (12 Mar 2014)

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 both the HERE Maps Free text Search and Geocoding services 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 single Marker on the map at the target location. Alternatively the user can enter a string to a text box and the search text can used in a places search where we will put a Marker on the map for each location found on the result list. For example a free text search for "Berlin" would result in several map Markers, as there are several towns and cities on the globe with that name, however a geocode search would only find the most significant one - i.e the Capital of Germany.

Prerequisites

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

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

NokiaMapsGeocoding.png

A fully working example can be found at:

http://rawgithub.com/heremaps/examples/master/maps_api_for_javascript/simple-examples/geocoding.html

Comments on the code

We will utilize the zoomTo() method to cover all the findings, so the map will be zoomed out far enough for them all to become visible. For the zoomTo() method 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.

  • A call to nokia.places.search.manager.geoCode() will call the geocoding service.
  • A call to nokia.places.search.manager.findPlaces() will call a free text search.

Note that the greater the detail you enter in an address, the higher the likelihood that the results will resemble one another.

Example code

This example demonstrates the use of both the geocoding service and the places free text search. Remember to add in your own AppId and Token.

A geocoding request can be made using the following code:

function startGeoCode(){ 
var geocode = document.getElementById("geo_text_box");
nokia.places.search.manager.geoCode({
searchTerm : geocode.value,
onComplete: onGeocodeComplete
});
}

The callback function to place a marker on the map is as shown:

function onGeocodeComplete(data, requestStatus, requestId) {
if (requestStatus == "OK") {
alert("GEOCODE ENDED SUCCESSFULLY");
map.objects.clear();
var marker = new nokia.maps.map.StandardMarker(data.location.position);
map.objects.add(marker);
map.zoomTo(marker.getBoundingBox(), false);
if(map.get("zoomLevel") > 15){
map.setZoomLevel(15);
}
} else if(requestStatus == "ERROR") {
alert("GEOCODE FAILED.");
}
}

The Places API can be used to make a localised free-text search. The implementation is as shown:

function startFreeTextSearch(){ 
var freetext = document.getElementById("free_text_text_box");
nokia.places.search.manager.findPlaces({
searchTerm : freetext.value,
onComplete: onPlaceSearchComplete,
searchCenter: map.center
});
}

The callback displays all of the results found:

function onPlaceSearchComplete(data, requestStatus, requestId) {
if (requestStatus == "OK") {
alert("SEARCH ENDED SUCCESSFULLY");
map.objects.clear();
// Convert all found locations into a set of markers
var resultSet = new nokia.maps.map.Container();
var locations = data.results.items;
for (i = 0, len = locations.length; i < len; i++) {
marker = new nokia.maps.map.StandardMarker(locations[i].position, { text: i+1 });
resultSet.objects.add(marker);
}
// Next we add the marker(s) to the map's object collection so they will be rendered onto the map
map.objects.add(resultSet);
// We zoom the map to a view that encapsulates all the markers into map's viewport
map.zoomTo(resultSet.getBoundingBox(), false);
} else if(requestStatus == "ERROR") {
alert("SEARCH FAILED.");
}
}

For more on the HERE Maps API

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

You may also access the interactive API explorer

And additional examples on GitHub

This page was last modified on 12 March 2014, at 20:26.
852 page views in the last 30 days.
×