×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - How to change map zoom level and center point

From Nokia Developer Wiki
Jump to: navigation, search
Maveric (Talk | contribs)
(Maveric - - Map center)
jasfox (Talk | contribs)
m (Jasfox - - Example code)
(31 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]]
==Introduction==
+
{{Abstract|This article explains how to change map zoom level and map center. }}
  
In this article we will introduce the map zoom level and map center.
+
{{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, Google Chrome, Firefox, 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, Component, Components, center, centre
 +
|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]]
 +
}}
  
==Prerequisites==
 
  
Ovi Maps API supported web browser (basically any modern web browser)
+
{{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]]
 +
* [[Nokia Maps API - Geocoordinate system|The Geocoordinate system]]
 +
* [http://developer.here.net/apiexplorer/examples/api-for-js/map/zooming-the-map.html Zooming the Map]
 +
* [http://developer.here.net/apiexplorer/examples/api-for-js/map/panning-the-map.html Panning the Map]
 +
}}
  
The example assumes you have already added the Ovi Maps to your web page as explained in the previous article "Ovi Maps - Add Maps To Any Web Page"
+
==Prerequisites==
  
==Important about Maps credentials==
+
Nokia Maps API supported web browser (basically any modern web browser)
  
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
+
==Important note about maps credentials==
offering, you must get the credentials that authenticate your application against the Services. Please read through the Location API.
+
  
Note: At the time of writing, it is not necessary to include the token. Please follow the Announcement section for Ovi Maps at Nokia Projects.  
+
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
It will be updated when the authentication is enabled on the backend servers.
+
[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]
  
 
==Implementation==
 
==Implementation==
  
You can change the parameters for the map view by passing them to the previously instantiated Display (handled in the article "Ovi Maps - Add Maps To Any Web Page") that define the zoom level and center point for the map can be set to suite your purposes. The map will default in our example to Berlin, Germany but you can ofcourse change this the center to point to any other location on this planet.
+
You can change the parameters for the map view by passing them to your map instance (handled in the article "[[Nokia Maps API - Add Maps To Any Web Page|Add Maps To Any Web Page]]") that define the zoom level and center point for the map can be set to suit your purposes. The map will default in our example to Berlin, Germany but you can of course change the center to point to any other location on this planet.
  
[[Category:Ovi]][[Category:Web]][[Category:Browser]]
+
==Map centre==
==Map center==
+
  
The map center is defined by the center property and must be provided in type of ovi.mapsapi.geo.ICoordinate.
+
The centre of the map is defined by the {{Icode|center}} property and is defined by a {{Icode| nokia.maps.geo.Coordinate}}
  
 
<code java>
 
<code java>
Line 32: Line 57:
 
The coordinate represents a geographical position. As most of the use cases currently do not consider height information,  
 
The coordinate represents a geographical position. As most of the use cases currently do not consider height information,  
 
the altitude is defined as optional. If a corresponding type is already provided by the platform the product must not implement this class.
 
the altitude is defined as optional. If a corresponding type is already provided by the platform the product must not implement this class.
new ovi.mapsapi.geo.Coordinate(lat, lng, [alt, [skipValidation]])
+
new nokia.maps.geo.Coordinate(lat, lng, [alt, [skipValidation]])
  
 
Parameters:
 
Parameters:
{Number}    lat    Latitude in degrees. -90 <= latitude <= 90. Values outside of range throw IllegalArgument error.
+
{Number}    lat    //Latitude in degrees. -90 <= latitude <= 90. Values outside of range throw an {{Icode| IllegalArgument}} error.
{Number}    lng    Longitude in degrees. -180 <= longitude <= 180. Values outside of range throw IllegalArgument error.
+
{Number}    lng    // Longitude in degrees. -180 <= longitude <= 180. Values outside of range throw an {{Icode| IllegalArgument}} error.
{Number}    [alt]: undefined    Altitude in meters.
+
{Number}    [alt]: undefined    // Altitude in meters.
{Boolean}    [skipValidation]: false    If being true, then the validation of the latitude, longitude and altitude values is skipped.  
+
{Boolean}    [skipValidation]: false    //If true, the validation of the latitude, longitude and altitude values is skipped.  
 
</code>
 
</code>
  
We will create a new Coordinate, but will leave out altitude and want the values to be validated, so we will leave the skipValidation
+
We will create a new {{Icode|Coordinate}}, but will leave out altitude, and we want the values to be validated, so we will leave the {{Icode|skipValidation}}
 
to it's default value.
 
to it's default value.
  
  
<code java>
+
<code javascript>
new ovi.mapsapi.geo.Coordinate (lat, lng, [alt, [skipValidation]])   
+
new nokia.maps.geo.Coordinate (lat, lng, [alt, [skipValidation]])   
 
</code>
 
</code>
  
 
The coordinate represents a geographical position. Altitude is defined as optional. (For more details on this see the Maps API reference).
 
The coordinate represents a geographical position. Altitude is defined as optional. (For more details on this see the Maps API reference).
  
