Parsing Json Google GeoMaps - json

I am trying to parse a response from google maps, this is the JSON response.
{ "results" : [ { "address_components" : [ { "long_name" : "11", "short_name" : "11", "types" : [ "street_number" ] }, { "long_name" : "Shottermill", "short_name" : "Shottermill", "types" : [ "route" ] }, { "long_name" : "Horsham", "short_name" : "Horsham", "types" : [ "postal_town" ] }, { "long_name" : "West Sussex", "short_name" : "West Sussex", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "England", "short_name" : "England", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United Kingdom", "short_name" : "GB", "types" : [ "country", "political" ] }, { "long_name" : "RH12 5HG", "short_name" : "RH12 5HG", "types" : [ "postal_code" ] } ], "formatted_address" : "11 Shottermill, Horsham RH12 5HG, UK", "geometry" : { "location" : { "lat" : 51.084119, "lng" : -0.2948757 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 51.0854679802915, "lng" : -0.2935267197084979 }, "southwest" : { "lat" : 51.0827700197085, "lng" : -0.296224680291502 } } }, "place_id" : "ChIJ0ZUiT-_rdUgRadZ4jXXzam4", "types" : [ "street_address" ] } ], "status" : "OK" }
If i want to for example echo out geometry( lat and lng) , how do i do this.
I have tried : ($content contains the above response)
$json = json_decode($content);
echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;

The results is in an array, you should access it using by index
$json->results[0]->geometry->location->lat;
Check the documentation to see if the results array could have a length different to 1. If it is, you will need to iterate through it.

Related

Google Maps API Doesn't Return LatLng from Extended Zip Code - USA - 9 digit Zip code

The following shows Lat and Long
https://maps.googleapis.com/maps/api/geocode/json?address=90005
But with extended Zip code, it doesn't
https://maps.googleapis.com/maps/api/geocode/json?address=900053200
{
"results" : [],
"status" : "ZERO_RESULTS"
}
Is there a separate service for extended Zip codes?
The format required for zip + 4 is 90005-3200.
Using that format gives me a result (the same result as for zip code 90005).
https://maps.googleapis.com/maps/api/geocode/json?address=90005-3200&key=MY-API-KEY
returns:
{
"results" : [
{
"address_components" : [
{
"long_name" : "90005",
"short_name" : "90005",
"types" : [ "postal_code" ]
},
{
"long_name" : "Central LA",
"short_name" : "Central LA",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Los Angeles",
"short_name" : "Los Angeles",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Los Angeles County",
"short_name" : "Los Angeles County",
"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" ]
}
],
"formatted_address" : "Los Angeles, CA 90005, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.0637149,
"lng" : -118.282294
},
"southwest" : {
"lat" : 34.05513,
"lng" : -118.3393861
}
},
"location" : {
"lat" : 34.0578814,
"lng" : -118.3096648
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.0637149,
"lng" : -118.282294
},
"southwest" : {
"lat" : 34.05513,
"lng" : -118.3393861
}
}
},
"partial_match" : true,
"place_id" : "ChIJ0U_geYS4woAR6-RMnUWG2Gc",
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}

Google Geocoding API Not Returning adminstrative area level 2

We're using the Google Geocoding API to populate the County field in a database of store locations. The API returns a location's County in the "administrative_area_level_3" node of the result set.
An example of the result set we'd expect is:
"results" : [
{
"address_components" : [
{
"long_name" : "1100",
"short_name" : "1100",
"types" : [ "street_number" ]
},
{
"long_name" : "Front Street",
"short_name" : "Front St",
"types" : [ "route" ]
},
{
"long_name" : "Morgan City",
"short_name" : "Morgan City",
"types" : [ "locality", "political" ]
},
{
"long_name" : "6",
"short_name" : "6",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "St. Mary County",
"short_name" : "St Mary County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Louisiana",
"short_name" : "LA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "70380",
"short_name" : "70380",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1100 Front St, Morgan City, LA 70380, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 29.7015251,
"lng" : -91.21346009999999
},
"southwest" : {
"lat" : 29.701518,
"lng" : -91.213477
}
},
"location" : {
"lat" : 29.701518,
"lng" : -91.213477
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 29.7028705302915,
"lng" : -91.21211956970848
},
"southwest" : {
"lat" : 29.7001725697085,
"lng" : -91.2148175302915
}
}
},
"place_id" : "EikxMTAwIEZyb250IFN0LCBNb3JnYW4gQ2l0eSwgTEEgNzAzODAsIFVTQQ",
"types" : [ "street_address" ]
}
],
"status" : "OK"
Ocassionally, the API returns a result set that does not contain the administrative_area_level_3 node. For example:
"results" : [
{
"address_components" : [
{
"long_name" : "48th Street",
"short_name" : "48th St",
"types" : [ "route" ]
},
{
"long_name" : "Newport News",
"short_name" : "Newport News",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Virginia",
"short_name" : "VA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "48th St, Newport News, VA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.004314,
"lng" : -76.40833950000001
},
"southwest" : {
"lat" : 36.992475,
"lng" : -76.439454
}
},
"location" : {
"lat" : 36.9986291,
"lng" : -76.4239863
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 37.004314,
"lng" : -76.40833950000001
},
"southwest" : {
"lat" : 36.992475,
"lng" : -76.439454
}
}
},
"partial_match" : true,
"place_id" : "ChIJf4pmrRCduokR6tpCG9H5Wbg",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "2306",
"short_name" : "2306",
"types" : [ "street_number" ]
},
{
"long_name" : "48th Street",
"short_name" : "48th St",
"types" : [ "route" ]
},
{
"long_name" : "Briarfield",
"short_name" : "Briarfield",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Hampton",
"short_name" : "Hampton",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Virginia",
"short_name" : "VA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "23661",
"short_name" : "23661",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "2306 48th St, Hampton, VA 23661, USA",
"geometry" : {
"location" : {
"lat" : 37.004314,
"lng" : -76.40833950000001
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.0056629802915,
"lng" : -76.40699051970851
},
"southwest" : {
"lat" : 37.0029650197085,
"lng" : -76.40968848029152
}
}
},
"partial_match" : true,
"place_id" : "ChIJGTG5l9-cuokRmnDAATl0HQI",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
We did some research on these addresses, and found that the addresses are correct (no typos). So we're not sure why the API isn't returning the administrative area level 3 value.
Does anyone know why the API doesn't return this node for some of the locations?
Thank you!

