HERE Maps API - Using the geocoding service
hamishwillee
(Talk | contribs) m (Hamishwillee - Automated change of category from Ovi Maps to Nokia Maps) |
(Avnee.nathani - Updated article for Nokia Maps Javascript API v2.0. (Released in Dec 2011)) |
||
| Line 4: | Line 4: | ||
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 | + | 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 | 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 | ||
| Line 14: | Line 14: | ||
==Prerequisites== | ==Prerequisites== | ||
| − | + | Nokia Maps API supported web browser (basically any modern web browser) | |
| − | + | ||
| − | + | ||
==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]. | |
| − | + | ||
| − | + | ||
| − | + | ||
==Implementation== | ==Implementation== | ||
| Line 37: | Line 32: | ||
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 | |
| − | + | <tt>rs</tt> is the search result | |
| − | The searchManager is instantiated with : var searchManager = new | + | The searchManager is instantiated with : <tt>var searchManager = new nokia.maps.search.Manager();</tt> |
| − | The check for | + | The check for <tt>if(value == "finished")</tt> will define what will happen when the search is finished. |
| − | With the | + | With the <tt>rs = (new nokia.maps.search.component.SearchResultSet(observedManager.locations)).container;</tt> we will put the Markers on the map. |
| − | The | + | The <tt>bbox = new nokia.maps.geo.BoundingBox...</tt> sets a fixed bounding box. |
| Line 54: | Line 49: | ||
<html> | <html> | ||
<head> | <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" /> | <link rel="stylesheet" href="style.css" /> | ||
</head> | </head> | ||
| Line 71: | Line 67: | ||
var map; | var map; | ||
| − | map = new | + | map = new nokia.maps.map.Display(document.getElementById("map"), |
{ | { | ||
| − | components: [ new | + | components: [ new nokia.maps.map.component.Behavior(), |
| − | new | + | new nokia.maps.map.component.ZoomBar(), |
| − | new | + | new nokia.maps.map.component.Overview(), |
| − | new | + | new nokia.maps.map.component.TypeSelector(), |
| − | new | + | new nokia.maps.map.component.ScaleBar()], |
'zoomLevel': 3, | 'zoomLevel': 3, | ||
}); | }); | ||
| Line 86: | Line 82: | ||
rs, | rs, | ||
prox = { | prox = { | ||
| − | center: new | + | center: new nokia.maps.geo.Coordinate(52.5, 13.3333), |
radius: 1200 | radius: 1200 | ||
}, | }, | ||
| − | bbox = new | + | bbox = new nokia.maps.geo.BoundingBox( |
| − | new | + | new nokia.maps.geo.Coordinate(50.1146125793467, 8.68348503112893), |
| − | new | + | new nokia.maps.geo.Coordinate(50.07635498046865, 8.517169952392568) |
); | ); | ||
| − | var searchManager = new | + | var searchManager = new nokia.maps.search.Manager(); |
searchManager.addObserver("state", function(observedManager, key, value) { | searchManager.addObserver("state", function(observedManager, key, value) { | ||
if(value == "finished") { | if(value == "finished") { | ||
if (observedManager.locations.length > 0) { | if (observedManager.locations.length > 0) { | ||
if (rs) map.objects.remove(rs); | if (rs) map.objects.remove(rs); | ||
| − | rs = (new | + | rs = (new nokia.maps.search.component.SearchResultSet(observedManager.locations)).container; |
alert("SEARCH ENDED SUCCESSFULLY"); | alert("SEARCH ENDED SUCCESSFULLY"); | ||
| Line 121: | Line 117: | ||
</code> | </code> | ||
| + | ==For more on Nokia Maps API== | ||
| − | + | Please check out the Nokia Maps API full documentation and API reference here: | |
| + | * http://api.maps.nokia.com/ | ||
| − | + | You may also access the interactive Nokia Maps API playground, | |
| − | + | * http://api.maps.nokia.com/2.1.0/playground/index.html | |
| − | http://api.maps. | + | |
==Tested with== | ==Tested with== | ||
| − | Google Chrome 11.0x | + | * Google Chrome 11.0x |
| − | + | * Mozilla Firefox 5.0 | |
| − | Mozilla Firefox 5.0 | + | |
Revision as of 13:48, 31 December 2011
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>
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

