Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2013, 05:19 PM   PM User | #1
BlastFirst
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
BlastFirst is an unknown quantity at this point
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.
BlastFirst is offline   Reply With Quote
Old 02-12-2013, 05:39 PM   PM User | #2
BlastFirst
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
BlastFirst is an unknown quantity at this point
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();
BlastFirst is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:33 PM.


Advertisement
Log in to turn off these ads.