Skip to content Skip to sidebar Skip to footer

Storing Returned Value From Google Geocoding In An External Javascript Variable

Unable to store the returned value from google geocoding into a global/external (javascript) variable (latlng1, in the following case).... perhaps because the the variable gets its

Solution 1:

As mentioned in comments you need to wait until google.maps.Geocoder() finishes its execution.

Your function geocode doesn't return anything, so no suprises that variable latlng1 is undefined. Anyway if geocode would return latLng value you have no guarantee that latlng1 will be defined.

My suggestion:

  1. Just call geocoder function without assigning it to latlng1.
  2. Make latlng1 variable global by initialising it outside all functions.
  3. Don't use latLng variable at all and just assign the required value directly to the global variable latlng1 like this:

    latlng1 = results[0].geometry.location;
  4. Replace latLng variable with latlng1 in these two calls: add_marker(latlng1, query) and mycallback(latlng1) functions or (This last advice is not essential, just recommended.) just redefine add_marker() to accept only one value - query and mycallback() accept no values and just use global latlng1 inside those functions.

Post a Comment for "Storing Returned Value From Google Geocoding In An External Javascript Variable"