×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Create map markers from XML data

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - Add link.)
jasfox (Talk | contribs)
m (Jasfox - Update code commentary to reflect 2.2)
Line 3: Line 3:
  
 
{{ArticleMetaData
 
{{ArticleMetaData
|sourcecode=  [[Media:Markersxml.zip]]
+
|sourcecode=  [[Media:Markersxml.zip|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=Opera 5.0.5 & Firefox 5.0
+
|devices=Internet Explorer, Firefox, Opera, Google Chrome
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|platform= Web browser
 
|platform= Web browser
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies=Nokia Maps 2.1.0
+
|dependencies=Nokia Maps 2.2.0
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
Line 27: Line 27:
 
  {{SeeAlso|
 
  {{SeeAlso|
 
* [http://api.maps.nokia.com/ Nokia Maps API]   
 
* [http://api.maps.nokia.com/ Nokia Maps API]   
* [http://api.maps.nokia.com/2.1.1/playground/?example&#61;standardmarker Adding a Marker]
+
* [http://api.maps.nokia.com/en/playground/examples/maps/map_objects/standardmarker.html Adding a Marker]
* [http://api.maps.nokia.com/2.1.1/playground/?example&#61;kmlfile Loading a KML file example]
+
* [http://api.maps.nokia.com/en/playground/examples/maps/kml/kmlfile.html Loading a KML file example]
 
* [[Nokia Maps API - How to display KML file data on the map]]
 
* [[Nokia Maps API - How to display KML file data on the map]]
 
}}
 
}}
Line 44: Line 44:
 
== Example code ==
 
== 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!
+
Note that the example code - [[Media:Markersxml.zip|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 ===
 
=== Header ===
 
Both the Nokia Maps API for JavaScript and the jQuery library need to be referenced in the header.
 
Both the Nokia Maps API for JavaScript and the jQuery library need to be referenced in the header.
 
<code javascript>
 
<code javascript>
<script type="text/javascript" src="http://api.maps.nokia.com/2.0.0/jsl.js" charset="utf-8"></script>
+
<script type="text/javascript" src="http://api.maps.nokia.com/2.2.0/jsl.js" charset="utf-8"></script>
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
 
</code>
 
</code>
Line 157: Line 157:
 
Since the latest version of the Nokia Maps API is able to natively render KML files, it would make sense to transform the XML into KML using XSLT. You can copy the ''markers.xml'' file from  [[Media:Markersxml.zip]]  
 
Since the latest version of the Nokia Maps API is able to natively render KML files, it would make sense to transform the XML into KML using XSLT. You can copy the ''markers.xml'' file from  [[Media:Markersxml.zip]]  
 
into an [http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog XSLT Editor] and use the the XSLT stylesheet described below.The KML can then be loaded directly onto a map
 
into an [http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog XSLT Editor] and use the the XSLT stylesheet described below.The KML can then be loaded directly onto a map
using the code from the [http://api.maps.nokia.com/2.1.1/playground/?example&#61;kmlfile Loading a KML file example] on the Developer's Playground.
+
using the code from the [http://api.maps.nokia.com/en/playground/examples/maps/kml/kmlfile.html Loading a KML file example] on the Developer's Playground.
  
 
<code xml>
 
<code xml>
Line 191: Line 191:
  
 
You may also access the interactive Nokia Maps API playground,
 
You may also access the interactive Nokia Maps API playground,
* http://api.maps.nokia.com/2.1.0/playground/index.html
+
* http://api.maps.nokia.com/en/playground/env/desktop/

Revision as of 12:52, 13 April 2012

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

Article Metadata
Code Example
Source file: Markersxml.zip
Tested with
Devices(s): Internet Explorer, Firefox, Opera, Google Chrome
Compatibility
Platform(s): Web browser
Dependencies: Nokia Maps 2.2.0
Article
Keywords: Nokia Maps, jQuery, JavaScript, KML
Created: Maveric (27 Jun 2011)
Updated: jasfox (31 Dec 2011)
Last edited: jasfox (13 Apr 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 - 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

Both the Nokia Maps API for JavaScript and the jQuery library need to be referenced in the header.

<script type="text/javascript" src="http://api.maps.nokia.com/2.2.0/jsl.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

Body

The Map is set up in init() the usual manner.

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.

Note.pngNote: 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.

Alternative Solution

Since the latest version of the Nokia Maps API is able to natively render KML files, it would make sense to transform the XML into KML using XSLT. You can copy the markers.xml file from Media:Markersxml.zip into an XSLT Editor and use the the XSLT stylesheet described below.The KML can then be loaded directly onto a map using the code from the Loading a KML file example on the Developer's Playground.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:template match="markers">
<Document>
<xsl:for-each select="marker">
<Placemark>
<name><xsl:value-of select="name"/></name>
<address><xsl:value-of select="address"/></address>
<coordinates>
<xsl:value-of select="lat"/>,<xsl:value-of select="lng"/>,0
</coordinates>
</Placemark>
</xsl:for-each>
</Document>
</xsl:template>
</xsl:stylesheet>

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,

293 page views in the last 30 days.
×