×
Namespaces

Variants
Actions
Revision as of 10:33, 20 April 2012 by jasfox (Talk | contribs)

HERE Maps API - Performing multiple concurrent search requests

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Internet Explorer, Google Chrome , Firefox, Opera
Compatibility
Platform(s): Web
Dependencies: Nokia Maps 2.2.0
Article
Created: Maveric (25 Oct 2011)
Last edited: jasfox (20 Apr 2012)

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 concurrent requests.

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

This is a fully working example code in HTML and JavaScript.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.0/jsl.js"></script>
<title>Concurrent Search example</title>
</head>
<body>
<div id="map" style="width:100%; height:100%; 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");
//
/////////////////////////////////////////////////////////////////////////////////////
 
//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 });
 
// we will put our address markers into this container
var addressContainer = new nokia.maps.map.Container();
 
// this function will be used when all managers have returned
var 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, true);
} 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
 
var onSearchComplete = function (data, requestStatus) {
if (requestStatus == "OK") {
// if we are finished, we add a marker for the mapped position
addressContainer.objects.add(new nokia.maps.map.StandardMarker(data.location.position));
 
//increment the counter to notify another manager has finished
managersFinished++;
} else if(requestStatus === "ERROR") {
 
// 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();
}
};
 
//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" ];
var i = addresses.length;
var managersFinished = 0;
 
// iterate over all addresses, create a manager for each of them,
// add the observer and call the geocode method
 
while(i--) {
nokia.places.search.manager.geoCode({
searchTerm :addresses[i],
onComplete: onSearchComplete
});
}
 
</script>
</body>
</html>

Screenshot

The following screenshot shows the resultant map after making the search.

Concurrent-search.png

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

×