×
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 - Add dependencies field to ArticleMetaData)
jasfox (Talk | contribs)
(Jasfox - Updated formatting and jQuery version. Added code commentary.)
Line 3: Line 3:
  
 
{{ArticleMetaData
 
{{ArticleMetaData
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
+
|sourcecode= [[Media:Markersxml.zip]]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
Line 38: Line 38:
 
== 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 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!
+
Note that the example code - [[Media:Markersxml.zip]] -  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!
 
+
<code java>
+
<!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.
+
  
 +
=== Header ===
 +
<code javascript>
 +
<script type="text/javascript" src="http://api.maps.nokia.com/2.0.0/jsl.js" charset="utf-8"></script>
 +
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
 +
<code>
 +
Bith the Nokia Maps API for JavaScript and the jQuery library need to be referenced in the header.
 +
=== Body ===
 +
The Map is set up in ''init()'' the usual manner.
 +
<code javascript>
 
function init(){
 
function init(){
  
// Here we create an Nokia Maps within a Container.
+
// Here we create an Nokia Maps within a Container.  
 
+
 
mapContainer = document.getElementById("map");
 
mapContainer = document.getElementById("map");
 
 
myInfoBubbles = new nokia.maps.map.component.InfoBubbles();
 
myInfoBubbles = new nokia.maps.map.component.InfoBubbles();
 
+
 
myMap = nokia.maps.map,     
 
myMap = nokia.maps.map,     
 
                 map = new myMap.Display(mapContainer, {
 
                 map = new myMap.Display(mapContainer, {
Line 89: Line 65:
 
                   myInfoBubbles ]
 
                   myInfoBubbles ]
 
                 });
 
                 });
 
+
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
placeMarkersOnMaps('uploads/markers.xml');
+
placeMarkersOnMaps('markers.xml');  
 +
}</code>
  
}
+
When the HTML page body section is loaded the  ''placeMarkersOnMaps'' function will be called.
 +
This function gets passed the marker XML data file name. The location of the XML file is relative to the URL of the html page.
 +
'''Both'' files must be hosted on a server for this function to work. The ''err'' function will fire if the file cannot be accessed.
  
// This function gets passed the marker XML data file name
+
<code javascript>
 
function placeMarkersOnMaps(filename)  
 
function placeMarkersOnMaps(filename)  
 
{
 
{
 
var counter = 0;
 
var counter = 0;
    $.get(filename, function(xml){
+
$.ajax({
              $(xml).find("marker").each(function()
+
    type: "GET",
        {
+
    url: filename ,
        //Read the name, address, latitude and longitude for each Marker
+
    dataType: "xml",
            var name = $(this).find('name').text();
+
    success: parseXml,
            var address = $(this).find('address').text();
+
              error : err
            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));  
+
}
 
+
function err (){
            marker = new nokia.maps.map.StandardMarker(markerCoords,
+
alert("An Error has occurred.");
 +
}
 +
</code>
  
            {text:name, brush:"#FF69B4", pen:"#09F911", textPen:"#00009C", shape: "star"});
+
The ''parseXml'' function is the meat of the example. It uses standard jQuery parsing to retrieve the elements from the XML file.
                     
+
<code javascript>
              container.objects.add(marker);    
+
function parseXml(xml)
 
+
{
              // Used for testing:
+
              // myInfoBubbles.addBubble(name, markerCoords);
+
var container = new nokia.maps.map.Container();
              // counter ++ 1;
+
$(xml).find("marker").each(function(){
              // alert(counter);
+
 +
//Read the name, address, latitude and longitude for each Marker
 +
var nme = $(this).find('name').text();
 +
var address = $(this).find('address').text();
 +
var lat = $(this).find('lat').text();
 +
var lng = $(this).find('lng').text();
 +
 +
//Put each marker on the map as the data has been read.
 +
 +
var markerCoords = new nokia.maps.geo.Coordinate(parseFloat(lat), parseFloat(lng));   
 +
 +
var marker = new nokia.maps.map.StandardMarker(markerCoords, {text:nme});
 +
container.objects.add(marker);    
 +
 +
});
 +
// Add the marker container .
 +
map.objects.add(container);
 +
// Zoom into the markers.
 
   
 
   
    });
 
 
// 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.");
 
alert("Markers have been loaded. Hit OK to zoom the map into the area covering the markers.");
 
+
 
map.zoomTo(container.getBoundingBox(), false);
 
map.zoomTo(container.getBoundingBox(), false);
  
Line 142: Line 125:
 
</code>
 
</code>
  
== Example XML file ==
+
=== Example XML file ===
<code java>
+
The file to be parsed is in the following format - the full XML file can be found  in  [[Media:Markersxml.zip]]
 
+
<code xml>
 
<?xml version="1.0"?>
 
<?xml version="1.0"?>
 +
    <markers>
 +
        <marker>
 +
            <name>M1</name>
 +
            <address>ADDRESS 1</address>
 +
            <lat>52.516617</lat>
 +
            <lng>13.377675</lng>
 +
        </marker>
  
<markers>
+
        .... etc
<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>
 
</markers>
 
 
</code>
 
</code>
  
 
== Summary ==
 
== Summary ==
 +
The following screenshot shows how the XML has been parsed
 +
 +
[[File:MarkersXMLexample.png]]
  
 
Edit the script and file for your own purposes, hope you find this article useful.
 
Edit the script and file for your own purposes, hope you find this article useful.

Revision as of 18:28, 5 January 2012

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

Article Metadata
Code ExampleCompatibility
Platform(s): Web browser
Dependencies: Nokia Maps 2.1.0
Article
Created: Maveric (27 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (05 Jan 2012)

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 - Media:Markersxml.zip - 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!

Header

<script type="text/javascript" src="http://api.maps.nokia.com/2.0.0/jsl.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<code>
Bith the Nokia Maps API for JavaScript and the jQuery library need to be referenced in the header.
=== Body ===
The Map is set up in ''init()'' the usual manner.
<code javascript>
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 ]
});
 
// Define the XML filename to read that contains the marker data
placeMarkersOnMaps('markers.xml');
}

When the HTML page body section is loaded the placeMarkersOnMaps function will be called. This function gets passed the marker XML data file name. The location of the XML file is relative to the URL of the html page. 'Both files must be hosted on a server for this function to work. The err function will fire if the file cannot be accessed.

function placeMarkersOnMaps(filename) 
{
var counter = 0;
$.ajax({
type: "GET",
url: filename ,
dataType: "xml",
success: parseXml,
error : err
});
 
}
function err (){
alert("An Error has occurred.");
}

The parseXml function is the meat of the example. It uses standard jQuery parsing to retrieve the elements from the XML file.

function parseXml(xml)
{
 
var container = new nokia.maps.map.Container();
$(xml).find("marker").each(function(){
 
//Read the name, address, latitude and longitude for each Marker
var nme = $(this).find('name').text();
var address = $(this).find('address').text();
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
 
//Put each marker on the map as the data has been read.
 
var markerCoords = new nokia.maps.geo.Coordinate(parseFloat(lat), parseFloat(lng));
 
var marker = new nokia.maps.map.StandardMarker(markerCoords, {text:nme});
container.objects.add(marker);
 
});
// 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

The file to be parsed is in the following format - the full XML file can be found in Media:Markersxml.zip

<?xml version="1.0"?>
<markers>
<marker>
<name>M1</name>
<address>ADDRESS 1</address>
<lat>52.516617</lat>
<lng>13.377675</lng>
</marker>
 
.... etc
</markers>

Summary

The following screenshot shows how the XML has been parsed

MarkersXMLexample.png

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,

333 page views in the last 30 days.
×