Google map get title of region - google-maps

I can not extract the title of natural region from Google Map geocoder (in Terrain mode). I can only display the name of nearest road from results[0] (red arrow). Is there a way to extract the nearest title displayed on the map (Green Arrow)? Is it available in results array?
var latlng = {lat: parseFloat(lat), lng: parseFloat(lng)};
var geocoder = new google. maps. Geocoder;
geocoder. geocode({'location': latlng}، function(results، status) {
if (status === 'OK') {
if (results[0]) {
$("#place"). val(results[0]. formatted_address);
} else {
//No title found
}
} else {
//window. alert('Geocoder failed due to: ' + status);
}
});

This is response on google maps results, in address_components you have a title of region or locality
I think that you want this results[0].address_components[2]
{
"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 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 Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

Related

Parse Geometry from Google APIs using $.ajax

I am struggling to retrieve the Latitude and Longitude from the Google geocode API. I'm not sure if its how I am printing the data on the page, or if its a problem with actually retrieving the JSON file. But when I run the page nothing shows up. Any help would be greatly appreciated.
<!DOCTYPE html>
<html>
<body>
<p id="output"></p>
<script>
$.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=yourkey',
type: 'get',
dataType: 'json',
success: function(data) {
if (data.status === 'OK') {
let test = $.parseJSON(data);
let lat = test.results[0].geometry.location.lat;
let lng = test.results[0].geometry.location.lng;
document.getElementById("output").innerHTML = lat + ", " + lng;
}
},
error: function(msg) {
document.getElementById("output").innerHTML = 'help';
}
});
</script>
</body>
EDIT: Here is the JSON that should be returned from the API call.
{
"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.4213117,
"lng" : -122.0839677
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4226606802915,
"lng" : -122.0826187197085
},
"southwest" : {
"lat" : 37.4199627197085,
"lng" : -122.0853166802915
}
}
},
"place_id" : "ChIJtYuu0V25j4ARwu5e4wwRYgE",
"plus_code" : {
"compound_code" : "CWC8+GC Mountain View, California, United States",
"global_code" : "849VCWC8+GC"
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

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"
}

Parsing Json Google GeoMaps

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.

Get Area Name from Street Address in Google Maps

I am developing a website where I plot statistics for an area and show it on Google Maps.
If you search Google Maps for "Harlem New York, NY USA" or "University Heights Bronx, NY USA", it shades that entire area. I will be showing this area and will figure out a way to show markers with statistics over this.
However, given the user will input addresses such as "500 W 133rd St, New York, NY 10027, USA", is it possible, using some Google Map API or something, to retrieve the Area Name (Harlem), from the mentioned street address?
You may use Google Maps Geocoding API
(in order to use it, you need a Gecoding API key)
For your example "500 W 133rd St, New York, NY 10027, USA" you may call the API in the following way:
https://maps.googleapis.com/maps/api/geocode/json?address=500%20W%20133rd%20St,%20New%20York,%20NY%2010027,%20USA
If you provide a proper key you should get a following JSON:
{
"results" : [
{
"address_components" : [
{
"long_name" : "500",
"short_name" : "500",
"types" : [ "street_number" ]
},
{
"long_name" : "West 133rd Street",
"short_name" : "W 133rd St",
"types" : [ "route" ]
},
{
"long_name" : "Upper Manhattan",
"short_name" : "Upper Manhattan",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Manhattan",
"short_name" : "Manhattan",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "New York",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New York County",
"short_name" : "New York County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Stany Zjednoczone",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "10027",
"short_name" : "10027",
"types" : [ "postal_code" ]
},
{
"long_name" : "7302",
"short_name" : "7302",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "500 W 133rd St, New York, NY 10027, Stany Zjednoczone",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.8176249,
"lng" : -73.9532346
},
"southwest" : {
"lat" : 40.8176127,
"lng" : -73.9532442
}
},
"location" : {
"lat" : 40.8176127,
"lng" : -73.9532442
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 40.81896778029149,
"lng" : -73.9518904197085
},
"southwest" : {
"lat" : 40.81626981970849,
"lng" : -73.95458838029151
}
}
},
"place_id" : "Eic1MDAgVyAxMzNyZCBTdCwgTmV3IFlvcmssIE5ZIDEwMDI3LCBVU0E",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
Which you can filter out for neighborhood, sublocality_level_1 or postal_code types and get the related long_name to search for the polygon.

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