Last active
August 29, 2015 14:22
-
-
Save icetee/62b6a204449e5bd4a645 to your computer and use it in GitHub Desktop.
acf-google-map.js added "set zoom" feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
function render_map($el) { | |
var $markers = $el.find('.marker'); | |
// Add custom data-zoom | |
window.gmapzoom = parseInt($markers.attr('data-zoom')); | |
if ( isNaN(window.gmapzoom) ) { window.gmapzoom = 16; } | |
var args = { | |
zoom: window.gmapzoom, | |
center: new google.maps.LatLng(0, 0), | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}; | |
var map = new google.maps.Map($el[0], args); | |
map.markers = []; | |
$markers.each(function () { | |
add_marker($(this), map); | |
}); | |
center_map(map); | |
} | |
function add_marker($marker, map) { | |
var latlng = new google.maps.LatLng($marker.attr('data-lat'), $marker.attr('data-lng')); | |
var marker = new google.maps.Marker({ | |
position: latlng, | |
map: map | |
}); | |
map.markers.push(marker); | |
if ($marker.html()) { | |
var infowindow = new google.maps.InfoWindow({ | |
content: $marker.html() | |
}); | |
google.maps.event.addListener(marker, 'click', function () { | |
infowindow.open(map, marker); | |
}); | |
} | |
} | |
function center_map(map) { | |
var bounds = new google.maps.LatLngBounds(); | |
$.each(map.markers, function (i, marker) { | |
var latlng = new google.maps.LatLng(marker.position.lat(), marker.position.lng()); | |
bounds.extend(latlng); | |
}); | |
if (map.markers.length == 1) { | |
map.setCenter(bounds.getCenter()); | |
map.setZoom(window.gmapzoom); | |
} else { | |
map.fitBounds(bounds); | |
} | |
} | |
$(document).ready(function () { | |
$('.acf-map').each(function () { | |
render_map($(this)); | |
}); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment