×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Zooming to all Markers

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - Update links.)
jasfox (Talk | contribs)
m (Jasfox - add monospacing.)
Line 27: Line 27:
 
{{SeeAlso|
 
{{SeeAlso|
 
*  [http://api.maps.nokia.com/ Nokia Maps API]  
 
*  [http://api.maps.nokia.com/ Nokia Maps API]  
 +
* [[Nokia Maps API - Add Maps To Any Web Page|Add Maps To Any Web Page]]
 
* [http://api.maps.nokia.com/en/playground/examples/maps/map_objects/zoomtostandardmarkers.html Zoom to Markers Example]
 
* [http://api.maps.nokia.com/en/playground/examples/maps/map_objects/zoomtostandardmarkers.html Zoom to Markers Example]
 
  }}
 
  }}
Line 38: Line 39:
 
Nokia 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 Nokia Maps to your web page as explained in the previous article "Nokia 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 note about maps credentials==
 
==Important note about maps credentials==
Line 47: Line 48:
 
==Implementation==
 
==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:
+
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:
  
 
'''What is a Bounding box?'''
 
'''What is a Bounding box?'''
Line 58: Line 59:
 
'''Creating the map Markers'''
 
'''Creating the map Markers'''
  
Let's first create three map Markers. Each marker will have a geocoordinate of it's own and they are type of Standard markers by default.
+
Let's first create three map Markers. Each marker will have a {{Icode|Geocoordinate}} of it's own and they are type of {{Icode|StandardMarker}} by default.
  
 
<code java>
 
<code java>
Line 68: Line 69:
 
'''Creating the map Container and add the Markers into it'''
 
'''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 object inside the Container:
+
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 object inside the {{Icode|Container}}:
  
 
<code java>
 
<code java>
Line 82: Line 83:
 
'''Zooming to the Bounding box covering all Markers in the Container:'''
 
'''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 "getBoundingBox" method on the "myContainer".  
+
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 java>
 
<code java>
Line 90: Line 91:
 
</code>
 
</code>
  
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)
+
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}})
 
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.  
  

Revision as of 10:52, 17 April 2012

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.0
Article
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (17 Apr 2012)

Contents

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

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

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 it's own and they are type of StandardMarker by default.

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

<!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.0/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 Nokia Maps API

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

You may also access the interactive Nokia Maps API playground,

395 page views in the last 30 days.
×