Export Google map directions to lat/long - google-maps

I have multiple sets of lat/long coordinates that I am using in the Google directions API with way points to get directions following roads and paths. Each set of coordinates have about 100 points which I am converting to a usable number for the start, end and waypoints.
I would like to get the lat/long values from the directions which are returned from Google. Basically get each coordinate from each road segment.
Is this possible?

Yes, by using the direction API, you would get a respond like this:
{
"status": "OK",
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJ7cv00DwsDogRAMDACa2m4K8",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ69Pk6jdlyIcRDqM1KDY3Fpg",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJgdL4flSKrYcRnTpP0XQSojM",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJE9on3F3HwoAR9AhGJW_fL-I",
"types" : [ "locality", "political" ]
}
],
"routes": [ {
"summary": "I-40 W",
"legs": [ {
"steps": [ {
"travel_mode": "DRIVING",
"start_location": {
"lat": 41.8507300,
"lng": -87.6512600
},
"end_location": {
"lat": 41.8525800,
"lng": -87.6514100
},
"polyline": {
"points": "a~l~Fjk~uOwHJy#P"
},
"duration": {
"value": 19,
"text": "1 min"
},
"html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
"distance": {
"value": 207,
"text": "0.1 mi"
}
},
...
... additional steps of this leg
...
... additional legs of this route
"duration": {
"value": 74384,
"text": "20 hours 40 mins"
},
"distance": {
"value": 2137146,
"text": "1,328 mi"
},
"start_location": {
"lat": 35.4675602,
"lng": -97.5164276
},
"end_location": {
"lat": 34.0522342,
"lng": -118.2436849
},
"start_address": "Oklahoma City, OK, USA",
"end_address": "Los Angeles, CA, USA"
} ],
"copyrights": "Map data ©2010 Google, Sanborn",
"overview_polyline": {
"points": "a~l~Fjk~uOnzh#vlbBtc~#tsE`vnApw{A`dw#~w\\|tNtqf#l{Yd_Fblh#rxo#b}#xxSfytAblk#xxaBeJxlcBb~t#zbh#jc|Bx}C`rv#rw|#rlhA~dVzeo#vrSnc}Axf]fjz#xfFbw~#dz{A~d{A|zOxbrBbdUvpo#`cFp~xBc`Hk#nurDznmFfwMbwz#bbl#lq~#loPpxq#bw_#v|{CbtY~jGqeMb{iF|n\\~mbDzeVh_Wr|Efc\\x`Ij{kE}mAb~uF{cNd}xBjp]fulBiwJpgg#|kHntyArpb#bijCk_Kv~eGyqTj_|#`uV`k|DcsNdwxAott#r}q#_gc#nu`CnvHx`k#dse#j|p#zpiAp|gEicy#`omFvaErfo#igQxnlApqGze~AsyRzrjAb__#ftyB}pIlo_BflmA~yQftNboWzoAlzp#mz`#|}_#fda#jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC"
},
"warnings": [ ],
"waypoint_order": [ 0, 1 ],
"bounds": {
"southwest": {
"lat": 34.0523600,
"lng": -118.2435600
},
"northeast": {
"lat": 41.8781100,
"lng": -87.6297900
}
}
} ]
}
Where you can get the lat/lng of each turning points under steps. You can even decode the polyline to get the set of points which describe the road.

Related

Importing JSON file to MongoDB 'Operation passed in cannot be an Array' error

I'm trying to import a json dataset into my MongoDB collection and am getting this error, 'Operation passed in cannot be an Array'. I'm not too sure which array is casuing the issue of what I can chnage to fix this. This is the strucutre of my json file:
[
{
"business_status": "OPERATIONAL",
"formatted_address": "Divis Rd, Hannahstown, Belfast BT17 0NG",
"geometry":
[{
"location": {
"lat": 54.6054311,
"lng": -5.999924500000001
},
"viewport": {
"south": 54.60408127010727,
"west": -6.001274329892722,
"north": 54.60678092989272,
"east": -5.998574670107279
}
}],
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/park-71.png",
"icon_background_color": "#4DB546",
"icon_mask_base_uri": "https://maps.gstatic.com/mapfiles/place_api/icons/v2/tree_pinlet",
"name": "Black Mountain",
"photos": [
{
"height": 3024,
"html_attributions": [
"A Google User"
],
"width": 4032
}
],
"place_id": "ChIJB5sGQEYHYUgRDxrlOYU58As",
"plus_code": [{
"compound_code": "J242+52 Belfast",
"global_code": "9C6PJ242+52"
}],
"rating": 4.9,
"reference": "ChIJB5sGQEYHYUgRDxrlOYU58As",
"types": [
"park",
"point_of_interest",
"establishment"
],
"user_ratings_total": 49,
"html_attributions": []
}
]

