HERE Maps API - Performing multiple concurrent search requests
m (Maveric - - →Implementation) |
m (Maveric - - →Implementation) |
||
| Line 32: | Line 32: | ||
<code java> | <code java> | ||
| − | <html> | + | <html> |
| − | <head> | + | <head> |
| − | <script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.0.0/jsl.js"></script> | + | <script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.0.0/jsl.js"></script> |
| − | </head> | + | </head> |
<body> | <body> | ||
| − | <div id="map" style="width:100%; height: | + | <div id="map" style="width:100%; height:100%;"></div> |
| + | </body> | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
//initialize a new map | //initialize a new map | ||
| − | |||
var display = new nokia.maps.map.Display(document.getElementById("map"), | var display = new nokia.maps.map.Display(document.getElementById("map"), | ||
| − | + | { "components": [ | |
| − | + | new nokia.maps.map.component.ZoomBar(), | |
| − | + | new nokia.maps.map.component.Behavior(), | |
| − | + | new nokia.maps.map.component.TypeSelector()], | |
| + | "zoomLevel": 10 }); | ||
| + | //Addresses to be displayed | ||
| + | var addresses = ["Helsinki, Finland", "Zaragoza, Spain", "Berlin, Germany", "Colorado Springs, USA", "Houston, TX", "London, UK", "Vancouver, Canada", "Hyderabad, India", "Beijing, China", "Ilomantsi, Finland" ], i = addresses.length, | ||
| − | + | // we will put our address markers into this container | |
| − | + | ||
| − | // we will put our address | + | |
addressContainer = new nokia.maps.map.Container(), | addressContainer = new nokia.maps.map.Container(), | ||
| Line 57: | Line 58: | ||
onAllManagersFinished = function() { | onAllManagersFinished = function() { | ||
| − | //we get the | + | //we get the bounding box of the container |
var bbox = addressContainer.getBoundingBox(); | var bbox = addressContainer.getBoundingBox(); | ||
| Line 65: | Line 66: | ||
if (bbox != null) { | if (bbox != null) { | ||
| − | |||
// we have at least one address mapped | // we have at least one address mapped | ||
// so we add the container and zoomTo it | // so we add the container and zoomTo it | ||
| Line 71: | Line 71: | ||
display.objects.add(addressContainer); | display.objects.add(addressContainer); | ||
display.zoomTo(bbox); | display.zoomTo(bbox); | ||
| − | |||
} else { | } else { | ||
| Line 77: | Line 76: | ||
alert("There are no addresses to show :(") | alert("There are no addresses to show :(") | ||
| − | }}, | + | } |
| + | }, | ||
| + | // we will use the same state obeserver function for all managers | ||
| − | |||
onManagerStateChange = function(manager, propertyName, value) | onManagerStateChange = function(manager, propertyName, value) | ||
| − | { | + | { if(value === "finished") |
| − | + | { | |
| − | + | // if we are finished, we add a marker for the mapped position | |
| − | // if we are finished, we add a marker for the mapped position | + | addressContainer.objects.add(new nokia.maps.map.StandardMarker(manager.locations[0].displayPosition)); |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| + | //increment the counter to notify another manager has finished | ||
| + | managersFinished++; | ||
} else if(value === "failed") | } else if(value === "failed") | ||
| − | + | { | |
// we'll also increment in case of an error | // we'll also increment in case of an error | ||
| − | |||
| + | managersFinished++; | ||
} | } | ||
| − | |||
| − | if(managersFinished === addresses.length) | + | // if all managers are finished, we call the final function |
| − | + | ||
| + | if(managersFinished === addresses.length) { onAllManagersFinished(); } | ||
}, manager, managersFinished = 0; | }, manager, managersFinished = 0; | ||
// iterate over all addresses, create a manager for each of them, | // iterate over all addresses, create a manager for each of them, | ||
| − | // add the observer and call the geocode method | + | // add the observer and call the geocode method |
| − | { | + | while(i--) |
| − | + | { manager = new nokia.maps.search.Manager(); | |
manager.addObserver("state", onManagerStateChange); | manager.addObserver("state", onManagerStateChange); | ||
manager.geocode(addresses[i]); | manager.geocode(addresses[i]); | ||
| Line 114: | Line 111: | ||
</script> | </script> | ||
</body> | </body> | ||
| − | </html> | + | </html> |
</code> | </code> | ||
Revision as of 13:35, 28 October 2011
Contents |
Introduction
In this article we will examine the following use case on Nokia Maps API.
1. Multiple addresses exist, a total of 10 for this example. 2. A Map Marker is needed to be displayed for each of the addresses. 3. Finally the ZoomLevel needs to be set to cover all of the Markers.
This article enforces the correct use of the search.Manager in case of multiple sequential requests.
Prerequisites
Ovi Maps API supported web browser (basically any modern web browser).
Please note: The title of this wiki article refers to the Ovi Maps API, but the code is already supporting the new version 2.0.0 titled Nokia Maps API. Also we will refer to the new domain of http://api.maps.nokia.com
Important about Maps credentials
With Nokia 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 Nokia Maps API Developers Guide section titled "Acquiring API credentials".
Implementation
This is a fully working example code in HTML and JavaScript.
<html>
<head>
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.0.0/jsl.js"></script>
</head>
<body>
<div id="map" style="width:100%; height:100%;"></div>
</body>
<script type="text/javascript">
//initialize a new map
var display = new nokia.maps.map.Display(document.getElementById("map"),
{ "components": [
new nokia.maps.map.component.ZoomBar(),
new nokia.maps.map.component.Behavior(),
new nokia.maps.map.component.TypeSelector()],
"zoomLevel": 10 });
//Addresses to be displayed
var addresses = ["Helsinki, Finland", "Zaragoza, Spain", "Berlin, Germany", "Colorado Springs, USA", "Houston, TX", "London, UK", "Vancouver, Canada", "Hyderabad, India", "Beijing, China", "Ilomantsi, Finland" ], i = addresses.length,
// we will put our address markers into this container
addressContainer = new nokia.maps.map.Container(),
// this function will be used when all managers have returned
onAllManagersFinished = function() {
//we get the bounding box of the container
var bbox = addressContainer.getBoundingBox();
// if the bounding box is null then there are no objects inside
// meaning no markers have been added to it
if (bbox != null) {
// we have at least one address mapped
// so we add the container and zoomTo it
display.objects.add(addressContainer);
display.zoomTo(bbox);
} else {
// otherwise we'll pop up an error message
alert("There are no addresses to show :(")
}
},
// we will use the same state obeserver function for all managers
onManagerStateChange = function(manager, propertyName, value)
{ if(value === "finished")
{
// if we are finished, we add a marker for the mapped position
addressContainer.objects.add(new nokia.maps.map.StandardMarker(manager.locations[0].displayPosition));
//increment the counter to notify another manager has finished
managersFinished++;
} else if(value === "failed")
{
// we'll also increment in case of an error
managersFinished++;
}
// if all managers are finished, we call the final function
if(managersFinished === addresses.length) { onAllManagersFinished(); }
}, manager, managersFinished = 0;
// iterate over all addresses, create a manager for each of them,
// add the observer and call the geocode method
while(i--)
{ manager = new nokia.maps.search.Manager();
manager.addObserver("state", onManagerStateChange);
manager.geocode(addresses[i]);
}
</script>
</body>
</html>
For more on Nokia Maps API
Please check out the Nokia Maps API full documentation and API reference here: http://api.maps.nokia.com
Tested with
Google Chrome 14.0.835.202 m
Mozilla Firefox 8.0

