×
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 - links)
jasfox (Talk | contribs)
m (Jasfox - link)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Nokia Maps]][[Category:Code Examples]][[Category:JavaScript]][[Category:XML]]
+
[[Category:HERE Maps]][[Category:Code Examples]][[Category:JavaScript]][[Category:XML]]
 
{{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
 
{{ArticleMetaData
|sourcecode=  [[Media:Markersxml.zip|Markersxml.zip]]
+
|sourcecode=  [https://github.com/heremaps/examples/blob/master/maps_api_for_javascript/demos/load-marker-data-using-ajax/index.html load-marker-data-using-ajax.html]
 
|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=Internet Explorer, Firefox, Opera, Google Chrome
 
|devices=Internet Explorer, Firefox, Opera, Google Chrome
Line 9: Line 9:
 
|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.2.3
+
|dependencies=HERE Maps 2.5.3 or higher
 
|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. -->
|keywords= Nokia Maps, jQuery, JavaScript, XML, KML
+
|keywords= HERE Maps, jQuery, JavaScript, XML, KML
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
Line 26: Line 26:
 
}}
 
}}
 
  {{SeeAlso|
 
  {{SeeAlso|
* [http://developer.here.net/javascript_api Nokia Maps API]   
+
* [http://developer.here.com/javascript_api HERE Maps API]
 +
* [http://heremaps.github.io/examples/index.html HERE Maps on Github]   
 
* [http://developer.here.net/apiexplorer/examples/api-for-js/markers/marker.html Adding a Marker]
 
* [http://developer.here.net/apiexplorer/examples/api-for-js/markers/marker.html Adding a Marker]
 
* [http://developer.here.net/apiexplorer/examples/api-for-js/data-visualization/map-with-interactive-kml-objects.html Loading a KML file example]
 
* [http://developer.here.net/apiexplorer/examples/api-for-js/data-visualization/map-with-interactive-kml-objects.html Loading a KML file example]
* [[Nokia Maps API - How to display KML file data on the map]]
+
* [[HERE Maps API - How to display KML file data on the map]]
 
* [[Reading XML Map Marker data with the Maps API for Java ME]]
 
* [[Reading XML Map Marker data with the Maps API for Java ME]]
 
}}
 
}}
Line 51: Line 52:
 
== Prerequisites ==
 
== Prerequisites ==
  
* Nokia Maps API supported web browser (basically any modern web browser).
+
* HERE 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.
Line 57: Line 58:
 
== Example code ==
 
== Example code ==
  
Note that the example code - [[Media:Markersxml.zip|Markersxml.zip]] - leaves you room to implement your own {{Icode|InfoBubbles}}, and some other functionality (for e.g. the behaviour of the map and availability of components) is there if you need it, otherwise you can remove it. Feel free to copy and modify for your own purposes!
+
When the HTML page body section is loaded the  {{Icode|loadXMLFile()}} method will be called. The location of the XML file is relative to the URL of the HTML page.
 +
{{Note|'''Both''' files must be hosted on a server for this function to work. The {{Icode|err}} method will fire if the file cannot be accessed. }}
  
=== 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.2.3/jsl.js" charset="utf-8"></script>
+
function loadXMLFile(){
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+
var filename = './data/markers.xml';
 +
$.ajax({
 +
type: "GET",
 +
url: filename ,
 +
dataType: "xml",
 +
success: parseXML,
 +
error : onXMLLoadFailed
 +
});
 +
 +
}
 
</code>
 
</code>
 
+
When an error occurs a simple {{Icode|alert()}} is shown:
=== Body ===
+
The Map is set up in {{Icode|init()}} the usual manner.
+
 
<code javascript>
 
<code javascript>
function init(){
+
function onXMLLoadFailed(){
 
+
alert("An Error has occurred.");
// Here we create an instance of 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');
+
}</code>
+
 
+
When the HTML page body section is loaded the  {{Icode|placeMarkersOnMaps()}} method 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|'''Both''' files must be hosted on a server for this function to work. The {{Icode|err}} method will fire if the file cannot be accessed. }}
+
 
+
<code javascript>
+
function placeMarkersOnMaps(filename)
+
{
+
var counter = 0;
+
$.ajax({
+
    type: "GET",
+
    url: filename ,
+
    dataType: "xml",
+
    success: parseXml,
+
              error : err
+
  });
+
 
+
}
+
function err (){
+
alert("An Error has occurred.");
+
 
}
 
}
 
</code>
 
</code>
Line 114: Line 83:
 
The {{Icode|parseXml()}} function is the meat of the example. It uses standard '''jQuery''' parsing to retrieve the elements from the XML file.
 
The {{Icode|parseXml()}} function is the meat of the example. It uses standard '''jQuery''' parsing to retrieve the elements from the XML file.
 
<code javascript>
 
<code javascript>
function parseXml(xml)
+
function parseXML(xml){
{
+
container = new nokia.maps.map.Container();
var container = new nokia.maps.map.Container();
+
 
$(xml).find("marker").each(function(){
 
$(xml).find("marker").each(function(){
+
//Read the name, address, latitude and longitude for each Marker
//Read the name, address, latitude and longitude for each Marker
+
var nme = $(this).find('name').text();
var nme = $(this).find('name').text();
+
var address = $(this).find('address').text();
var address = $(this).find('address').text();
+
var lat = $(this).find('lat').text();
var lat = $(this).find('lat').text();
+
var lng = $(this).find('lng').text();
var lng = $(this).find('lng').text();
+
var markerCoords = new nokia.maps.geo.Coordinate
+
(parseFloat(lat), parseFloat(lng));     
//Put each marker on the map as the data has been read.
+
container.objects.add(new nokia.maps.map.StandardMarker(
+
markerCoords, {text:nme}));       
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);
+
  
 +
});
 +
map.objects.add(container);
 +
map.zoomTo(container.getBoundingBox(), false);
 
}
 
}
 
</code>
 
</code>
  
 
=== Example XML file ===
 
=== Example XML file ===
The file to be parsed is in the following format - the full XML file can be found  in  [[Media:Markersxml.zip]]
+
The file to be parsed is in the following format - the full XML file can be found  in  [https://github.com/heremaps/examples/blob/master/maps_js/data/markers.xml markers.xml]
 
<code xml>
 
<code xml>
 
<?xml version="1.0"?>
 
<?xml version="1.0"?>
Line 165: Line 123:
 
[[File:MarkersXMLexample.png]]
 
[[File:MarkersXMLexample.png]]
  
Edit the script and file for your own purposes, hope you find this article useful.
+
A fully working example can be found at:
 +
 
 +
http://heremaps.github.io/examples/examples.html#load-marker-data-using-ajax__index
  
 
== Alternative  Solution ==
 
== 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]]  
+
Since the latest version of the HERE 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  [https://github.com/heremaps/examples/blob/master/maps_js/data/markers.xml markers.xml]  
 
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://developer.here.net/apiexplorer/examples/api-for-js/data-visualization/map-with-interactive-kml-objects.html Loading a KML file example] on the [http://developer.here.net/javascript_api_explorer API explorer]
 
using the code from the [http://developer.here.net/apiexplorer/examples/api-for-js/data-visualization/map-with-interactive-kml-objects.html Loading a KML file example] on the [http://developer.here.net/javascript_api_explorer API explorer]
Line 194: Line 154:
 
</code>
 
</code>
  
==For more on the Nokia Maps API==
+
==For more on the HERE Maps API==
  
Please check out the Nokia Maps API full documentation and API reference here:
+
Please check out the HERE Maps API full documentation and API reference here:
* [http://developer.here.net/javascript_api Nokia Maps API]
+
* [http://developer.here.net/javascript_api HERE Maps API]
  
 
You may also access the interactive API explorer
 
You may also access the interactive API explorer
 
* [http://developer.here.net/javascript_api_explorer API explorer]
 
* [http://developer.here.net/javascript_api_explorer API explorer]

Latest revision as of 18:37, 20 December 2013

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

Article Metadata
Code ExampleTested with
Devices(s): Internet Explorer, Firefox, Opera, Google Chrome
Compatibility
Platform(s): Web browser
Dependencies: HERE Maps 2.5.3 or higher
Article
Keywords: HERE Maps, jQuery, JavaScript, XML, KML
Created: Maveric (27 Jun 2011)
Updated: jasfox (31 Dec 2011)
Last edited: jasfox (20 Dec 2013)

Contents

[edit] Introduction

Sometimes it is useful to have the data for a map, e.g. markers, in a separate file. This article provides an example which you can use to generate map content from an XML file. The example uses JavaScript and jQuery, and processes an XML data file of the following format:

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

[edit] Prerequisites

  • HERE 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.

[edit] Example code

When the HTML page body section is loaded the loadXMLFile() method will be called. 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 method will fire if the file cannot be accessed.

function loadXMLFile(){
var filename = './data/markers.xml';
$.ajax({
type: "GET",
url: filename ,
dataType: "xml",
success: parseXML,
error : onXMLLoadFailed
});
 
}

When an error occurs a simple alert() is shown:

function onXMLLoadFailed(){
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){
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();
var markerCoords = new nokia.maps.geo.Coordinate
(parseFloat(lat), parseFloat(lng));
container.objects.add(new nokia.maps.map.StandardMarker(
markerCoords, {text:nme}));
 
});
map.objects.add(container);
map.zoomTo(container.getBoundingBox(), false);
}

[edit] Example XML file

The file to be parsed is in the following format - the full XML file can be found in markers.xml

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

[edit] Summary

The following screenshot shows how the XML has been parsed

MarkersXMLexample.png

A fully working example can be found at:

http://heremaps.github.io/examples/examples.html#load-marker-data-using-ajax__index

[edit] Alternative Solution

Since the latest version of the HERE 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 markers.xml 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 API explorer

<?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>

[edit] 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

This page was last modified on 20 December 2013, at 18:37.
314 page views in the last 30 days.
×