×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Create map markers from XML data

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Automated change of category from Ovi Maps to Nokia Maps)
avnee.nathani (Talk | contribs)
(Avnee.nathani - Updated article for Nokia Maps Javascript API v2.0. (Released in Dec 2011))
Line 1: Line 1:
 
[[Category:Browser]][[Category:Nokia Maps]][[Category:Location]]
 
[[Category:Browser]][[Category:Nokia Maps]][[Category:Location]]
{{Abstract|This article explains how to read a structured XML file and create map markers based on the data }}  
+
{{Abstract|This article explains how to read a structured XML file and create map markers based on the data.}}
 +
 
 +
{{ArticleMetaData
 +
|platform= Web browser
 +
|update-by=[[User:avnee.nathani]]
 +
|update-timestamp=20111231
 +
}}
  
 
== Introduction ==
 
== Introduction ==
  
Sometimes it would be useful to have the data for map, e.g. for markers, to reside on a separate file. In this article
+
Sometimes it would be useful to have the data for map, e.g. for markers, to reside on a separate file. In this article, an example is presented, using which you can yourself utilize a separate XML file for creating map content. The example will use Javascript and jQuery.  
an example is presented, using which you can yourself utilize a separate XML file for creating map content. The example
+
will use Javascript and jQuery.  
+
  
 
== Prerequisites ==
 
== Prerequisites ==
  
Ovi Maps API supported web browser (basically any modern web browser).
+
* Nokia Maps API supported web browser (basically any modern web browser).
 
+
* JQuery downloaded and installed.  
JQuery downloaded and installed.  
+
* Change the directory names for your own version of the example, if needed.
 
+
Change the directory names for your own version of the example, if needed.
+
 
+
 
+
  
 
== Example code ==
 
== Example code ==
  
Note that the example code leaves you room to implement your own infoBubbles and some other functionality is there if you  
+
Note that the example code leaves you room to implement your own infoBubbles and some other functionality is there if you need it, otherwise you can remove it; for e.g. the behaviour of the map and availability of components. Feel free to copy and modify for your own purposes!
need it, otherwise you can remove it; e.g. the behaviour of the map and availability of components. Feel free to copy and
+
modify for your own purposes!
+
  
 
<code java>
 
<code java>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head>
 
<head>
<title>Ovi Maps API example - Multiple markers created from XML data</title>
+
<title>Nokia Maps API example - Multiple markers created from XML data</title>
 
</head>
 
</head>
 
<p>
 
<p>
 
<body onLoad="init();">
 
<body onLoad="init();">
<p style="text-align: center;">- OVI MAPS API EXAMPLE -</p>
+
<p style="text-align: center;">- Nokia MAPS API EXAMPLE -</p>
 
<p style="text-align: center;">READ DATA OF MULTIPLE MARKERS FROM XML FILE AND PUT THE MARKERS ON THE MAP</p>
 
<p style="text-align: center;">READ DATA OF MULTIPLE MARKERS FROM XML FILE AND PUT THE MARKERS ON THE MAP</p>
 
<center>
 
<center>
Line 37: Line 35:
 
</center>
 
</center>
 
<p>
 
<p>
<script src="http://api.maps.ovi.com/jsl.js" type="text/javascript" charset="utf-8"></script>
+
<script type="text/javascript"
 +
    src="http://api.maps.nokia.com/2.0.0/jsl.js" charset="utf-8">
 +
</script>
 
<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
 
<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
 
<script type="text/javascript" src="js/markers.js"></script>
 
<script type="text/javascript" src="js/markers.js"></script>
Line 55: Line 55:
 
