Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Interrogating Google Geocoding results

    I've not worked with JSON and am a Javascript novice too and I'm confused with the results I get from the Geocoder API. I am interested in finding latitude and longitude and need to extract these as individual numbers for later processing - I get the lat and long in a format (xxxx, xxxx) but don't seem to be able to do anything more with it.

    To get the above lat and long format I just use:

    Code:
    function codeAddress() {
    geocoder = new google.maps.Geocoder();
           var address = document.getElementById('address').value;
           geocoder.geocode( { 'address': address}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
    		var latLon=results[0].geometry.location;
    		alert(results[0].geometry.location);
    	  } else {
                    alert('Geocode was not successful for the following reason: ' + status);
              }
           });
    I would have thought to get individual lat and long I could use:

    Code:
    var lat=results[0].geometry.location.lat;
    var lon=results[0].geometry.location.lng;
    Or even manipulate latLon as a string - seems not.

    The Geocoding result format is at :

    https://developers.google.com/maps/d...ion/geocoding/

    Code:
    {
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "1600",
                   "short_name" : "1600",
                   "types" : [ "street_number" ]
                },
                {
                   "long_name" : "Amphitheatre Pkwy",
                   "short_name" : "Amphitheatre Pkwy",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "Mountain View",
                   "short_name" : "Mountain View",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Santa Clara",
                   "short_name" : "Santa Clara",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "California",
                   "short_name" : "CA",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "United States",
                   "short_name" : "US",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "94043",
                   "short_name" : "94043",
                   "types" : [ "postal_code" ]
                }
             ],
             "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
             "geometry" : {
                "location" : {
                   "lat" : 37.42291810,
                   "lng" : -122.08542120
                },
                "location_type" : "ROOFTOP",
                "viewport" : {
                   "northeast" : {
                      "lat" : 37.42426708029149,
                      "lng" : -122.0840722197085
                   },
                   "southwest" : {
                      "lat" : 37.42156911970850,
                      "lng" : -122.0867701802915
                   }
                }
             },
             "types" : [ "street_address" ]
          }
       ],
       "status" : "OK"
    }
    Any assistance appreciated.

  • #2
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found it, for reference in case anyone stumbles across this post I used:

    Code:
    var lat=results[0].geometry.location.lat();
    var lon=results[0].geometry.location.lng();


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •