var map;
var markers = [];
var infoWindow;
var plugins_url = 'https://nathanielshope.org/wp-content/plugins/buddy-break-map/';
var customIcons = {
heart: {
icon: plugins_url + 'images/icon-heart-small.png',
shadow: ''
}
};
var hqLat = 28.449482;
var hqLng = -81.352691;
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(hqLat, hqLng),
zoom: 13,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl(plugins_url + "map.php", function(data) {
var xml = data.responseXML;
console.log(xml);
var markers = xml.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
if(markers.length > 0) {
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var street_address = markers[i].getAttribute("street_address");
var city = markers[i].getAttribute("city");
var state = markers[i].getAttribute("state");
var zip = markers[i].getAttribute("zip_code");
var coordinator_name = markers[i].getAttribute("coordinator_name");
var phone_number_1 = markers[i].getAttribute("phone_number_1");
var email_address = markers[i].getAttribute("email_address");
var web_address = markers[i].getAttribute("web_address");
var permalink = markers[i].getAttribute("permalink");
var type = markers[i].getAttribute("type");
var distance = markers[i].getAttribute("distance");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
console.log(point);
var html = "";
html += "" + name + "
" + street_address + "
" + city + ", " + state + " " + zip + "
";
html += "Buddy Break Coordinator
" + coordinator_name + "
" + phone_number_1 + "
" + email_address + "
View Website";
if(distance != '0') {
html += '
Distance from location: ' + distance + ' Miles';
}
html += "
View more information »";
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);
bounds.extend(point);
}
} else {
var point = new google.maps.LatLng(hqLat,hqLng);
bindInfoWindow(marker, map, infoWindow, html);
bounds.extend(point);
}
map.fitBounds(bounds);
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function getLatLng() {
var address = document.getElementById("addressInput").value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.buddyBreakSearch.lat.value = results[0].geometry.location.lat();
document.buddyBreakSearch.lng.value = results[0].geometry.location.lng();
//console.log(document.buddyBreakSearch.lat.value);
//console.log(document.buddyBreakSearch.lng.value);
return true;
}
});
}
/*function searchLocations() {
var address = document.getElementById("addressInput").value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
searchLocationsNear(results[0].geometry.location);
} else {
alert(address + ' not found');
}
});
}
function searchLocationsNear(center) {
//clearLocations();
var radius = document.getElementById('radiusSelect').value;
var searchUrl = plugins_url + 'map.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
//var searchUrl = plugins_url + 'map.php';
downloadUrl(searchUrl, function(data) {
var xml = data.responseXML;
var markerNodes = xml.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markerNodes.length; i++) {
var name = markerNodes[i].getAttribute("name");
var street_address = markerNodes[i].getAttribute("street_address");
var type = markerNodes[i].getAttribute("type");
var distance = parseFloat(markerNodes[i].getAttribute("distance"));
var latlng = new google.maps.LatLng(
parseFloat(markerNodes[i].getAttribute("lat")),
parseFloat(markerNodes[i].getAttribute("lng"))
);
var html = "" + name + "
" + street_address;
var icon = customIcons[type] || {};
//createOption(name, distance, i);
createMarker(latlng, name, street_address);
bounds.extend(latlng);
}
//map.fitBounds(bounds);
});
}
function createMarker(latlng, name, address) {
var html = "" + name + "
" + address;
var marker = new google.maps.Marker({
map: map,
position: latlng
});
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
markers.push(marker);
}*/
function openMoreInfo(permalink) {
//var infoDiv = jQuery("a[name=" + permalink + "]").next().next().next().next(); /* Use this if the "More Info" button is being used */
var infoDiv = jQuery("a[name=" + permalink + "]").next().next().next(); /* Use this if the church name is the More Info link */
if(infoDiv.is(":hidden")) {
//jQuery("a[name=" + permalink + "]").next().children("a.view_more_info").html('Collapse »');
infoDiv.show();
}
}
function doNothing() {}