﻿var map;
var geocoder;
var xml;
var markers;
var address;

// On page load, call this function
function load()
{
  // Create new map object
  map = new GMap2(document.getElementById("map"));  

   // Set map center location	  
  map.setCenter(new GLatLng(-36.930718, 174.723752), 15);  
  
   // Add Map Controls
   map.addControl(new GSmallMapControl());
   map.addControl(new GMapTypeControl());
   
  // Create new geocoding object
  geocoder = new GClientGeocoder();
  
  // Download the data in data.xml and load it on the map.
   GDownloadUrl("./Assets/data.xml", function(data) {
      xml = GXml.parse(data);
      markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        address = markers[i].getAttribute("address");           
       geocoder.getLocations(address, addToMap);}
    });   
}
// This function adds the point to the map

function addToMap(response)
{
  // Retrieve the object
  place = response.Placemark[0];

  // Retrieve the latitude and longitude
  point = new GLatLng(place.Point.coordinates[1],
                      place.Point.coordinates[0]);

  // Create a marker
  marker = new GMarker(point);

  // Add the marker to map
  map.addOverlay(marker);


}
