HERE Maps API - Zooming to all Markers
This article shows how to show a map and zoom to several markers with HERE Maps.
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.
HERE Maps API supported web browser (basically any modern web browser)
The example assumes you have already added the HERE Maps to your web page as explained in the previous article HERE Maps API - 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
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()
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.
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.
This example zooms out to fit all markers onto the map.
markerOne = new nokia.maps.map.Marker([19.119, 72.8957]);
markerTwo = new nokia.maps.map.Marker([19.119, 73.8957]);
markerThree = new nokia.maps.map.Marker([19.119, 74.8957]);
var container = new nokia.maps.map.Container()
The code behind the working example may be found here:
For more on the HERE Maps API
Please check out the HERE Maps API full documentation and API reference here:
You may also access the interactive API explorer