×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Zooming to all Markers

From Nokia Developer Wiki
Jump to: navigation, search
Maveric (Talk | contribs)
m (Maveric - - Implementation)
Maveric (Talk | contribs)
(Maveric - - Implementation)
Line 18: Line 18:
 
For more information on how to obtain the credentials, please start with the  Ovi Maps API Developers Guide section "Acquiring API credentials"
 
For more information on how to obtain the credentials, please start with the  Ovi Maps API Developers Guide section "Acquiring API credentials"
  
 +
[[Category:Ovi]][[Category:Web]][[Category:Browser]]
 
[[Category:Ovi]][[Category:Web]][[Category:Browser]]
 
[[Category:Ovi]][[Category:Web]][[Category:Browser]]
 
==Implementation==
 
==Implementation==
  
For this, we would need to use the concept of BoundingBox. It will later contain the Marker that we will create.
+
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:
  
'''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  
Line 30: Line 31:
 
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'''
 +
 
 +
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.
  
 
<code java>
 
<code java>
    myMarkerOne  = new ovi.mapsapi.map.Marker([22.1,10.1]);
+
myMarkerOne  = new ovi.mapsapi.map.Marker([22.1,10.1]);
    myMarkerTwo  = new ovi.mapsapi.map.Marker([53.1,63.1]);
+
myMarkerTwo  = new ovi.mapsapi.map.Marker([53.1,63.1]);
    myMarkerThree = new ovi.mapsapi.map.Marker([22.1,13.1]);
+
myMarkerThree = new ovi.mapsapi.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'''
 +
 
 +
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:
  
 
<code java>
 
<code java>
Line 49: Line 54:
 
map.objects.add(myContainer)
 
map.objects.add(myContainer)
 
</code>
 
</code>
'''
+
 
Zoom 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".
  
 
<code java>
 
<code java>
Line 57: Line 64:
  
 
</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 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==
 +
 +
<code java>
 +
 +
<html>
 +
<head>
 +
<title>Marker on the Map</title>
 +
<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()
 +
 +
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>
 +
 +
 +
</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==
 +
 +
Google Chrome 11.0x
 +
 +
Mozilla Firefox 5.0´
  
 
==Example code==
 
==Example code==

Revision as of 14:05, 21 June 2011

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

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

Important 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 offering, you must get the credentials that authenticate your application against the Services. Please read through the Location API.

For more information on how to obtain the credentials, please start with the Ovi Maps API Developers Guide section "Acquiring API credentials"

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 Standard markers by default.

myMarkerOne  = new ovi.mapsapi.map.Marker([22.1,10.1]);
myMarkerTwo = new ovi.mapsapi.map.Marker([53.1,63.1]);
myMarkerThree = new ovi.mapsapi.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 ovi.mapsapi.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 "getBoundingBox" method on the "myContainer".

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

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.

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

Example code

<html>
<head>
<title>Marker on the Map</title>
<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()
 
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>

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

Google Chrome 11.0x

Mozilla Firefox 5.0´

Example code

Here is a full HTML & JavaScript implementation.

<html>
<head>
<title>Marker on the Map</title>
<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()
 
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>

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

Google Chrome 11.0x

Mozilla Firefox 5.0b

163 page views in the last 30 days.
×