Search code examples
javascriptsplitgoogle-geocoder

Using split() on a Javascript variable (and googlemaps geocoder)


I think im missing something realyl stupid here... basically iam trying to get coordinates of a location by postocde (kinda irrelevant) by using this:

            geocoder.geocode({address: address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                onPostcodeLocation(results[0].geometry.location);
            } else {
                alert(address + ' not found');
            }

So this passes the lat/long values to the OnPostcodeLocation():

        function onPostcodeLocation(position) { 
          var positionVar = position;
          positionArray = position.split(",");
          alert(positionArray[0]);
      }

Now, if I alert() "position" in the first line of the onPostcodeLocation(), I get the correct value alerted, but as shown above im trying to split the values up to seperate variables. trying to put alert(positionArray[0]); doesnt do anything... what I am doing wrong????

Thanks


Solution

  • After a quick look at the docs I've noticed that the location property isn't a string, or array but an object:

    location : {
        "lat" : 37.42291810,
        "lng" : -122.08542120
    }
    

    So obviously, split won't work. Just alert(position.lat) or alert(position.lng). Also: be weary of implied global variables, when using a variable in a function, always declare it first, better to declare it too much than not at all.