How do I obtain shape of a road represented by a PlaceId - google-maps

If I have a PlaceId that represents a road, how can I obtain the shape of the road to highlight it on the map? The Places API only returns a single location in the geometry field for roads.
For example given PlaceID: ChIJ8R8rpjCuEmsRKxCMhhWn9Kg
I get the following response. Notice the types field has value of "route", but the geometry field only provides single location instead of a path.
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Darling Dr",
"short_name" : "Darling Dr",
"types" : [ "route" ]
},
{
"long_name" : "Sydney",
"short_name" : "Sydney",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New South Wales",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Australia",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "2000",
"short_name" : "2000",
"types" : [ "postal_code" ]
}
],
"adr_address" : "\u003cspan class=\"street-address\"\u003eDarling Dr\u003c/span\u003e, \u003cspan class=\"locality\"\u003eSydney\u003c/span\u003e \u003cspan class=\"region\"\u003eNSW\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e2000\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
"formatted_address" : "Darling Dr, Sydney NSW 2000, Australia",
"geometry" : {
"location" : {
"lat" : -33.8718136,
"lng" : 151.1984465
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "82a04f4f0548a2e7c467b71ed9c2a3a1615dac50",
"name" : "Darling Dr",
"place_id" : "ChIJ8R8rpjCuEmsRKxCMhhWn9Kg",
"reference" : "CpQBhAAAAA4dsRcdqFcTxsVmwWs1ZWWSg9XdclQgRHhLYT2XJwEIU8BOXoS3D4cM58oNhcND-bJU7k7nmLvgHZFep405LokQpkTu0yUPQtrrgNonLRVPdLy4j9Z0M7iBpmGkPN6BuDntYu6JG9urfnIJZRdIvpibFI9QxzP7uheEnJQtI7S8Aoxth99pVl5OvyySDSfn5hIQ4Nf4YLpRjiuY34ND9qvTyBoUr24qn9M1aFa6GoO46wrfW2a12DA",
"scope" : "GOOGLE",
"types" : [ "route" ],
"url" : "https://maps.google.com/maps/place?q=Darling+Dr,+Sydney+NSW+2000,+Australia&ftid=0x6b12ae30a62b1ff1:0xa8f4a715868c102b",
"vicinity" : "Sydney"
},
"status" : "OK"
}

This functionality is not supported. What you can do is create a map on MyMap and add a driving route for the path you want to highlight. Then, you can save it and export it as a KML layer. If you wanted to do this programmatically, you could use the Directions API to get the polyline for the route.

Related

Google maps API gives different results in browser vs made from http client, components parameter behaves different in browser vs app as well

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.

Geocoding API is not giving Rooftop location even if address is accurate in Google Maps

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?

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.

Google Places API results do not match Google search

In my data I have found many examples like the following. If you search Google for "Cheyney University PA", you get the following information:
Address: 1837 University Cir, Cheyney, PA 19319
Phone: (610) 399-2000
However, when doing a search using the following:
my $pid_url = Mojo::URL->new('https://maps.googleapis.com/maps/api/place/textsearch/json');
$pid_url->query({query => $query, key => 'removed'});
my $place_id = $ua->get($pid_url)->res->json->{results}->[0]->{place_id};
# Query = https://maps.googleapis.com/maps/api/place/textsearch/json?query=Cheyney+University+PA&key=removed
# Place ID returned = ChIJ5wiPkSvuxokR2rwaVUTMwks
$deets_url = Mojo::URL->new('https://maps.googleapis.com/maps/api/place/details/json');
$deets_url->query({placeid => $place_id, key => 'removed'});
say $ua->get($deets_url)->res->body;
I get the following back from Google:
{
"result" : {
"address_components" : [
{
"long_name" : "Cheyney",
"short_name" : "Cheyney",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Thornbury",
"short_name" : "Thornbury",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Delaware County",
"short_name" : "Delaware County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Pennsylvania",
"short_name" : "PA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Cheyney, PA, USA",
"name" : "Cheyney",
"place_id" : "ChIJ5wiPkSvuxokR2rwaVUTMwks",
"scope" : "GOOGLE",
"types" : [ "locality", "political" ],
"url" : "https://maps.google.com/maps/place?q=Cheyney,+PA,+USA&ftid=0x89c6ee2b918f08e7:0x4bc2cc44551abcda",
"vicinity" : "Cheyney"
},
"status" : "OK"
}
(I removed some of the output for clarity.) Notice that in the results the street address components are missing (1837 University Cir), as well as the phone number. I have found many instances like this, and I am trying to figure out why my queries are not returning the same results as what I get when I Google the information.
Here's another example:
Google Results:
Address: 1320 S Dixie Hwy, Coral Gables, FL 33124
Phone: (305) 284-2211
Places API Results:
{
"html_attributions" : [],
"results" : [
{
"formatted_address" : "University of Miami, Coral Gables, FL 33146, USA",
"geometry" : {
"location" : {
"lat" : 25.7216374,
"lng" : -80.2792843
},
"viewport" : {
"northeast" : {
"lat" : 25.7316894,
"lng" : -80.26327689999999
},
"southwest" : {
"lat" : 25.7115846,
"lng" : -80.29529169999999
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "7eb529034293a6eb760e990e034a4314d83edcf7",
"name" : "University of Miami",
"place_id" : "ChIJn9K4n_C32YgRqR37uAXB5IM",
"reference" : "CqQBnAAAACffq3Xp3JUFowUDlEsge90htrhAoUES9tGmsB919iNQCxgldtq4khP4f_gCAju90qGY1IEUH52hZl_GP_H6xJC5z22EceCfprWEHFhhXKsW23Y9EeV24svXC-PRYnOyQaXZmbQFCeUlsmKWB6570e-8W_w31muZKwFd4hVPdeeqO45zH2zp7CAuSrHjBr0bSiOQufEB59ibxG4QcuwenSASEHvq1xMcCCtMUsFcyLYsrOIaFN0vg6S680hJay1jDSrZ686Mt9Gn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
}
],
"status" : "OK"
}
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "University of Miami",
"short_name" : "University of Miami",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Coral Gables",
"short_name" : "Coral Gables",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Miami-Dade County",
"short_name" : "Miami-Dade County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Florida",
"short_name" : "FL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "33146",
"short_name" : "33146",
"types" : [ "postal_code" ]
}
],
"adr_address" : "University of Miami, \u003cspan class=\"locality\"\u003eCoral Gables\u003c/span\u003e, \u003cspan class=\"region\"\u003eFL\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e33146\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUSA\u003c/span\u003e",
"formatted_address" : "University of Miami, Coral Gables, FL 33146, USA",
"geometry" : {
"location" : {
"lat" : 25.7216374,
"lng" : -80.2792843
},
"viewport" : {
"northeast" : {
"lat" : 25.7316894,
"lng" : -80.26327689999999
},
"southwest" : {
"lat" : 25.7115846,
"lng" : -80.29529169999999
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "7eb529034293a6eb760e990e034a4314d83edcf7",
"name" : "University of Miami",
"place_id" : "ChIJn9K4n_C32YgRqR37uAXB5IM",
"reference" : "CqQBlAAAAFeKC7LbGqAr7-LnBXMqbMIhuyuUh22XBMRO11hADojDclomC-InGocKSStmZ8S-S1UVWC1qHAsZi3XSp7PiDOFBgZVMqhm5kr87CfEAJmPdwsmZ6VEg5FxXh5LZmSSZdFgH0UR0OVGLL5M17SmWZp4W3Die8UNa5yCMyV5CP0T0VWmvqCb54qJR6mlyIx1itiLzRp1XhTs8_UmEt9pjIkYSEGrAqS6TWo2G1WBOrDO0YSAaFO4FfqvmFabJLia36vKL0XTz23Mu",
"scope" : "GOOGLE",
"types" : [ "sublocality_level_1", "sublocality", "political" ],
"url" : "https://maps.google.com/maps/place?q=University+of+Miami,+Coral+Gables,+FL+33146,+USA&ftid=0x88d9b7f09fb8d29f:0x83e4c105b8fb1da9",
"vicinity" : "University of Miami"
},
"status" : "OK"
}
It looks like you are wanting to use Google Maps to do some sort of address validation and parsing. The short story is, unfortunately Google Maps cannot be replied upon to return parsed address components consistently. What Google Maps does do really well is geocode - that is translate input into lat/long to be displayed on a map. You could perhaps try reverse geocoding (i.e. giving Google lat/long) to see what that returns, but I'm pretty sure you will get similar results.
I have also wanted and tried in vain to use Google Maps similarly in the past myself. After much testing against various international, real world datasets, I concluded that Google couldn't be used for validation any more than (a) getting lat/long (b) a general yes/no that address sort of looks ok. I have also tried some other Geocoding providers and later settled on GeocodeFarm when Google proved too expensive once my sites outgrew what they previously provided for free. My general conclusion was the same though - I had to stick to fairly traditional address data entry forms without 'fullproof' validation, otherwise users would be told addresses were faulty that were actually valid. GeocodeFarm did "pretty print" localized addresses quite well though (i.e. almost suitable for putting as a mailing label on an envelope), but it wasn't parsed into components for storage into a typical relational database schema.
Doing some more research to verify my own experiences, I found this and this to be useful. These links also point towards a service called 'SmartyStreets' - I cannot vouch for it however, and have zero experience with it.
Hope that helps!

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