×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Using the geocoding service

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - Updated example to use Nokia Maps API 2.2)
jasfox (Talk | contribs)
m (Jasfox - Update code commentary to reflect 2.2)
Line 33: Line 33:
 
In this example we will create a small online tool for the user to search the globe for a city, country and so on.
 
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.
+
We will utilize both the Nokia 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 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.
+
  
 +
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 more than one city on the globe with that name, however a gecode search would only
 +
find the most significant one - i.e the Capital of Germany.
  
 
==Prerequisites==
 
==Prerequisites==
Line 47: Line 46:
 
==Important about Maps credentials==
 
==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 [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].
+
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
 +
s, 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 free from the [https://api.developer.nokia.com/ovi-api Nokia Developer API Registration page].
  
 
==Implementation==
 
==Implementation==
Line 55: Line 55:
 
==Comments on the code==
 
==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.
+
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.
+
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  
 
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.  
 
located in the center of the viewport.  
  
<tt>var searchTerm</tt> The object used for the search var searchTerm
+
*  A call to ''nokia.places.search.manager.geoCode''  will call the geocoding service.
<tt>rs</tt> is the search result
+
* A call to ''nokia.places.search.manager.findPlaces'' will call a free text search.
 
+
The searchManager is instantiated with : <tt>var searchManager = new nokia.maps.search.Manager();</tt>
+
 
+
The check for <tt>if(value == "finished")</tt>  will define what will happen when the search is finished.
+
  
With the <tt>rs = (new nokia.maps.search.component.SearchResultSet(observedManager.locations)).container;</tt> we will put the Markers on the map.
+
Note that if you enter a detailed address, the results are likely to be closer to each other.
  
The <tt>bbox = new nokia.maps.geo.BoundingBox...</tt> sets a fixed bounding box.
 
  
  

Revision as of 18:28, 12 April 2012

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

Article Metadata
Tested with
Devices(s): Internet Explorer, Google Chrome , Firefox
Compatibility
Platform(s): Web
Dependencies: Nokia Maps 2.2.0
Article
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (12 Apr 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 both the Nokia 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 more than one city on the globe with that name, however a gecode search would only find the most significant one - i.e the Capital of Germany.

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 you must obtain and use authentication and authorization credentials to use the services s, 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 free 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.

*  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 if you enter a detailed address, the results are likely to be closer to each other.


Example code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.0/jsl.js"></script>
<title>Search example</title>
</head>
<body>
<div id="extra" style="top:0%; position: absolute;">
<label for="geo_text_box"> Geocode </label><br/>
<input type="text" id="geo_text_box"/>
<input type="button" value="Geo code with name" onclick="mapStartGeoCode()"/>
<br/>
<label for="geo_text_box"> Free text search</label><br/>
<input type="text" id="free_text_text_box"/>
<input type="button" value="Search" onclick="mapStartFreeTextSearch()"/>
</div>
<div id="map" style="top:30%; width:100%; height:70%; 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;
 
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': 10,
});
 
map.setCenter (new nokia.maps.geo.Coordinate(18.975, 72.825833));
 
 
var onGeocodeComplete = function (data, requestStatus , requestId) {
 
if (requestStatus == "OK") {
alert("GEOCODE ENDED SUCCESSFULLY");
var marker = new nokia.maps.map.StandardMarker(data.location.position);
map.objects.add(marker);
map.zoomTo(marker.getBoundingBox(), true);
 
} else if(requestStatus == "ERROR") {
alert("GEOCODE FAILED.");
}
};
 
var onSearchComplete = function (data, requestStatus , requestId) {
 
if (requestStatus == "OK") {
alert("SEARCH ENDED SUCCESSFULLY");
// 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.");
}
};
 
function mapStartGeoCode(){
var geocode = document.getElementById("geo_text_box");
 
nokia.places.search.manager.geoCode({
searchTerm : geocode.value,
onComplete: onGeocodeComplete
});
}
 
function mapStartFreeTextSearch(){
var freetext = document.getElementById("free_text_text_box");
 
nokia.places.search.manager.findPlaces({
searchTerm : freetext.value,
onComplete: onSearchComplete,
searchCenter: map.center
});
 
 
}
 
</script>
</body>
</html>

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

×