function init(){
 
function init(){
  
// Here we create an Ovi Maps within a Container.
+
// Here we create an Nokia Maps within a Container.
  
 
mapContainer = document.getElementById("map");
 
mapContainer = document.getElementById("map");
  
myInfoBubbles = new ovi.mapsapi.map.component.InfoBubbles();
+
myInfoBubbles = new nokia.maps.map.component.InfoBubbles();
  
myMap = ovi.mapsapi.map,     
+
myMap = nokia.maps.map,     
 
                 map = new myMap.Display(mapContainer, {
 
                 map = new myMap.Display(mapContainer, {
 
                     center: [52.59, 13.3], zoomLevel: 10,
 
                     center: [52.59, 13.3], zoomLevel: 10,
 
                     components: [ new myMap.component.Behavior(),
 
                     components: [ new myMap.component.Behavior(),
                                   new ovi.mapsapi.map.component.ZoomBar(),
+
                                   new nokia.maps.map.component.ZoomBar(),
                                   new ovi.mapsapi.map.component.Overview(),                             
+
                                   new nokia.maps.map.component.Overview(),                             
                                   new ovi.mapsapi.map.component.TypeSelector(),     
+
                                   new nokia.maps.map.component.TypeSelector(),     
                                   new ovi.mapsapi.map.component.ScaleBar(),
+
                                   new nokia.maps.map.component.ScaleBar(),
 
                   myInfoBubbles ]
 
                   myInfoBubbles ]
 
                 });
 
                 });
  
container = new ovi.mapsapi.map.Container();
+
container = new nokia.maps.map.Container();
  
 
// Define the XML filename to read that contains the marker data
 
// Define the XML filename to read that contains the marker data
Line 95: Line 95:
 
         //Put each marker on the map as the data has been read.
 
         //Put each marker on the map as the data has been read.
  
         markerCoords = new ovi.mapsapi.geo.Coordinate(parseFloat(lat), parseFloat(lng));     
+
         markerCoords = new nokia.maps.geo.Coordinate(parseFloat(lat), parseFloat(lng));     
 
    
 
    
             marker = new ovi.mapsapi.map.StandardMarker(markerCoords,
+
             marker = new nokia.maps.map.StandardMarker(markerCoords,
  
 
             {text:name, brush:"#FF69B4", pen:"#09F911", textPen:"#00009C", shape: "star"});
 
             {text:name, brush:"#FF69B4", pen:"#09F911", textPen:"#00009C", shape: "star"});
Line 197: Line 197:
  
 
Microsoft XP Pro and the following browsers:
 
Microsoft XP Pro and the following browsers:
 +
* Opera 5.0.5 & Firefox 5.0
  
Opera 5.0.5 & Firefox 5.0
+
==For more on Nokia Maps API==
 
+
== See the example live here ==
+
  
http://mapswidgets.com/markers.html
+
Please check out the Nokia Maps API full documentation and API reference here:
 +
* http://api.maps.nokia.com/
  
== For more on Ovi Maps API ==
+
You may also access the interactive Nokia Maps API playground,
Please go to http://api.maps.ovi.com
+
* http://api.maps.nokia.com/2.1.0/playground/index.html

Revision as of 15:19, 31 December 2011

This article explains how to read a structured XML file and create map markers based on the data.

Article Metadata
Compatibility
Platform(s): Web browser
Article
Created: ()
Updated: avnee.nathani (31 Dec 2011)
Last edited: avnee.nathani (31 Dec 2011)

Contents

Introduction

Sometimes it would be useful to have the data for map, e.g. for markers, to reside on a separate file. In this article, an example is presented, using which you can yourself utilize a separate XML file for creating map content. The example will use Javascript and jQuery.

Prerequisites

  • Nokia Maps API supported web browser (basically any modern web browser).
  • JQuery downloaded and installed.
  • Change the directory names for your own version of the example, if needed.

Example code

Note that the example code leaves you room to implement your own infoBubbles and some other functionality is there if you need it, otherwise you can remove it; for e.g. the behaviour of the map and availability of components. Feel free to copy and modify for your own purposes!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>Nokia Maps API example - Multiple markers created from XML data</title>
</head>
<p>
<body onLoad="init();">
<p style="text-align: center;">- Nokia MAPS API EXAMPLE -</p>
<p style="text-align: center;">READ DATA OF MULTIPLE MARKERS FROM XML FILE AND PUT THE MARKERS ON THE MAP</p>
<center>
<div id="map" style="width:600px; height:400px; position: relative"></div>
</center>
<p>
<script type="text/javascript"
src="http://api.maps.nokia.com/2.0.0/jsl.js" charset="utf-8">
</script>
<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="js/markers.js"></script>
</p>
<p>&nbsp;</p>
</body>
</html>
 
var markerCoords;
var mapContainer;
var container;
var myMap;
var bubbles=new Array();
 
// When the HTML page body section is loaded this function will be called.
 
function init(){
 
// Here we create an Nokia Maps within a Container.
 
mapContainer = document.getElementById("map");
 
myInfoBubbles = new nokia.maps.map.component.InfoBubbles();
 
myMap = nokia.maps.map,
map = new myMap.Display(mapContainer, {
center: [52.59, 13.3], zoomLevel: 10,
components: [ new myMap.component.Behavior(),
new nokia.maps.map.component.ZoomBar(),
new nokia.maps.map.component.Overview(),
new nokia.maps.map.component.TypeSelector(),
new nokia.maps.map.component.ScaleBar(),
myInfoBubbles ]
});
 
container = new nokia.maps.map.Container();
 
// Define the XML filename to read that contains the marker data
placeMarkersOnMaps('uploads/markers.xml');
 
}
 
// This function gets passed the marker XML data file name
function placeMarkersOnMaps(filename)
{
var counter = 0;
$.get(filename, function(xml){
$(xml).find("marker").each(function()
{
//Read the name, address, latitude and longitude for each Marker
var name = $(this).find('name').text();
var address = $(this).find('address').text();
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
//Used for testing:
//alert("Name:"+name+" Address:"+address+" Lat:"+lat+" Lon:"+lng);
//Put each marker on the map as the data has been read.
 
markerCoords = new nokia.maps.geo.Coordinate(parseFloat(lat), parseFloat(lng));
 
marker = new nokia.maps.map.StandardMarker(markerCoords,
 
{text:name, brush:"#FF69B4", pen:"#09F911", textPen:"#00009C", shape: "star"});
 
container.objects.add(marker);
 
// Used for testing:
// myInfoBubbles.addBubble(name, markerCoords);
// counter ++ 1;
// alert(counter);
 
});
 
// Add the marker container .
 
map.objects.add(container);
 
});
// Zoom into the markers.
 
alert("Markers have been loaded. Hit OK to zoom the map into the area covering the markers.");
 
map.zoomTo(container.getBoundingBox(), false);
 
}

Example XML file

<?xml version="1.0"?>
 
<markers>
<marker>
<name>M1</name>
<address>ADDRESS 1</address>
<lat>52.516617</lat>
<lng>13.377675</lng>
</marker>
 
<marker>
<name>M2</name>
<address>ADDRESS 2</address>
<lat>52.525286</lat>
<lng>13.369499</lng>
</marker>
 
<marker>
<name>M3</name>
<address>ADDRESS 3</address>
<lat>52.518506</lat>
<lng>13.375082</lng>
</marker>
 
<marker>
<name>M4</name>
<address>ADDRESS 4</address>
<lat>52.52446</lat>
<lng>13.381562</lng>
</marker>
 
<marker>
<name>M5</name>
<address>ADDRESS 5</address>
<lat>52.523572</lat>
<lng>13.378451</lng>
</marker>
 
<marker>
<name>M6</name>
<address>ADDRESS 6</address>
<lat>52.530386</lat>
<lng>13.385189</lng>
</marker>
 
<marker>
<name>M7</name>
<address>ADDRESS 7</address>
<lat>52.518545</lat>
<lng>13.363709</lng>
</marker>
 
<marker>
<name>M8</name>
<address>ADDRESS 8</address>
<lat>52.515738</lat>
<lng>13.378515</lng>
</marker>
 
</markers>

Summary

Edit the script and file for your own purposes, hope you find this article useful.

Tested with

Microsoft XP Pro and the following browsers:

  • Opera 5.0.5 & Firefox 5.0

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,

309 page views in the last 30 days.