MySQL: filter by value in a JSON string?

I'm trying to implement a query that computes the distance between two pairs of lat/lgn. Assume that my lat/lng values are in a JSON string, would such a query be possible (I'm assuming some string functions might be needed first to extract lat/lng)? And if possible, is it a good idea to do it? Will it scale? Or am I better off having separate columns in the database specifically for lat/lng values?
Example JSON:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"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.4220352,
"lng" : -122.0841244
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.42338418029149,
"lng" : -122.0827754197085
},
"southwest" : {
"lat" : 37.4206862197085,
"lng" : -122.0854733802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

google maps api not taking street

I have a problem with using Google Maps API. I use a geolocation service for the coordination of streets, but faced with the problem. When I select a street that I need API does not calculate right the coordinates of the street. JSON response shows that the API search only city. This situation only occurs with some streets.
http://maps.googleapis.com/maps/api/geocode/json?address=1+Schorsa+Kyiv&sensor=false
{ "results" : [ { "address_components" : [ { "long_name" : "Kiev", "short_name" : "Kiev", "types" : [ "locality", "political" ] }, { "long_name" : "Kiev city", "short_name" : "Kiev city", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Kyiv city", "short_name" : "Kyiv city", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "Ukraine", "short_name" : "UA", "types" : [ "country", "political" ] } ], "formatted_address" : "Kiev, Kyiv city, Ukraine", "geometry" : { "bounds" : { "northeast" : { "lat" : 50.590798, "lng" : 30.825941 }, "southwest" : { "lat" : 50.213273, "lng" : 30.2394401 } }, "location" : { "lat" : 50.4501, "lng" : 30.5234 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 50.590798, "lng" : 30.825941 }, "southwest" : { "lat" : 50.213273, "lng" : 30.2394401 } } }, "partial_match" : true, "types" : [ "locality", "political" ] } ], "status" : "OK" }
If you click the link that I send it in a browser
{
"results" : [
{
"address_components" : [
{
"long_name" : "1",
"short_name" : "1",
"types" : [ "street_number" ]
},
{
"long_name" : "улица Щорса",
"short_name" : "улица Щорса",
"types" : [ "route" ]
},
{
"long_name" : "Деснянский район",
"short_name" : "Деснянский район",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Киев",
"short_name" : "Киев",
"types" : [ "locality", "political" ]
},
{
"long_name" : "город Киев",
"short_name" : "город Киев",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "город Киев",
"short_name" : "город Киев",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Украина",
"short_name" : "UA",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "улица Щорса, 1, Киев, город Киев, Украина",
"geometry" : {
"location" : {
"lat" : 50.52287399999999,
"lng" : 30.59185099999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 50.5242229802915,
"lng" : 30.59319998029149
},
"southwest" : {
"lat" : 50.5215250197085,
"lng" : 30.5905020197085
}
}
},
Add "Street" to the name of the street
http://maps.googleapis.com/maps/api/geocode/json?address=1+Schorsa+Street+Kyiv&sensor=false

Groovy JSON/GPath query

Given the following JSON, I would like to extract the postal_code (either long_name or short_name). I've used JsonSlurper to ingest it into a variable and have tried various queries using find/contains/etc. to grab the node that has "postal_code" in its "types" but haven't been able to figure it out. Any help is greatly appreciated.
{
"results" : [
{
"address_components" : [
{
"long_name" : "Jefferson Ave",
"short_name" : "Jefferson Ave",
"types" : [ "route" ]
},
{
"long_name" : "North Newport News",
"short_name" : "North Newport News",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Newport News",
"short_name" : "Newport News",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Virginia",
"short_name" : "VA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "23608",
"short_name" : "23608",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Jefferson Ave & Denbigh Blvd, Newport News, VA 23608, USA",
"geometry" : {
"location" : {
"lat" : 37.13852930,
"lng" : -76.52013079999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 37.13987828029151,
"lng" : -76.51878181970848
},
"southwest" : {
"lat" : 37.13718031970851,
"lng" : -76.52147978029149
}
}
},
"types" : [ "intersection" ]
}
],
"status" : "OK"
}
The following should find the node with a postal_code type. If the results or address_components ever have multiple list items you would have to adjust accordingly by replacing the indexed access with some iteration, but hopefully this helps.
import groovy.json.*
def text = '''
{
"results" : [
<omitted rest to save space>
....
}
'''
def json = new JsonSlurper().parseText(text)
def theNode = json.results[0]
.address_components
.find { it.types[0] == 'postal_code' }
assert '23608' == theNode.long_name