Google maps textsearch API is returning empty result

I'm searching for top attractions in a 'CITY_NAME' using google textsearch API. Till yesterday it was returning data but when I'm trying now it is not returning results! whats going on here, why, I'm getting ZERO_RESULTS.
Ex:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=top attractions in bengaluru&language=en&key=MY_API_KEY
I'm expecting it to return results like below
{
"html_attributions": [],
"results": [
{
"formatted_address": "Mavalli, Bengaluru, Karnataka 560004, India",
"geometry": {
"location": {
"lat": 12.9507432,
"lng": 77.5847773
},
"viewport": {
"northeast": {
"lat": 12.9578032,
"lng": 77.59536344999999
},
"southwest": {
"lat": 12.9424072,
"lng": 77.57495644999999
}
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/generic_recreational-71.png",
"id": "8ae4a9c5e9c2b40e58bf0c947e5502ff9ef71c29",
"name": "Lalbagh Botanical Garden",
"opening_hours": {
"open_now": true
},
"photos": [
{
"height": 4032,
"html_attributions": [
"S.Ravindra Kumar"
],
"photo_reference": "CmRaAAAAYzPzHyHDleeTK4pM19jZ16SfDbG7lLZCrgr2wDEE_eNqDB5HdH670V0MXBRw03HH_HvFg0Rfe1dEZeG18NVVcxMUtpIOoHbN4a-csy0zqYRiJrTibapAiCxbYr8GKWrDEhBQAFmvTdSMKhoYbPlWmb-UGhSp70QBfrJgnUYSl5g_Ih24d80zIg",
"width": 3024
}
],
"place_id": "ChIJHdPykcEVrjsRIr4v35kLEY4",
"plus_code": {
"compound_code": "XH2M+7W Bengaluru, Karnataka, India",
"global_code": "7J4VXH2M+7W"
},
"rating": 4.4,
"reference": "ChIJHdPykcEVrjsRIr4v35kLEY4",
"types": [
"park",
"point_of_interest",
"establishment"
],
"user_ratings_total": 108174
},
{
.......
.......
}
]
}
But I'm getting the below response.
Response
{
"html_attributions" : [],
"results" : [],
"status" : "ZERO_RESULTS"
}
Any kind of help will much be appreciated.
Note that "attractions" is a very broad input; it can involve a wide variety of points of interests, place types etc. from museums to parks to zoos.
You should use a more specific place type in your queries. E.g.:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=top%20restaurants%20in%20bengaluru&language=en&key=KEY
Hope this answers your question.

Json object selection

I have a large JSON object returned from a Google Maps reverse geocode lookup.
in this object there are many possible location details returned.
Using jq how do I select the first returned location with a location_type "ROOFTOP" and get the formatted_address and place_id ?
in the below example the first entry has a location_type of "GEOMETRIC_CENTER"
The second entry has the "ROOFTOP" location_type. I want to ignore the entry with "GEOMETRIC_CENTER" and only return the fist entry with location_type "ROOFTOP"
Many thanks
Mike
[
{
"address_components": [
{
"long_name": "30",
"short_name": "30",
"types": [
"street_number"
]
},
{
"long_name": "Allée Jean de Lattre de Tassigny",
"short_name": "Allée Jean de Lattre de Tassigny",
"types": [
"route"
]
},
{
"long_name": "Montpellier",
"short_name": "Montpellier",
"types": [
"locality",
"political"
]
},
{
"long_name": "Hérault",
"short_name": "Hérault",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Occitanie",
"short_name": "Occitanie",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "France",
"short_name": "FR",
"types": [
"country",
"political"
]
},
{
"long_name": "34000",
"short_name": "34000",
"types": [
"postal_code"
]
}
],
"formatted_address": "30 Allée Jean de Lattre de Tassigny, 34000 Montpellier, France",
"geometry": {
"bounds": {
"northeast": {
"lat": 43.6097932,
"lng": 3.8817559
},
"southwest": {
"lat": 43.6094097,
"lng": 3.881321299999999
}
},
"location": {
"lat": 43.6095516,
"lng": 3.881559199999999
},
"location_type": "GEOMETRIC_CENTER",
"viewport": {
"northeast": {
"lat": 43.6109504302915,
"lng": 3.882887580291503
},
"southwest": {
"lat": 43.6082524697085,
"lng": 3.880189619708498
}
}
},
"place_id": "ChIJ13k0paCvthIRcTgwBrisc10",
"types": [
"premise"
]
},
{
"address_components": [
{
"long_name": "8",
"short_name": "8",
"types": [
"street_number"
]
},
{
"long_name": "Rue Michelet",
"short_name": "Rue Michelet",
"types": [
"route"
]
},
{
"long_name": "Montpellier",
"short_name": "Montpellier",
"types": [
"locality",
"political"
]
},
{
"long_name": "Hérault",
"short_name": "Hérault",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Occitanie",
"short_name": "Occitanie",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "France",
"short_name": "FR",
"types": [
"country",
"political"
]
},
{
"long_name": "34000",
"short_name": "34000",
"types": [
"postal_code"
]
}
],
"formatted_address": "8 Rue Michelet, 34000 Montpellier, France",
"geometry": {
"location": {
"lat": 43.60911189999999,
"lng": 3.8814264
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 43.61046088029149,
"lng": 3.882775380291502
},
"southwest": {
"lat": 43.60776291970849,
"lng": 3.880077419708498
}
}
},
"place_id": "ChIJ2UaGt6CvthIRJoNW7vS2Ibs",
"plus_code": {
"compound_code": "JV5J+JH Montpellier, France",
"global_code": "8FM5JV5J+JH"
},
"types": [
"street_address"
]
}
]
Rather than using .[0] to obtain the first object satisfying, it would be more efficient to use first/1. Here is a solution using first/1 and ..:
$ jq 'first(..|objects|select(.geometry.location_type == "ROOFTOP"))
| {formatted_address, place_id}' input.json
{
"formatted_address": "8 Rue Michelet, 34000 Montpellier, France",
"place_id": "ChIJ2UaGt6CvthIRJoNW7vS2Ibs"
}
Using firstin this way avoids having to compute the entire array before taking the first element.
Alternatively ...
Retrieving the first top-level object, without using ..:
first(.[] | select(.geometry.location_type=="ROOFTOP"))
Assuming the entries are enclosed in an array, you can use this jq filter that gives all objects having ROOFTOP as location:
jq '.[] | select(.geometry.location_type=="ROOFTOP")' file
If you want to select only the first one from all objects, use the following:
jq 'map(select(.geometry.location_type=="ROOFTOP"))|.[0]' file
And you only need need some of the values, pipe it another filter like this:
jq 'map(select(.geometry.location_type=="ROOFTOP"))|.[0]|.place_id, .formatted_address' file

Google Directions API start_address different from origin in input

My Google Directions API request is:
https://maps.googleapis.com/maps/api/directions/json?origin=41.43206,-81.38992&destination=41.43206,-81.48992&key=xxxxxxxx
The response I receive is:
{
...
"routes": [
{
"bounds": {
"northeast": {
"lat": 41.4358763,
"lng": -81.3897891
},
"southwest": {
"lat": 41.4238459,
"lng": -81.4910838
}
},
"copyrights": "Map data ©2016 Google",
"legs": [
{
"distance": {
"text": "6.5 mi",
"value": 10537
},
"duration": {
"text": "14 mins",
"value": 857
},
"end_address": "4743-4753 Country Ln, Warrensville Heights, OH 44128, USA",
"end_location": {
"lat": 41.4322651,
"lng": -81.4910838
},
"start_address": "45 Bell St, Chagrin Falls, OH 44022, USA",
"start_location": {
"lat": 41.4318382,
"lng": -81.3897891
},
"steps": [
{
"distance": {
"text": "0.1 mi",
"value": 191
},
"duration": {
"text": "1 min",
"value": 33
},
"end_location": {
"lat": 41.4311157,
"lng": -81.3918637
},
"html_instructions": "Head <b>southwest</b> on <b>Bell St</b> toward <b>Pleasant Dr</b>",
"polyline": {
"points": "_d{{FdmwoNFVRx#bA`EVbAVdA"
},
"start_location": {
"lat": 41.4318382,
"lng": -81.3897891
},
"travel_mode": "DRIVING"
},
...
{
"distance": {
"text": "0.2 mi",
"value": 317
},
"duration": {
"text": "1 min",
"value": 32
},
"end_location": {
"lat": 41.4322651,
"lng": -81.4910838
},
"html_instructions": "Turn <b>left</b> onto <b>Country Ln</b><div style=\"font-size:0.9em\">Destination will be on the left</div>",
"maneuver": "turn-left",
"polyline": {
"points": "ix{{FldkpNxJ?x#Ln#PnAX"
},
"start_location": {
"lat": 41.4350887,
"lng": -81.49079350000001
},
"travel_mode": "DRIVING"
}
],
"traffic_speed_entry": [],
"via_waypoint": []
}
],
"overview_polyline": {
"points": "_d{{FdmwoNvBvIVdAs#`#_Af#cCnAp#vCf#rBlAzEHd#`#nEFz#Cj#Or#{#nBUl#Q|#c#dGVZz#vAxDdGtBnDzCrEhAjCdAnCZ~Ap#bJJ`CAn#X\\\\r#hSx^hAxCn#pBXr#FbR?dCAz#ElAOhBe#xCkBvHqAtF]|BOhBG~BFpOFxBhBt\\r#vMBpA?zH?z\\CV?rV#V?nT?|QBvl##vI#lJAdI?fDDhODxPHvq#?hB_#EgA?m##EDyBBmCCqE?{FAkHB}E?cDEwBBcE?cKA_#?VdFl#fMp#pNd#fK#fJApCxJ?hB^nAX"
}
...
}
Why is the start address in the sole leg different from the origin specified in the request? These two points are 6.5 miles apart.
Same is true for end_address and destination specified in the request.
Thanks,
Abhishek Batra
The DirectionsService reverse geocodes any location provided as coordinates, finds the nearest location on the road and uses that as the origin (or destination).
Your two points both happen to be in the middle of water.

Finding location by zip and object name

I wanted to create app that will longitude, latitude by zip and name of object (for example mcdonalds). I tried to do that via google maps geocoder but my idea was wrong because geocoder returned weird results. Maybe someone know a service or strategy of getting location by zip/name please let me know.
You can test out the Google Geocoder by using the same search string in the maps.google.com search box. For example "McDonalds 2000 Australia", returns appropriate geocoding results. Are you including the country? That might clarify the search results.
reverse-geocoding from your tags means geolocation with given latitude and longitude.
If you want to geocode something you should just use Google Maps Geocode API.
http://maps.google.com/maps/api/geocode/json?address=Sydney&sensor=false
In result you have:
{
"status": "OK",
"results": [ {
"types": [ "locality", "political" ],
"formatted_address": "Sydney Nowa Południowa Walia, Australia",
"address_components": [ {
"long_name": "Sydney",
"short_name": "Sydney",
"types": [ "locality", "political" ]
}, {
"long_name": "Nowa Południowa Walia",
"short_name": "NSW",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "Australia",
"short_name": "AU",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": -33.8671390,
"lng": 151.2071140
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": -33.8764033,
"lng": 151.1911066
},
"northeast": {
"lat": -33.8578737,
"lng": 151.2231214
}
},
"bounds": {
"southwest": {
"lat": -33.8797030,
"lng": 151.1970330
},
"northeast": {
"lat": -33.8559920,
"lng": 151.2229770
}
}
}
} ]
}
In results array you have result objects. Coordinates are just in geometry->location.
Nothing simplier.
Here is reference: http://code.google.com/intl/pl-PL/apis/maps/documentation/geocoding/#GeocodingRequests