1

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.

6
  • This doesn't appear to be enough code for us to solve the problem. Can you show us more code, like how your arrays of Lats and Lngs are created? Commented Aug 7, 2014 at 2:28
  • Edited to include this information. Commented Aug 7, 2014 at 2:31
  • 2
    And where is the line 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 the lats and lngs arrays, since that callback will be executed sometime after the page has loaded. Commented Aug 7, 2014 at 2:39
  • Edited to include this code. I'm not 100% sure if I am within the callback - I might not be - but I don't understand that completely. Maybe that is where the problem is. @GregL - can you tell me if that's where it's breaking? Also - as I'm looking at this I'm noticing that I have used function initialize() twice - is that a problem? Should Those two functions have different names? Commented Aug 7, 2014 at 10:52
  • The initialize function is not in the callback. Both your initialize function and your geocode operation start on the window load event. can you provide the contents of "address"? To me it looks like your code will only ever geocode the first address, if that (the "geocode" function only takes a single address, not an array). Commented Aug 7, 2014 at 11:01

1 Answer 1

1
 var locat = new google.maps.LatLng(34,74);
            geocoder = new google.maps.Geocoder();
            var mapOptions = {
                zoom: 11,
                center: locat,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById(divID), mapOptions);
Sign up to request clarification or add additional context in comments.

2 Comments

This is not what I need. I need to pass the latitude and longitude into the google.maps.LatLng() using one array containing latitudes and one array containing longitudes.
so map them with help of array index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.