×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Using the geocoding service

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Ovi Maps category)
jasfox (Talk | contribs)
m (Jasfox - update to 2.2.4)
(31 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:Web]][[Category:Browser]][[Category:Ovi Maps]]
+
[[Category:Nokia Maps]][[Category:Code Snippet]][[Category:JavaScript]]
 +
{{Abstract|This article explains how to use Nokia Maps search and geocoding service.}}
 +
{{ArticleMetaData
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= Internet Explorer, Google Chrome , Firefox
 +
|platform= Web
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|dependencies=Nokia Maps 2.2.4
 +
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= Nokia Maps, JavaScript, search, geocoding
 +
|id= <!-- Article Id (Knowledge base articles only) -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by=<!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by=[[User:avnee.nathani]]
 +
|update-timestamp=20111231
 +
|creationdate=20110621
 +
|author=[[User:Maveric]]
 +
}}
 +
{{SeeAlso|
 +
*  [http://developer.here.net/javascript_api Nokia Maps API]
 +
* [http://developer.here.net/apiexplorer/examples/api-for-js/places-search/search-by-address.html Search example]
 +
}}
 +
 
 
==Introduction==
 
==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.
 
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 Ovi 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 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==
 
==Prerequisites==
  
Ovi Maps API supported web browser (basically any modern web browser)
+
Nokia  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 the map to any web page"
+
==Important note about maps credentials==
  
==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. 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]
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==
 
==Implementation==
Line 31: 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 {{Icode|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 we must provide the name of the boundingBox, keepCenter (false by default) and optionally animation name.
+
For the {{Icode|zoomTo()}} method we must provide the name of the {{Icode|boundingBox}}, {{Icode|keepCenter}} ({{Icode|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 {{Icode|keepCenter}} flag is used ({{Icode|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.  
  
"var searchTerm" The object used for the search var searchTerm
+
* A call to {{Icode|nokia.places.search.manager.geoCode()}} will call the '''geocoding''' service.
"rs" is the search result
+
* A call to {{Icode|nokia.places.search.manager.findPlaces()}} will call a '''free text''' search.
 
+
The searchManager is instantiated with : var searchManager = new ovi.mapsapi.search.Manager();
+
 
+
The check for "if(value == "finished")"  will define what will happen when the search is finished.
+
 
+
With the "rs = (new ovi.mapsapi.search.component.SearchResultSet(observedManager.locations)).container;" we will put the Markers on the map.
+
 
+
The "bbox = new ovi.mapsapi.geo.BoundingBox..." sets a fixed bounding box.
+
  
 +
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]].
<code java>
+
<code javascript>
<html>
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 +
<html>  
 
<head>
 
<head>
    <script src="http://api.maps.ovi.com/jsl.js" type="text/javascript" charset="utf-8">
+
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.4/jsl.js"></script>
    </script>
+
    <title>Search example</title>
      <link rel="stylesheet" href="style.css" />
+
 
</head>
 
</head>
<div id="map" style="width:100%; height:80%;"></div>
+
<body>
<body style = "width:80%; height:80%;">
+
<div id="extra" style="top:0%; position: absolute;">  
<div id="extra">                  
+
<label for="geo_text_box"> Geocode </label><br/>             
<input type=text id="geo_text_box">
+
<input type="text" id="geo_text_box"/>
   <INPUT TYPE=BUTTON VALUE="Geo code with name" ONCLICK="mapStartGeoCode()">
+
   <input type="button" value="Geo code with name" onclick="mapStartGeoCode()"/>
   <br>  
+
   <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>
</body>
+
<div id="map" style="top:30%; width:100%; height:70%; position: absolute;"></div>
 +
 
 
<script type="text/javascript">
 
<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;
 
var map;
 
+
map = new ovi.mapsapi.map.Display(document.getElementById("map"),  
+
map = new nokia.maps.map.Display(document.getElementById("map"),  
 
{
 
{
 
+
     components: [ new ovi.mapsapi.map.component.Behavior(),
+
     components: [ new nokia.maps.map.component.Behavior(),
                 new ovi.mapsapi.map.component.ZoomBar(),
+
                 new nokia.maps.map.component.ZoomBar(),
                 new ovi.mapsapi.map.component.Overview(),                             
+
                 new nokia.maps.map.component.Overview(),                             
                 new ovi.mapsapi.map.component.TypeSelector(),     
+
                 new nokia.maps.map.component.TypeSelector(),     
                 new ovi.mapsapi.map.component.ScaleBar()],
+
                 new nokia.maps.map.component.ScaleBar()],
     'zoomLevel': 3,  
+
     'zoomLevel': 10,  
 
});
 
});
  
function mapStartGeoCode(){
+
map.setCenter (new nokia.maps.geo.Coordinate(18.975, 72.825833));
  var searchTerm,
+
                rs,
+
                prox = {
+
                    center: new ovi.mapsapi.geo.Coordinate(52.5, 13.3333),
+
                    radius: 1200
+
                },
+
                bbox = new ovi.mapsapi.geo.BoundingBox(
+
                    new ovi.mapsapi.geo.Coordinate(50.1146125793467, 8.68348503112893),
+
                    new ovi.mapsapi.geo.Coordinate(50.07635498046865, 8.517169952392568)
+
                );
+
  var searchManager = new ovi.mapsapi.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 ovi.mapsapi.search.component.SearchResultSet(observedManager.locations)).container;
+
  
                        alert("SEARCH ENDED SUCCESSFULLY");
+
var onGeocodeComplete = function (data, requestStatus , requestId) {
                        map.objects.add(rs);
+
                        map.zoomTo(rs.getBoundingBox(), true);
+
if (requestStatus == "OK") { 
                    }
+
    alert("GEOCODE ENDED SUCCESSFULLY");
                } else if(value == "failed") {
+
    var marker = new nokia.maps.map.StandardMarker(data.location.position);
 +
    map.objects.add(marker);  
 +
    map.zoomTo(marker.getBoundingBox(), true);
  
                    alert("SEARCH FAILED.");
+
    } 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");
 
   var geocode = document.getElementById("geo_text_box");
   alert(geocode.value);
+
 
   searchTerm = geocode.value;
+
   nokia.places.search.manager.geoCode({
  searchManager.geocode(searchTerm, prox);
+
      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>
 
</script>
 +
</body>
 
</html>
 
</html>
<code java>
 
  
 
</code>
 
</code>
  
 +
==Screenshots==
 +
[[File:NokiaMapsGeocoding.png]]
  
==For more on Ovi Maps API==
+
==For more on the Nokia Maps API==
 
+
Please check out the Ovi Maps API full documentation and API reference here:
+
 
+
http://api.maps.ovi.com
+
 
+
==Tested with==
+
  
Google Chrome 11.0x
+
Please check out the Nokia Maps API full documentation and API reference here:
 +
* [http://developer.here.net/javascript_api Nokia Maps API]
  
Mozilla Firefox 5.0
+
You may also access the interactive API explorer
 +
* [http://developer.here.net/javascript_api_explorer API explorer]

Revision as of 14:21, 14 March 2013

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.4
Article
Keywords: Nokia Maps, JavaScript, search, geocoding
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (14 Mar 2013)

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

Nokia 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

The full example code is provided below.

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.

<!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.4/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 the Nokia Maps API

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

You may also access the interactive API explorer

873 page views in the last 30 days.
×