×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Performing multiple concurrent search requests

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - - Important note about maps credentials)
jasfox (Talk | contribs)
m (Jasfox - Update links)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Code Snippet]][[Category:Nokia Maps]][[Category:JavaScript]]
+
[[Category:HERE Maps]][[Category:JavaScript]][[Category:Code Examples]]
 
{{ArticleMetaData <!-- v1.1 -->
 
{{ArticleMetaData <!-- v1.1 -->
|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/demos/concurrent-search-requests/index.html concurrent-search-requests]]
 
|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, Opera  
 
|devices= Internet Explorer, Google Chrome , Firefox, Opera  
Line 7: Line 7:
 
|platform= Web
 
|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
+
|keywords= HERE Maps, JavaScript, Search
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|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 -->
Line 25: Line 25:
  
 
{{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/map-with-search-result-addresses.html Search example]
 
* [http://developer.here.net/apiexplorer/examples/api-for-js/places-search/map-with-search-result-addresses.html Search example]
 
  }}
 
  }}
Line 31: Line 31:
 
==Introduction==   
 
==Introduction==   
  
In this article we will examine the following usage of the Nokia Maps API.
+
In this article we will examine the following usage of the HERE Maps API.
 
# Multiple addresses exist, a total of 10 for this example.  
 
# Multiple addresses exist, a total of 10 for this example.  
 
# A Map {{Icode|Marker}} is needed to be displayed for each of the addresses.  
 
# A Map {{Icode|Marker}} is needed to be displayed for each of the addresses.  
Line 40: Line 40:
 
==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==
Line 48: Line 48:
  
 
==Implementation==   
 
==Implementation==   
 
This is a fully working example code in HTML and JavaScript.
 
  
 
<code javascript>   
 
<code javascript>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
+
function concurrentSearch(map){
<html>
+
// we will put our address markers into this container 
<head>   
+
addressContainer = new nokia.maps.map.Container();
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.3/jsl.js"></script>
+
managersFinished = 0;  
<title>Concurrent Search example</title>
+
</head>
+
<body>
+
<div id="map" style="width:100%; height:100%; position: absolute;"></div>
+
 
+
<script type="text/javascript">
+
 
 
/////////////////////////////////////////////////////////////////////////////////////
+
//Addresses to be displayed
// Don't forget to set your API credentials
+
addresses = ["Helsinki, Finland", "Zaragoza, Spain", "Berlin, Germany",
//
+
"Colorado Springs, USA", "Houston, TX", "London, UK", "Vancouver, Canada",
// Replace with your appId and token which you can obtain when you
+
"Hyderabad, India", "Beijing, China", "Ilomantsi, Finland" ];
// register on http://api.developer.nokia.com/
+
var  i = addresses.length; 
//
+
nokia.Settings.set( "appId", "YOUR APP ID GOES HERE");
+
nokia.Settings.set( "authenticationToken", "YOUR AUTHENTICATION TOKEN GOES HERE");
+
//
+
/////////////////////////////////////////////////////////////////////////////////////
+
 
 
//initialize a new map
+
// iterate over all addresses, create a manager for each of them,
var display = new nokia.maps.map.Display(document.getElementById("map"),
+
// add the observer and call the geocode method
                    {    "components": [
+
                                    new nokia.maps.map.component.ZoomBar(),               
+
while(i--) {     
                                    new nokia.maps.map.component.Behavior(),               
+
nokia.places.search.manager.geoCode({
                                    new nokia.maps.map.component.TypeSelector()],    
+
searchTerm :addresses[i],
                                    "zoomLevel": 10 });
+
onCompleteonSearchComplete
                                                                                                                 
+
});
// we will put our address markers into this container   
+
}
var addressContainer = new nokia.maps.map.Container();   
+
}
+
</code>
// this function will be used when all managers have returned   
+
<code javascript>
var onAllManagersFinished = function() {   
+
function onSearchComplete(data, requestStatus) {
   
+
if (requestStatus == "OK") {               
//we get the bounding box of the container       
+
// if we are finished, we add a marker for the mapped position             
var bbox = addressContainer.getBoundingBox();       
+
addressContainer.objects.add(
+
new nokia.maps.map.StandardMarker(data.location.position));             
// if the bounding box is null then there are no objects inside       
+
//increment the counter to notify another manager has finished             
// 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 observer 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++;         
 
managersFinished++;         
}         
+
} else if(requestStatus === "ERROR") {             
+
// we'll also increment in case of an error           
// if all managers are finished, we call the final function         
+
managersFinished++;
if(managersFinished === addresses.length) {
+
}         
onAllManagersFinished();  
+
}     
+
// if all managers are finished, we call the final function         
};
+
if(managersFinished === addresses.length) {
 +
onAllManagersFinished();  
 +
}     
 +
}  
 +
</code>
  
//Addresses to be displayed
+
<code javascript>
var addresses = ["Helsinki, Finland", "Zaragoza, Spain", "Berlin, Germany", "Colorado Springs, USA", "Houston, TX",
+
function onAllManagersFinished() {   
"London, UK", "Vancouver, Canada", "Hyderabad, India", "Beijing, China", "Ilomantsi, Finland" ];
+
//we get the bounding box of the container       
var  i = addresses.length;
+
var bbox = addressContainer.getBoundingBox();        
var    managersFinished = 0;
+
+
// iterate over all addresses, create a manager for each of them,
+
// add the observer and call the geocode method
+
  
while(i--) {   
+
// if the bounding box is null then there are no objects inside       
nokia.places.search.manager.geoCode({
+
// meaning no markers have been added to it       
              searchTerm :addresses[i],
+
      onComplete:  onSearchComplete
+
});
+
}
+
  
</script>
+
if (bbox != null) {           
</body>
+
// we have at least one address mapped           
</html>
+
// so we add the container and zoomTo it           
 +
map.objects.add(addressContainer);           
 +
map.zoomTo(bbox, true);       
 +
} else {       
 +
// otherwise we'll pop up an error message             
 +
alert("There are no addresses to show :(");       
 +
}   
 +
}
 
</code>
 
</code>
  
== Screenshot ==
 
The following screenshot shows the resultant map after making the search.
 
  
 
[[File:concurrent-search.png]]
 
[[File:concurrent-search.png]]
 +
 +
A fully working example can be found at:
 +
 +
http://rawgithub.com/heremaps/examples/master/maps_api_for_javascript/demos/concurrent-search-requests/index.html

Latest revision as of 14:46, 28 November 2013

Article Metadata
Code ExampleTested with
Devices(s): Internet Explorer, Google Chrome , Firefox, Opera
Compatibility
Platform(s): Web
Dependencies: HERE Maps API 2.5.3
Article
Keywords: HERE Maps, JavaScript, Search
Created: Maveric (25 Oct 2011)
Last edited: jasfox (28 Nov 2013)

Contents

[edit] Introduction

In this article we will examine the following usage of the HERE 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.

[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

function concurrentSearch(map){
// we will put our address markers into this container
addressContainer = new nokia.maps.map.Container();
managersFinished = 0;
 
//Addresses to be displayed
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;
 
// 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
});
}
}
function onSearchComplete(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();
}
}
function onAllManagersFinished() {     
//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
map.objects.add(addressContainer);
map.zoomTo(bbox, true);
} else {
// otherwise we'll pop up an error message
alert("There are no addresses to show :(");
}
}


Concurrent-search.png

A fully working example can be found at:

http://rawgithub.com/heremaps/examples/master/maps_api_for_javascript/demos/concurrent-search-requests/index.html

This page was last modified on 28 November 2013, at 14:46.
362 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.

×