How to Read XML Files with the Google Maps API

  • google plus

In Brief...

If you have an XML document containing locations that you want to display on a map, you can load this XML file and display each location as a marker in Google Maps. Here's how:

Instructions

Before you can use Google Maps in your own application, you'll need to get an API key. An API key is a unique series of numbers and letters that a web service provider (such as Google) can use to keep track of who is using their services. Google Maps API keys are free, and use of Google Maps is free for developers.

  1. Start with the following basic Google Map. Make sure to replace YOUR_API_KEY in the script tag with your API key that you got from https://developers.google.com/maps.
  2. 
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>A Basic Map</title>
    	<style>
    		#map {
    			height: 100%;
    		}
    		/* Optional: Makes the sample page fill the window. */
    		html, body {
    			height: 100%;
    			margin: 0;
    			padding: 0;
    		}
    	</style>
    	<script>
    	function initMap() {
    		var mapOptions = {
    			zoom: 3,
    			center: new google.maps.LatLng(37.5, -122),
    			mapTypeId: 'roadmap'
    		};
    		var map = new google.maps.Map(document.getElementById('map'), mapOptions);
    	}
    	</script>
    </head>
    <body>
    <div id="map"></div>
    <script async defer
    		src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>
    </body>
    </html>
    
  3. Use a script tag in the head element to include the jQuery library. We'll use jQuery to simplify the process of opening and reading the XML file.
  4. 
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    
  5. Create an XML file containing the latitude and longitude of places you want to display as markers and save the file with the extension .xml.
  6. 
    <root>
    	<place name="Niagra Falls" latitude="43.0962" longitude="-79.0377" />
    	<place name="Yosemite Falls" latitude="37.7566" longitude="-119.5969" />
    	<place name="Multnomah Falls" latitude="45.5762" longitude="-122.1158" />
    	<place name="Snoqualmie Falls" latitude="47.5417" longitude="-121.8377" />
    </root>
    
  7. Inside the initMap() function, after the var map = statement, use jQuery to load the .xml file. Pass the xml data into the success callback function.
  8. 
    $.ajax({
    	type: "GET",
    	async: true,
    	url: "falls.xml",
    	dataType: "xml",
    	success:
    	function (xml) {
    
    	}
    });
    	
  9. Inside the callback function, create an array of places from the XML data.
  10. 
    var places = xml.documentElement.getElementsByTagName("place");
    	
  11. Loop through each marker and add it to the map.
  12. 
    for (var i = 0; i < places.length; i++) {
    	var lat = places[i].getAttribute('latitude');
    	var long = places[i].getAttribute('longitude');
    	var latLng = new google.maps.LatLng(lat, long);
    	var marker = new google.maps.Marker({
    		position:  latLng,
    		map: map,
    		label:places[i].name
    	});
    }
    	
  13. Open your web page in a browser to see the results.
    XML data loaded into markers

Author: Chris Minnick

Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.

Discuss