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.
Related
I've seen this problem mentioned in other threads but they are often using older versions of google maps and are no longer valid.
If I use the request parameters and access the following url (https://maps.googleapis.com/maps/api/geocode/json?region=ca&address=10+York+Street%2C+Sydney%2C+NS%2C+Canada&key=API_KEY) directly in my browser I get the following response:
{
"results" : [
{
"address_components" : [
{
"long_name" : "10",
"short_name" : "10",
"types" : [ "street_number" ]
},
{
"long_name" : "York Street",
"short_name" : "York St",
"types" : [ "route" ]
},
{
"long_name" : "Sydney",
"short_name" : "Sydney",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Cape Breton Regional Municipality",
"short_name" : "Cape Breton Regional Municipality",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Nova Scotia",
"short_name" : "NS",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Canada",
"short_name" : "CA",
"types" : [ "country", "political" ]
},
{
"long_name" : "B1P 6B1",
"short_name" : "B1P 6B1",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "10 York St, Sydney, NS B1P 6B1, Canada",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 46.1427341,
"lng" : -60.19779020000001
},
"southwest" : {
"lat" : 46.1425438,
"lng" : -60.1980493
}
},
"location" : {
"lat" : 46.1426556,
"lng" : -60.19790510000001
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 46.14398793029149,
"lng" : -60.19657076970849
},
"southwest" : {
"lat" : 46.1412899697085,
"lng" : -60.1992687302915
}
}
},
"place_id" : "ChIJl_ddHCz7Z0sRQmXbPQOCT90",
"plus_code" : {
"compound_code" : "4RV2+3R Sydney, NS, Canada",
"global_code" : "87RX4RV2+3R"
},
"types" : [ "premise" ]
}
],
"status" : "OK"
}
Which is what I'm looking for. Great.
However if I make that same request in my program I get the wrong place, namely the same street address but in Ontario:
{
"results" : [
{
"address_components" : [
{
"long_name" : "10",
"short_name" : "10",
"types" : [ "street_number" ]
},
{
"long_name" : "York Street",
"short_name" : "York St",
"types" : [ "route" ]
},
{
"long_name" : "Old Toronto",
"short_name" : "Old Toronto",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Toronto",
"short_name" : "Toronto",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Toronto",
"short_name" : "Toronto",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Ontario",
"short_name" : "ON",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Canada",
"short_name" : "CA",
"types" : [ "country", "political" ]
},
{
"long_name" : "M5J 0E1",
"short_name" : "M5J 0E1",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "10 York St, Toronto, ON M5J 0E1, Canada",
"geometry" : {
"location" : {
"lat" : 43.64109759999999,
"lng" : -79.38122469999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 43.64244658029149,
"lng" : -79.37987571970848
},
"southwest" : {
"lat" : 43.6397486197085,
"lng" : -79.3825736802915
}
}
},
"place_id" : "ChIJBQZc3pM1K4gRIf5tNOFZWxI",
"plus_code" : {
"compound_code" : "JJR9+CG Toronto, ON, Canada",
"global_code" : "87M2JJR9+CG"
},
"types" : [ "establishment", "point_of_interest" ]
}
],
"status" : "OK"
}
If I change the address to 12 York Street in my app everything is working great and it shows me the NS address. I guess Google has it out for this one particular address. I make the request in my app with the following code:
$response = $this->client->request('GET', 'geocode/json?', [
'query' => [
'key' => Config::get('services.googleMaps')['key'],
'region' => $this->ccTLD,
'address' => urlencode($this->searchstr),
]
]);
$jsonString = $response->getBody()->getContents();
Log::info($jsonString); // Shows response quoted up above
Where region is 'ca', and address is '10+York+Street%2C+Sydney%2C+NS%2C+Canada'. The client here is a standard GuzzleHttp Client:
$this->client = new Client([
'base_uri' => 'https://maps.googleapis.com/maps/api/'
]);
I've tried using adding the bounds parameter with no difference at all, but the components parameter did ...do something for me. If I add '&components=administrative_area:NS' to the request in my browser (which starts off showing me NS) there is no change as expected.
If I then go back to my http client and add 'components' => 'administrative_area:NS', I would expect that to be the fix, right? Well no, I get ZERO RESULTS.
Out of curiosity I went back to the browser and tried to use administrative_area:ON to force it to show me the Ontario address. I instead was shown ZERO RESULTS in the browser now instead.
Does anyone know how to make my request in the client work correctly? or know why it is behaving completely differently in the first place? Is there a problem with GuzzleHttp I don't know about that could be responsible? I'm completely baffled here.
I am trying to decode an address "344 East Columbia Avenue, Pomona, 91767, CA" but it gives me Location type as Range_interpolated
{
"results" : [
{
"address_components" : [
{
"long_name" : "344",
"short_name" : "344",
"types" : [ "street_number" ]
},
{
"long_name" : "East Columbia Avenue",
"short_name" : "E Columbia Ave",
"types" : [ "route" ]
},
{
"long_name" : "Pomona",
"short_name" : "Pomona",
"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" ]
},
{
"long_name" : "91767",
"short_name" : "91767",
"types" : [ "postal_code" ]
},
{
"long_name" : "3952",
"short_name" : "3952",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "344 E Columbia Ave, Pomona, CA 91767, USA",
"geometry" : {
"location" : {
"lat" : 34.0684616,
"lng" : -117.7472136
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 34.0698105802915,
"lng" : -117.7458646197085
},
"southwest" : {
"lat" : 34.0671126197085,
"lng" : -117.7485625802915
}
}
},
"partial_match" : true,
"place_id" : "EikzNDQgRSBDb2x1bWJpYSBBdmUsIFBvbW9uYSwgQ0EgOTE3NjcsIFVTQSIbEhkKFAoSCVf7or0HLsOAEaYT6OuR5xZiENgC",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
Whereas on searching the same address in Google Maps, it is showing me location accurately.
Whereas on decoding "343 East Columbia Avenue, Pomona, 91767, CA" address gives me Rooftop location type.
As per Google documentation, Range_interpolated indicates that the returned result reflects an approximation (usually on a road) interpolated between two precise points (such as intersections). Interpolated results are generally returned when rooftop geocodes are unavailable for a street address.
Does anyone have an idea on accuracy of the Range_interpolated location type? How much accurate location it provides in meters?
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"
}
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"
}
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