I am currently loading a json file and then parsing it. The entries in the json file is stored as "places", which I defined as a global variable, but the browser still says it is undefined.
var request;
var places;
var map;
var myLatLng = {lat: 34, lng: 38};
// load database and parse into entries
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', 'places.json');
request.onreadystatechange = function() {
if ((request.readyState ===4) && (request.status===200)) {
places = JSON.parse(request.responseText);
}
}
request.send();
function initMap() {
var mapOptions = {
zoom: 6,
center: myLatLng,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP_RIGHT
},
mapTypeId: google.maps.MapTypeId.HYBRID
};
// initialize the map
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
for (var i = 0; i < places.length; i++) {
// the place
var place = places[i];
// place co-ordinates
var latlng = new google.maps.LatLng(place.latitude, place.longitude);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: place.city
});
}
}