×
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 - - Example code)
jasfox (Talk | contribs)
m (Jasfox - - For more on the HERE Maps API)
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Nokia Maps]][[Category:Code Snippet]][[Category:JavaScript]]
+
[[Category:HERE Maps]][[Category:JavaScript]][[Category:Code Examples]]
{{Abstract|This article explains how to use Nokia Maps search and geocoding service.}}
+
{{Abstract|This article explains how to use HERE Maps search and geocoding service.}}
{{ArticleMetaData
+
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
+
|sourcecode=  [https://github.com/heremaps/examples/blob/master/maps_api_for_javascript/simple-examples/geocoding.html geocoding.html]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= Internet Explorer, Google Chrome , Firefox
+
|devices= Internet Explorer, Google Chrome, Firefox
|platform= Web
+
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= Web
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies=Nokia Maps 2.2.3
+
|dependencies= HERE Maps API 2.5.3
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= Nokia Maps, JavaScript, search, geocoding
+
|keywords= HERE Maps, JavaScript, search, geocoding
|id= <!-- Article Id (Knowledge base articles only) -->
+
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|update-by=[[User:avnee.nathani]]
+
|update-by= [[User:avnee.nathani]]
|update-timestamp=20111231
+
|update-timestamp= 20111231
|creationdate=20110621
+
|creationdate= 20110621
|author=[[User:Maveric]]
+
|author= [[User:Maveric]]
 
}}
 
}}
 
{{SeeAlso|
 
{{SeeAlso|
*  [http://developer.here.net/javascript_api Nokia Maps API]  
+
*  [http://developer.here.net/javascript_api HERE Maps API]  
 
* [http://developer.here.net/apiexplorer/examples/api-for-js/places-search/search-by-address.html Search example]
 
* [http://developer.here.net/apiexplorer/examples/api-for-js/places-search/search-by-address.html Search example]
 
  }}
 
  }}
Line 33: Line 32:
 
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 both the Nokia Maps Free text Search and Geocoding services for this.
+
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.
+
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.
+
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
+
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.
 
find the most significant one - i.e the Capital of Germany.
  
 
==Prerequisites==
 
==Prerequisites==
  
Nokia  Maps API supported web browser (basically any modern web browser)
+
HERE Maps API supported web browser (basically any modern web browser)
  
 
==Important note about maps credentials==
 
==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
+
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]
 
[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==
  
The full example code is provided below.
+
[[File: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==
 
==Comments on the code==
Line 64: Line 67:
 
* A call to {{Icode|nokia.places.search.manager.findPlaces()}} will call a '''free text''' search.
 
* A call to {{Icode|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.
+
Note that the greater the detail you enter in an address, the higher the likelihood that the results will resemble one another.
  
 
==Example code==
 
==Example code==
 
This example demonstrates the use of both the geocoding service and the places free text search. Remember to add in your own [[How to Obtain Your Own Nokia appID and Token| AppId and Token]].
 
This example demonstrates the use of both the geocoding service and the places free text search. Remember to add in your own [[How to Obtain Your Own Nokia appID and Token| AppId and Token]].
 +
 +
A geocoding request can be made using the following code:
 
<code javascript>
 
<code javascript>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
function startGeoCode(){
<html>
+
var geocode = document.getElementById("geo_text_box");
<head>
+
nokia.places.search.manager.geoCode({
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.3/jsl.js"></script>
+
searchTerm :  geocode.value,
    <title>Search example</title>
+
onCompleteonGeocodeComplete
</head>
+
});  
<body>
+
}
<div id="extra" style="top:0%; position: absolute;">
+
</code>
<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">
+
The callback function to place a marker on the map is as shown:
/////////////////////////////////////////////////////////////////////////////////////
+
<code javascript>
// Don't forget to set your API credentials
+
function onGeocodeComplete(data, requestStatus, requestId) {
//
+
if (requestStatus == "OK") { 
// Replace with your appId and token which you can obtain when you
+
alert("GEOCODE ENDED SUCCESSFULLY");
// register on http://api.developer.nokia.com/
+
map.objects.clear();
//
+
var marker = new nokia.maps.map.StandardMarker(data.location.position);
nokia.Settings.set( "appId", "YOUR APP ID GOES HERE");
+
map.objects.add(marker);  
nokia.Settings.set( "authenticationToken", "YOUR AUTHENTICATION TOKEN GOES HERE");
+
map.zoomTo(marker.getBoundingBox(), false);
//
+
if(map.get("zoomLevel") > 15){
/////////////////////////////////////////////////////////////////////////////////////
+
map.setZoomLevel(15);
+
+
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.");
+
 
}
 
}
};           
+
} else if(requestStatus == "ERROR") {
 
+
alert("GEOCODE FAILED.");
function mapStartGeoCode(){  
+
}
  var geocode = document.getElementById("geo_text_box");
+
 
+
  nokia.places.search.manager.geoCode({
+
      searchTerm :  geocode.value,
+
onComplete:  onGeocodeComplete
+
  });  
+
 
}
 
}
 +
</code>
  
function mapStartFreeTextSearch(){  
+
The Places API can be used to make a localised free-text search. The implementation is as shown:
  var freetext = document.getElementById("free_text_text_box");
+
<code javascript>
 
+
function startFreeTextSearch(){  
 +
var freetext = document.getElementById("free_text_text_box");
 
  nokia.places.search.manager.findPlaces({
 
  nokia.places.search.manager.findPlaces({
 
searchTerm :  freetext.value,
 
searchTerm :  freetext.value,
onComplete:  onSearchComplete,
+
onComplete:  onPlaceSearchComplete,
 
searchCenter: map.center
 
searchCenter: map.center
 
});
 
});
 
 
 
 
 
}
 
}
 +
</code>
  
</script>
+
The callback displays all of the results found:
</body>
+
<code javascript>
</html>
+
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.");
 +
}
 +
}
 
</code>
 
</code>
  
==Screenshots==
+
==For more on the HERE Maps API==
[[File:NokiaMapsGeocoding.png]]
+
  
==For more on the Nokia Maps API==
+
Please check out the HERE Maps API full documentation and API reference here:
 
+
* [http://developer.here.com/javascript_api HERE Maps API]
Please check out the Nokia Maps API full documentation and API reference here:
+
* [http://developer.here.net/javascript_api Nokia Maps API]
+
  
 
You may also access the interactive API explorer
 
You may also access the interactive API explorer
* [http://developer.here.net/javascript_api_explorer API explorer]
+
* [http://developer.here.com/javascript_api_explorer API explorer]
 +
 
 +
And additional examples on GitHub
 +
* [http://heremaps.github.io/examples/ HERE Maps on Github]

Latest revision as of 20:26, 12 March 2014

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

[edit] 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.

[edit] Prerequisites

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

[edit] 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

[edit] 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

[edit] 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.

[edit] 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.");
}
}

[edit] 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.
772 page views in the last 30 days.
×