I've read and read and read but I can't figure this out. I have an array of lats and an array lngs. I need to be able to pass the values into new google.maps.LatLng() from the array. It seems like this would work:
new google.maps.LatLng(lats[0], lngs[0])
But it does not. I can not figure out how to make this work. Help?
Here is how the arrays are created:
<script type="text/javascript">
var lats = [];
var lngs = [];
function initialize() {
var geo = new google.maps.Geocoder;
var address = "<?php echo $arrayAddress; ?>";
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
lats.push(results[0].geometry.location.lat());
lngs.push(results[0].geometry.location.lng());
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
if I console.log(lat[0]) I get the expected result.
And here is where I'm trying to create the new.google.mapsLatLng():
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(lats[0], lngs[0])
};
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
This is outside of the loop in wordpress. The geocoder part is inside the loop. I'm working on geocoding an address for all of the posts in the loop and moving the coordinates into that array, then placing the array of coordinates as markers on the map outside of the loop.
Lats andLngs are created?new google.maps.LatLng(lats[0], lngs[0])in the context of the code posted above? It would need to be within the callback constructing thelatsandlngsarrays, since that callback will be executed sometime after the page has loaded.function initialize()twice - is that a problem? Should Those two functions have different names?