×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Zooming to all Markers

From Nokia Developer Wiki
Jump to: navigation, search
Maveric (Talk | contribs)
(Maveric -)
 
jasfox (Talk | contribs)
m (Jasfox - - Example code)
(23 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:Ovi]][[Category:Web]][[Category:Browser]]
+
[[Category:Nokia Maps]][[Category:Code Snippet]][[Category:JavaScript]]
 +
{{Abstract|This article shows how to show a map and zoom to several markers with Nokia Maps.}}
 +
{{ArticleMetaData
 +
|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]]) -->
 +
|devices= Internet Explorer, Firefox, Google Chrome, Opera
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= Web browser
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|dependencies=Nokia Maps 2.2.3
 +
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= Nokia Maps, JavaScript, Zoom, Markers
 +
|id= <!-- Article Id (Knowledge base articles only) -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by=<!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by=[[User:avnee.nathani]]
 +
|update-timestamp=20111231
 +
|creationdate=20110621
 +
|author=[[User:Maveric]]
 +
}}
 +
 
 +
{{SeeAlso|
 +
* [http://developer.here.net/javascript_api Nokia Maps API]
 +
* [[Nokia Maps API - Add Maps To Any Web Page|Add Maps To Any Web Page]]
 +
* [http://developer.here.net/apiexplorer/examples/api-for-js/markers/zoom-to-set-of-markers.html Zoom to Markers Example]
 +
}}
 
==Introduction==
 
==Introduction==
  
An important use case would be when you have more than one Marker on the map and in a container, then would like to display all of the Markers for that  
+
An important case that necessitates zooming to all markers is when you have more than one Marker on the map and in a container, then would like to display all of the Markers for that  
container visible on the screen at the same time. To achieve this, you would need to know what is the correct zoom level so that all of the Markers become visible.  
+
container visible on the screen at the same time. To achieve this, you would need to know what the correct zoom level is such that all the Markers become visible.  
  
 
==Prerequisites==
 
==Prerequisites==
  
Ovi Maps API supported web browser (basically any modern web browser)
+
Nokia Maps API supported web browser (basically any modern web browser)
  
The example assumes you have already added the Ovi Maps to your web page as explained in the previous article "Ovi Maps - add the map to any web page"
+
The example assumes you have already added the Nokia Maps to your web page as explained in the previous article [[Nokia Maps API - Add Maps To Any Web Page|Add Maps To Any Web Page]]
  
==Important about Maps credentials==
+
==Important note about maps credentials==
  
With Ovi 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
+
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
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  Ovi Maps API Developers Guide section "Acquiring API credentials"
+
  
 
==Implementation==
 
==Implementation==
  
For this, we would need to use the concept of BoundingBox. Here we will define a new bounding box, which will contain the Marker that we will create.
+
We will need the concept of {{Icode|BoundingBox}} when we define the area that will later cover the map markers. Here is the definition of it:
  
'''Bounding box'''
+
'''What is a Bounding box?'''
  
A bounding box defines a rectangular area in a geographic coordinate system. As the bounding box is specified by its top-left and bottom-right corner  
+
A bounding box defines a rectangular area in a geographic coordinate system. As the bounding box is specified by its top-left and bottom-right corner,
 
the box is not necessarily the smallest rectangle spanned by these two points; it is possible to define bounding boxes that are wider than 180°  
 
the box is not necessarily the smallest rectangle spanned by these two points; it is possible to define bounding boxes that are wider than 180°  
 
or higher than 90° (i.e., by setting the longitude of top-left corner to a bigger value than the longitude of the bottom-right corner).  
 
or higher than 90° (i.e., by setting the longitude of top-left corner to a bigger value than the longitude of the bottom-right corner).  
 
Bounding box class is immutable.
 
Bounding box class is immutable.
  
First we need to '''implement a new map Container''', then we will create three Markers, which will then be inserted into the Container.
+
'''Creating the map Markers'''
  
<code java>
+
Let's first create three map Markers. Each marker will have a {{Icode|Geocoordinate}} of its own, and each is by default a {{Icode|StandardMarker}}.
    myMarkerOne  = new ovi.mapsapi.map.Marker([22.1,10.1]);
+
 
    myMarkerTwo  = new ovi.mapsapi.map.Marker([53.1,63.1]);
+
<code javascript>
    myMarkerThree = new ovi.mapsapi.map.Marker([22.1,13.1]);
+
myMarkerOne  = new nokia.maps.map.Marker([22.1,10.1]);
 +
myMarkerTwo  = new nokia.maps.map.Marker([53.1,63.1]);
 +
myMarkerThree = new nokia.maps.map.Marker([22.1,13.1]);
 
</code>
 
</code>
  
'''Create a map Container and add the Markers into it'''
+
'''Creating the map Container and add the Markers into it'''
  
<code java>
+
To carry the map {{Icode|Marker}}s, we will also need to define a new {{Icode|Container}}. We will use the {{Icode|add()}} method to put each of the marker objects inside the {{Icode|Container}}:
var myContainer = new ovi.mapsapi.map.Container()
+
 
 +
<code javascript>
 +
var myContainer = new nokia.maps.map.Container()
  
 
myContainer.objects.add(myMarkerOne);
 
myContainer.objects.add(myMarkerOne);
Line 48: Line 80:
 
map.objects.add(myContainer)
 
map.objects.add(myContainer)
 
</code>
 
</code>
'''
 
Zoom to the Bounding box covering all Markers in the Container:'''
 
  
<code java>
+
'''Zooming to the Bounding box covering all Markers in the Container:'''
 +
 
 +
Finally we will set the map {{Icode|ZoomLevel}} to cover all the {{Icode|Marker}} coordinates. This is achieved by calling the {{Icode|zoomTo()}} method on the map, to which we will give as parameter the object that will be received by calling the {{Icode|getBoundingBox()}} method on {{Icode|myContainer}}.
 +
 
 +
<code javascript>
  
 
     map.zoomTo(myContainer.getBoundingBox(),false,true);     
 
     map.zoomTo(myContainer.getBoundingBox(),false,true);     
Line 57: Line 91:
 
</code>
 
</code>
  
 
+
After this defined {{Icode|BoundingBox}} is available to be used by the  {{Icode|zoomTo()}} function, which will zoom the map to the given bounding box. If the {{Icode|keepCenter}} flag (default is {{Icode|false}})
After this definition the BoundingBox is available for zoomTo function, which will zoom the map to the given bounding box. If the keepCenter flag (default is false)
+
 
is used, the size of the viewport will be increased such that the original center is still located in the center of the viewport.  
 
is used, the size of the viewport will be increased such that the original center is still located in the center of the viewport.  
 
  
 
Note: This operation may use a platform specific animation if this is indicated by the corresponding optional animation string.
 
Note: This operation may use a platform specific animation if this is indicated by the corresponding optional animation string.
  
 
==Example code==
 
==Example code==
 +
This example zooms out to fit all markers onto the map. Remember to add in your own [[How to Obtain Your Own Nokia appID and Token| AppId and Token]].
 +
<code javascript>
 +
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 +
<html xmlns="http://www.w3.org/1999/xhtml">
 +
    <head>
 +
      <title>Zoom to all markers</title>
 +
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 +
      <script type="text/javascript"
 +
            src="http://api.maps.nokia.com/2.2.3/jsl.js" charset="utf-8">
 +
        </script>
 +
    </head>
 +
    <body>
  
Here is a full HTML & JavaScript implementation.
+
        <div id="mapContainer" style="z-index: -1; left:0px; top:0px; width: 100%; height: 80%; 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");
  
<code java>
+
//
 
+
/////////////////////////////////////////////////////////////////////////////////////
<html>
+
var map = new nokia.maps.map.Display(document.getElementById("mapContainer"), {
<head>
+
zoomLevel: 8,
<title>Marker on the Map</title>
+
center: [19.119, 72.8957]
<script src="http://api.maps.ovi.com/jsl.js" type="text/javascript" charset="utf-8"></script>
+
});
</head>
+
<body>
+
<div id="map" style="z-index: -1; left:0px; top:0px; width: 100%; height: 100%; position: absolute;"></div>
+
<script type="text/javascript">
+
 
+
// Set the authentication token, which is needed by the web-services to authenticate your application.
+
ovi.mapsapi.util.ApplicationContext.set("authenticationToken", "<Token>");
+
 
+
var map = new ovi.mapsapi.map.Display(document.getElementById("map"), {'zoomLevel':0, 'center':[53.1, 15.1]});
+
 
+
map.set("baseMapType", map.NORMAL);
+
 
+
myMarkerOne  = new ovi.mapsapi.map.Marker([51.1,15.1]);
+
myMarkerTwo  = new ovi.mapsapi.map.Marker([52.1,14.1]);
+
myMarkerThree = new ovi.mapsapi.map.Marker([53.1,13.1]);
+
 
+
var myContainer = new ovi.mapsapi.map.Container()
+
  
 +
myMarkerOne  = new nokia.maps.map.Marker([19.119, 72.8957]);
 +
myMarkerTwo  = new nokia.maps.map.Marker([19.119, 73.8957]);
 +
myMarkerThree = new nokia.maps.map.Marker([19.119, 74.8957]);
 +
 +
var myContainer = new nokia.maps.map.Container()
 +
 
myContainer.objects.add(myMarkerOne);
 
myContainer.objects.add(myMarkerOne);
 
myContainer.objects.add(myMarkerTwo);
 
myContainer.objects.add(myMarkerTwo);
 
myContainer.objects.add(myMarkerThree);
 
myContainer.objects.add(myMarkerThree);
 
+
 
map.objects.add(myContainer)
 
map.objects.add(myContainer)
 
+
 
map.zoomTo(myContainer.getBoundingBox(),false,true);     
 
map.zoomTo(myContainer.getBoundingBox(),false,true);     
  
 
</script>
 
</script>
 
</body>
 
</body>
</html>
+
</html></code>
</code>
+
 
+
==For more on Ovi Maps API==
+
 
+
Please check out the Ovi Maps API full documentation and API reference here:
+
 
+
http://api.maps.ovi.com
+
  
==Tested on==
+
==Screenshot==
 +
[[File:NokiaMapsZoomtoMultipleMarkers.png]]
 +
==For more on the Nokia Maps API==
  
Google Chrome 11.0x
+
Please check out the Nokia Maps API full documentation and API reference here:
 +
* [http://developer.here.net/javascript_api Nokia Maps API]
  
Mozilla Firefox 5.0b
+
You may also access the interactive API explorer
 +
* [http://developer.here.net/javascript_api_explorer API explorer]

Revision as of 14:31, 4 January 2013

This article shows how to show a map and zoom to several markers with Nokia Maps.

Article Metadata
Tested with
Devices(s): Internet Explorer, Firefox, Google Chrome, Opera
Compatibility
Platform(s): Web browser
Dependencies: Nokia Maps 2.2.3
Article
Keywords: Nokia Maps, JavaScript, Zoom, Markers
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (04 Jan 2013)

Contents

Introduction

An important case that necessitates zooming to all markers is when you have more than one Marker on the map and in a container, then would like to display all of the Markers for that container visible on the screen at the same time. To achieve this, you would need to know what the correct zoom level is such that all the Markers become visible.

Prerequisites

Nokia Maps API supported web browser (basically any modern web browser)

The example assumes you have already added the Nokia Maps to your web page as explained in the previous article Add Maps To Any Web Page

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

We will need the concept of BoundingBox when we define the area that will later cover the map markers. Here is the definition of it:

What is a Bounding box?

A bounding box defines a rectangular area in a geographic coordinate system. As the bounding box is specified by its top-left and bottom-right corner, the box is not necessarily the smallest rectangle spanned by these two points; it is possible to define bounding boxes that are wider than 180° or higher than 90° (i.e., by setting the longitude of top-left corner to a bigger value than the longitude of the bottom-right corner). Bounding box class is immutable.

Creating the map Markers

Let's first create three map Markers. Each marker will have a Geocoordinate of its own, and each is by default a StandardMarker.

myMarkerOne  = new nokia.maps.map.Marker([22.1,10.1]);
myMarkerTwo = new nokia.maps.map.Marker([53.1,63.1]);
myMarkerThree = new nokia.maps.map.Marker([22.1,13.1]);

Creating the map Container and add the Markers into it

To carry the map Markers, we will also need to define a new Container. We will use the add() method to put each of the marker objects inside the Container:

var myContainer = new nokia.maps.map.Container()
 
myContainer.objects.add(myMarkerOne);
myContainer.objects.add(myMarkerTwo);
myContainer.objects.add(myMarkerThree);
 
map.objects.add(myContainer)

Zooming to the Bounding box covering all Markers in the Container:

Finally we will set the map ZoomLevel to cover all the Marker coordinates. This is achieved by calling the zoomTo() method on the map, to which we will give as parameter the object that will be received by calling the getBoundingBox() method on myContainer.

    map.zoomTo(myContainer.getBoundingBox(),false,true);

After this defined BoundingBox is available to be used by the zoomTo() function, which will zoom the map to the given bounding box. If the keepCenter flag (default is false) is used, the size of the viewport will be increased such that the original center is still located in the center of the viewport.

Note: This operation may use a platform specific animation if this is indicated by the corresponding optional animation string.

Example code

This example zooms out to fit all markers onto the map. Remember to add in your own AppId and Token.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Zoom to all markers</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript"
src="http://api.maps.nokia.com/2.2.3/jsl.js" charset="utf-8">
</script>
</head>
<body>
 
<div id="mapContainer" style="z-index: -1; left:0px; top:0px; width: 100%; height: 80%; 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");
 
//
/////////////////////////////////////////////////////////////////////////////////////
var map = new nokia.maps.map.Display(document.getElementById("mapContainer"), {
zoomLevel: 8,
center: [19.119, 72.8957]
});
 
myMarkerOne = new nokia.maps.map.Marker([19.119, 72.8957]);
myMarkerTwo = new nokia.maps.map.Marker([19.119, 73.8957]);
myMarkerThree = new nokia.maps.map.Marker([19.119, 74.8957]);
 
var myContainer = new nokia.maps.map.Container()
 
myContainer.objects.add(myMarkerOne);
myContainer.objects.add(myMarkerTwo);
myContainer.objects.add(myMarkerThree);
 
map.objects.add(myContainer)
 
map.zoomTo(myContainer.getBoundingBox(),false,true);
 
</script>
</body>
</html>

Screenshot

NokiaMapsZoomtoMultipleMarkers.png

For more on the Nokia Maps API

Please check out the Nokia Maps API full documentation and API reference here:

You may also access the interactive API explorer

312 page views in the last 30 days.