×
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 - Add link.)
jasfox (Talk | contribs)
m (Jasfox - Nokia -> HERE)
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Code Snippet]][[Category:Nokia Maps]][[Category:JavaScript]]
+
[[Category:Code Snippet]][[Category:HERE Maps]][[Category:JavaScript]]
 
{{ArticleMetaData <!-- v1.1 -->
 
{{ArticleMetaData <!-- v1.1 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|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]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= Google Chrome 14.0.835.202 m, Mozilla Firefox 8.0
+
|devices= Internet Explorer, Google Chrome , Firefox, Opera
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
+
|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.0.0
+
|dependencies=  HERE Maps 2.2.4
 
|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= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
+
|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://api.maps.nokia.com/ Nokia Maps API]  
+
* [http://developer.here.net/javascript_api HERE Maps API]  
* [http://api.maps.nokia.com/2.1.1/playground/?example&#61;search Search example]
+
* [http://developer.here.net/apiexplorer/examples/api-for-js/places-search/map-with-search-result-addresses.html Search example]
 
  }}
 
  }}
  
 
==Introduction==   
 
==Introduction==   
  
In this article we will examine the following use case on 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 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.  
# Finally the ZoomLevel needs to be set to cover all of the Markers.   
+
# Finally the {{Icode|ZoomLevel}} needs to be set to cover all of the {{Icode|Markers}}.   
  
This article enforces the correct use of the search.Manager in case of multiple sequential requests.
+
This article enforces the correct use of the search.Manager in case of multiple concurrent requests.
  
 
==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 about Maps credentials==
+
==Important note about maps credentials==
  
With Nokia Maps API you can start without having any credentials given, but you might face a performance gap.
+
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
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.   
+
[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]
 
+
For more information on how to obtain the credentials, please start with the Nokia Maps API Developers Guide section titled "Acquiring API credentials".
+
  
 
==Implementation==   
 
==Implementation==   
Line 53: Line 51:
 
This is a fully working example code in HTML and JavaScript.
 
This is a fully working example code in HTML and JavaScript.
  
<code java>   
+
<code javascript>   
 
+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
 
<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.2.4/jsl.js"></script>
 +
<title>Concurrent Search example</title>
 
</head>  
 
</head>  
 
<body>  
 
<body>  
<div id="map" style="width:100%; height:100%;"></div>  
+
<div id="map" style="width:100%; height:100%; position: absolute;"></div>  
</body>
+
 
 
<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");
 +
//
 +
/////////////////////////////////////////////////////////////////////////////////////
 +
 
//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"),  
Line 69: Line 80:
 
                                     new nokia.maps.map.component.Behavior(),                 
 
                                     new nokia.maps.map.component.Behavior(),                 
 
                                     new nokia.maps.map.component.TypeSelector()],     
 
                                     new nokia.maps.map.component.TypeSelector()],     
                                     "zoomLevel": 10 });    
+
                                     "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 markers into this container     
addressContainer = new nokia.maps.map.Container(),      
+
var addressContainer = new nokia.maps.map.Container();      
 
+
 
// this function will be used when all managers have returned     
 
// this function will be used when all managers have returned     
onAllManagersFinished = function() {     
+
var onAllManagersFinished = function() {     
 
+
//we get the bounding box of the container         
+
//we get the bounding box of the container         
var bbox = addressContainer.getBoundingBox();         
+
var bbox = addressContainer.getBoundingBox();         
 
+
// if the bounding box is null then there are no objects inside         
+
// if the bounding box is null then there are no objects inside         
// meaning no markers have been added to it         
+
// meaning no markers have been added to it         
 
+
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             
 
+
display.objects.add(addressContainer);             
+
display.objects.add(addressContainer);             
display.zoomTo(bbox);         
+
display.zoomTo(bbox, true);         
} else {         
+
} else {         
 
+
// otherwise we'll pop up an error message            
+
// otherwise we'll pop up an error message            
 
+
alert("There are no addresses to show :(");          
alert("There are no addresses to show :(")         
+
}     
}     
+
};   
},   
+
// we will use the same state observer function for all managers     
// we will use the same state obeserver function for all managers     
+
 
+
var onSearchComplete = function (data, requestStatus) {
onManagerStateChange = function(manager, propertyName, value)  
+
  if (requestStatus == "OK") {            
{        if(value === "finished")  
+
    // if we are finished, we add a marker for the mapped position             
  {            
+
    addressContainer.objects.add(new nokia.maps.map.StandardMarker(data.location.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++;         
    //increment the counter to notify another manager has finished             
+
} else if(requestStatus === "ERROR") {            
    managersFinished++;         
+
} else if(value === "failed")  
+
// we'll also increment in case of an error             
{            
+
 
+
managersFinished++;         
// 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();  
// if all managers are finished, we call the final function        
+
}     
 
+
};
if(managersFinished === addresses.length) { onAllManagersFinished(); }     
+
},    manager,    managersFinished = 0;  
+
  
 +
//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,  
 
// 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--)  
+
while(i--) {     
{    manager = new nokia.maps.search.Manager();   
+
nokia.places.search.manager.geoCode({
      manager.addObserver("state", onManagerStateChange);   
+
              searchTerm :addresses[i],
      manager.geocode(addresses[i]);
+
      onComplete:  onSearchComplete
 +
});
 
}  
 
}  
 +
 
</script>  
 
</script>  
 
</body>  
 
</body>  
 
</html>
 
</html>
 
 
</code>
 
</code>
  
==Test this example online==
+
== Screenshot ==
 
+
The following screenshot shows the resultant map after making the search.  
http://mapswidgets.mobi/Performing_multiple_consequent_Search_requests.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
+
[[File:concurrent-search.png]]

Revision as of 19:14, 24 April 2013

Article Metadata
Tested with
Devices(s): Internet Explorer, Google Chrome , Firefox, Opera
Compatibility
Platform(s): Web
Dependencies: HERE Maps 2.2.4
Article
Keywords: HERE Maps, JavaScript, Search
Created: Maveric (25 Oct 2011)
Last edited: jasfox (24 Apr 2013)

Contents

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.

Prerequisites

HERE 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

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.4/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 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++;
}
 
// 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

317 page views in the last 30 days.
×