Note: There is an article about the coordinate system and geocoordinates titled: "Ovi Maps API - Geocoordinates". You may want to check it out for more detailed information on the geocoordinate system used in Ovi Maps and some generic information about the world coordinate system.
+
Note: There is an article about the   [[Nokia Maps API - Geocoordinate system|The coordinate system and geocoordinates]]. You may want to check it out for more detailed information on the geocoordinate system used in Nokia Maps and some generic information about the world coordinate system.
 +
 
 +
In our example we will use the latitude and longitude values for the city of D&uuml;sseldorf, Germany : 51.233333&deg;N, 6.783333&deg;E  These will be passed in the form {{Icode|[51.233333, 6.783333]}}.
 +
The latitude and longitude can be passed as a floating point number, i.e. {{Icode|13.4}} is just as valid as e.g. {{Icode|13.456789}}
  
In our example we will use the latitude and longitude values for the city of Dusseldorf, Germany : 52.51, 13.4. These will be passed in form [52.51, 13.4].
 
The latitude and longitude can be passed as a floating point number, i.e. 13.4 is just as valid as e.g. 13.456789
 
 
==ZoomLevel==
 
==ZoomLevel==
  
The Ovi Maps ZoomLevel is a number between minimum and maximum values and can be queried with minZoomLevel() and maxZoomLevel() methods.  
+
The Nokia Maps {{Icode|ZoomLevel}} is a number between the minimum and maximum zoom values and can be queried with the {{Icode|minZoomLevel()}} and {{Icode|maxZoomLevel()}} methods.  
  
At the moment, the API offers the scale of zoom between 0 (zero) and 20. This is reflected from the UI component ZoomBar on the Map view. The UI component
+
At the moment, the API offers zoom levels between 0 (zero) and 20. This is reflected by the UI Zoom Bar on the Map view. The zoom levels that the UI Zoom Bar uses are:
used exact zoom levels respectively are:
+
  
Level    Name
+
Level    Name <br />
14        Street   
+
14        Street  <br />
11        City
+
11        City <br />
8          County
+
8          County <br />
3          Country
+
3          Country <br />
  
Example of requesting the min and max zoom levels of the current map:
+
How to request the min and max zoom levels of the current map:
  
 
<code java>
 
<code java>
map.minZoomLevel  <-  Could return example "0" (zero)
+
map.minZoomLevel  // Could return for example "0" (zero)
map.maxZoomLevel  <-  Could return example "20"
+
map.maxZoomLevel  // Could return for example "20"
 
</code>
 
</code>
 
  
 
==Example code==
 
==Example code==
  
We are using the same definition as in the example mentioned in the section "Prerequisites".
+
We are using the same code as in the [[Nokia Maps API - Add Maps To Any Web Page|Add Maps To Any Web Page]] example, simply changing the {{Icode|zoomLevel}} and  {{Icode|center}}. Remember to add in your own [[How to Obtain Your Own Nokia appID and Token| AppId and Token]].
  
 
+
<code javascript>
<code java>
+
<!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>
'zoomLevel': 8,                        // Country level  
+
      <title>Map of D&uuml;sseldorf</title>
'center': [52.51, 13.4]                // New map center point
+
      <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>
 +
        <h1>Map of D&uuml;sseldorf</h1>
 +
        <div id="map" 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("map"),{
 +
'zoomLevel': 8,                        // Country level  
 +
'center': [51.233333, 6.783333]                // New map center point
 +
});  
 +
        </script>
 +
    </body>
 +
</html>
 
</code>
 
</code>
  
When you have defined these, the map should open on Country level of zoom, having the city of Dusseldorf, Germany in the middle of the map view.
+
When you have defined these, the map should open on Country level zoom, with the city of D&uuml;sseldorf, Germany in the middle of the map view.
 
+
 
+
==Test this example live here==
+
 
+
 
+
==For more on Ovi Maps API==
+
 
+
Please check out the Ovi Maps API full documentation and API reference here:
+
 
+
[link]http://api.maps.ovi.com[/link]
+
 
+
==Tested on==
+
 
+
Google Chrome 11.0x
+
Mozilla Firefox 5.0x
+
  
 
==Zoom level==
 
==Zoom level==
  
[b]zoomLevel[/b] The map zoom level is defined by this property  
+
{{Icode|zoomLevel}} -  The map zoom level is defined by this property  
 
    
 
    
The zoomLevel is a number between minimum and maximum values and can be queried with minZoomLevel() and maxZoomLevel() methods.  
+
The {{Icode|zoomLevel}} is a number between minimum and maximum values and can be queried with the {{Icode|minZoomLevel()}} and {{Icode|maxZoomLevel()}} methods.  
  
At the moment, the API offers the scale of zoom between 0 (zero) and 20. This is reflected from the UI component ZoomBar on the Map view. The UI component
+
At the moment, the API offers zoom levels between 0 (zero) and 20. This is reflected by the UI component ZoomBar on the Map view. The zoom levels
used exact zoom levels respectively are:
+
used by the UI map zoom component are:
  
 
<code java>
 
