- Obtaining a License Key
- Before You Map: Find Your Coordinates
- Creating a Basic Single-Location Map
- Adding a Marker to Your Map
- Adding an Info Window
- Adding Map Controls
- Adding Multiple Markers from Your Own Database
Adding Multiple Markers from Your Own Database
We’ll end this exercise with an adventure into more sophisticated Google Maps mashups—particularly, a mashup that maps multiple locations.
To plot multiple locations on a Google map, you have to create a database of those locations. Each location in the database has to be expressed as a latitude/longitude coordinate, of course; the database of coordinates can then be easily plotted as an overlay on the base Google map.
Let’s start with the database of locations. You need to create an XML file named data.xml. The contents of the file should be in the following format:
<markers> <marker lat="LATITUDE1" lng="LONGITUDE1" /> <marker lat="LATITUDE2" lng="LONGITUDE2" /> <marker lat="LATITUDE3" lng="LONGITUDE3" /> <marker lat="LATITUDE4" lng="LONGITUDE4" /> </markers>
Add as many <marker> lines as you like, each with its own coordinates.
You then call this file into your Google Maps code, using the GDownloadUrl command. You do this by adding the following lines of code after the map.setCenter line in the <HEAD> of your document:
GDownloadUrl("data.xml", function(data, responseCode) { var xml = GXml.parse(data); var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng"))); map.addOverlay(new GMarker(point));
This adds a new overlay to your map, with each point from the data.xml file translated into its own marker on the map. Very cool!