<code java>
Line 125: Line 157:
 
</code>
 
</code>
  
Example of requesting the min and max zoom levels of the current map:
+
How to request the min and max zoom levels of the current map:
  
 
<code java>
 
<code java>
Line 132: Line 164:
 
</code>
 
</code>
  
 +
==For more on the Nokia Maps API==
  
==Example code==
+
Please check out the Nokia Maps API full documentation and API reference here:
 
+
* [http://developer.here.net/javascript_api Nokia Maps API]
This code will add the zoomLevel and center point. Please visit the article "Ovi Maps API - Add Maps To Any Page", there the Example code
+
is divided in two: Example 1 and 2. The code below is what is added to the Example 1, thus making the map to position itself somewhere else than the coordinate 0,0 as is by default and setting the zoom level to be > 0. The default zoom level if not specified, is zero (0).
+
 
+
<code java>
+
...                                                   
+
{
+
'zoomLevel': 8,                        // Country level
+
'center': [52.51, 13.4]                // New map center point
+
}
+
);
+
...
+
</code>
+
 
+
When you have defined these, the map should open on Country level of zoom, having the city of Dusseldorf, Germany in the middle of the map view.
+
 
+
 
+
==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
+
You may also access the interactive API explorer
Mozilla Firefox 5.0
+
* [http://developer.here.net/javascript_api_explorer API explorer]

Revision as of 14:22, 4 January 2013

This article explains how to change map zoom level and map center.

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


Contents

Prerequisites

Nokia 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

You can change the parameters for the map view by passing them to your map instance (handled in the article "Add Maps To Any Web Page") that define the zoom level and center point for the map can be set to suit your purposes. The map will default in our example to Berlin, Germany but you can of course change the center to point to any other location on this planet.

Map centre

The centre of the map is defined by the center property and is defined by a nokia.maps.geo.Coordinate

Constructor detail:
 
The coordinate represents a geographical position. As most of the use cases currently do not consider height information,
the altitude is defined as optional. If a corresponding type is already provided by the platform the product must not implement this class.
new nokia.maps.geo.Coordinate(lat, lng, [alt, [skipValidation]])
 
Parameters:
{Number} lat //Latitude in degrees. -90 <= latitude <= 90. Values outside of range throw an {{Icode| IllegalArgument}} error.
{Number} lng // Longitude in degrees. -180 <= longitude <= 180. Values outside of range throw an {{Icode| IllegalArgument}} error.
{Number} [alt]: undefined // Altitude in meters.
{Boolean} [skipValidation]: false //If true, the validation of the latitude, longitude and altitude values is skipped.

We will create a new Coordinate, but will leave out altitude, and we want the values to be validated, so we will leave the skipValidation to it's default value.


new nokia.maps.geo.Coordinate (lat, lng, [alt, [skipValidation]])

The coordinate represents a geographical position. Altitude is defined as optional. (For more details on this see the Maps API reference).

Note: There is an article about the The coordinate system and geocoordinates. You may want to check it out for more detailed information on the geocoordinate system used in Nokia Maps and some generic information about the world coordinate system.

In our example we will use the latitude and longitude values for the city of Düsseldorf, Germany : 51.233333°N, 6.783333°E These will be passed in the form [51.233333, 6.783333]. The latitude and longitude can be passed as a floating point number, i.e. 13.4 is just as valid as e.g. 13.456789

ZoomLevel

The Nokia Maps ZoomLevel is a number between the minimum and maximum zoom values and can be queried with the minZoomLevel() and maxZoomLevel() methods.

At the moment, the API offers zoom levels between 0 (zero) and 20. This is reflected by the UI Zoom Bar on the Map view. The zoom levels that the UI Zoom Bar uses are:

Level Name
14 Street
11 City
8 County
3 Country

How to request the min and max zoom levels of the current map:

map.minZoomLevel   // Could return for example "0" (zero)
map.maxZoomLevel // Could return for example "20"

Example code

We are using the same code as in the Add Maps To Any Web Page example, simply changing the zoomLevel and center. 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>Map of D&uuml;sseldorf</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>
<h1>Map of D&uuml;sseldorf</h1>
<div id="map" 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("map"),{
'zoomLevel': 8, // Country level
'center': [51.233333, 6.783333] // New map center point
});
</script>
</body>
</html>

When you have defined these, the map should open on Country level zoom, with the city of Düsseldorf, Germany in the middle of the map view.

Zoom level

zoomLevel - The map zoom level is defined by this property

The zoomLevel is a number between minimum and maximum values and can be queried with the minZoomLevel() and maxZoomLevel() methods.

At the moment, the API offers zoom levels between 0 (zero) and 20. This is reflected by the UI component ZoomBar on the Map view. The zoom levels used by the UI map zoom component are:

Level     Name
14 Street
11 City
8 County
3 Country

How to request the min and max zoom levels of the current map:

map.minZoomLevel   <-  Could return example "0" (zero)
map.maxZoomLevel <- Could return example "20"

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

636 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×