Google distance martix order by distance - google-maps

When I fetchdata from https://maps.googleapis.com/maps/api/distancematrix/json?origins=10010&destinations=10011|10012|10013&key=AIzaSyBOsjeskUwOLYQZntsv7_34gVZ3xgcXko0
it returns :
{
"destination_addresses" : [
"New York, NY 10011, USA",
"New York, NY 10012, USA",
"New York, NY 10013, USA"
],
"origin_addresses" : [ "New York, NY 10010, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "3.4 km",
"value" : 3389
},
"duration" : {
"text" : "19 mins",
"value" : 1156
},
"status" : "OK"
},
{
"distance" : {
"text" : "2.6 km",
"value" : 2632
},
"duration" : {
"text" : "13 mins",
"value" : 767
},
"status" : "OK"
},
{
"distance" : {
"text" : "9.5 km",
"value" : 9545
},
"duration" : {
"text" : "19 mins",
"value" : 1143
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
But I need data order by distance. First showing small then large.
Is it possible?

Sure!
1. Parse the JSON data to an object (JSON.parse);
2. Sort the elements array using code as below:
// given els is one of the elements extracted from this object:
let sorted = els.sort((a, b) => a.distance.value - b.distance.value);

Related

Google Directions API transit returns always "ZERO_RESULTS"

i'm developing an app that use google API to obtain a transport solution to reach a location at a desired time.
My code that call google API work perfectly with "driving" and "walking" travel mode, but with "transit" and "bicycling" the response is always marked with the status "ZERO_RESULTS" and no routes is calculated.
This, by the google documentation, means that transit and bicycling are not available in the country where the solution must be calculated.
I tried with several country and I always had the same result, I tried also with the Google example (for transit) in the overview and it doesn't work.
The parameters that I pass with the HTTPS request are: origin, destination, travel mode, arrival_time.
The first thing that I tried is to change arrival_time, in case the value is referred to the past or to a too distant future, but it doesn't work.
In case I don't pass "arrival_time" the response is perfect, but I don't understand because in the Google Documentation is clear that arrival_time is a possible argument of the https request also with the transit travel mode. Without this argument the request is useless for my goal.
The curious thing is that if i pass "transit" (with arrival or departure time) the response is this:
{
"available_travel_modes" : [ "DRIVING", "WALKING" ],
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJu46S-ZZhLxMROG5lkwZ3D7k",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ53USP0nBhkcRjQ50xhPN_zw",
"types" : [ "locality", "political" ]
}
],
"routes" : [],
"status" : "ZERO_RESULTS"
}
And if I pass "bicycling" (with all the other parameters unchanged):
{
"available_travel_modes" : [ "DRIVING", "WALKING", "TRANSIT" ],
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJu46S-ZZhLxMROG5lkwZ3D7k",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ53USP0nBhkcRjQ50xhPN_zw",
"types" : [ "locality", "political" ]
}
],
"routes" : [],
"status" : "ZERO_RESULTS"
}
So in the second case the API said that TRANSIT is available and this doesn't make sense at all!
The format of the location that I use is geographical coordinates to avoid cases of ambiguity.
Hope that I give enough information to make you able to help me.
EDIT: sorry, i was thinking that my post was clear enough.
I use Google Directions API: https://developers.google.com/maps/documentation/directions/
My problem is with "transit" and "bicycling" as travel mode and is not a code problem, but the problem appears at the level of https response, even with the google example for "transit" that can be found on the overview i obtain a "ZERO_RESULTS" response.
This is the URL of the example:
https://maps.googleapis.com/maps/api/directions/json?origin=75+9th+Ave+New+York,+NY&destination=MetLife+Stadium+1+MetLife+Stadium+Dr+East+Rutherford,+NJ+07073&mode=transit&arrival_time=1391374800&key=YOUR_API_KEY
And here is the two URL that get me the two response that I copied above:
with transit:
https://maps.googleapis.com/maps/api/directions/json?origin=Rome&destination=Milan&mode=transit&arrival_time=1513162800000&key=++API_KEY++
and with bicycling:
https://maps.googleapis.com/maps/api/directions/json?origin=Rome&destination=Milan&mode=transit&arrival_time=1513162800000&key=++API_KEY++
in the code I use for origin and destination geographical coordinates as here:
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835,12.496365500000024&destination=45.4642035,9.189981999999986&mode=transit&arrival_time=1513162800000&key=++API_KEY++
the only difference is that google do not recognize geocoded waypoints:
{
"available_travel_modes" : [ "DRIVING", "WALKING" ],
"geocoded_waypoints" : [ {}, {} ],
"routes" : [],
"status" : "ZERO_RESULTS"
}
If I use "driving" as travel mode this worked:
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835,12.496365500000024&destination=45.4642035,9.189981999999986&mode=driving&arrival_time=1513162800000&key=++API_KEY++
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "EjJQaWF6emEgZGVsbGEgUmVwdWJibGljYSwgMTAsIDAwMTg1IFJvbWEgUk0sIEl0YWxpYQ",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJC5u9LazGhkcRXAZQFNDTpKc",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 45.4731019,
"lng" : 12.6177051
},
"southwest" : {
"lat" : 41.9028523,
"lng" : 9.1839517
}
},
"copyrights" : "Dati mappa ©2017 Google",
"legs" : [
{
"distance" : {
"text" : "573 km",
"value" : 572600
},
"duration" : {
"text" : "5 ore 51 min",
"value" : 21069
},
"end_address" : "Via Silvio Pellico, 2, 20121 Milano MI, Italia",
"end_location" : {
"lat" : 45.46495119999999,
"lng" : 9.1892874
},
"start_address" : "Piazza della Repubblica, 10, 00185 Roma RM, Italia",
"start_location" : {
"lat" : 41.9028523,
"lng" : 12.4964704
},
"steps" : [
{
"distance" : {
"text" : "93 m",
"value" : 93
},
"duration" : {
"text" : "1 min",
"value" : 19
},
"end_location" : {
"lat" : 41.9035781,
"lng" : 12.4959908
},
"html_instructions" : "Procedi in direzione \u003cb\u003enord\u003c/b\u003e da \u003cb\u003ePiazza della Repubblica\u003c/b\u003e verso \u003cb\u003eVia Giuseppe Romita\u003c/b\u003e",
"polyline" : {
"points" : "ycw~F}ugkAU?I#E#E#a#RG#EDED_#d#OP"
},
"start_location" : {
"lat" : 41.9028523,
"lng" : 12.4964704
},
"travel_mode" : "DRIVING"
},
// cut
{
"distance" : {
"text" : "52 m",
"value" : 52
},
"duration" : {
"text" : "1 min",
"value" : 14
},
"end_location" : {
"lat" : 45.4665738,
"lng" : 9.1889772
},
"html_instructions" : "Continua su \u003cb\u003eVia Santa Margherita\u003c/b\u003e",
"polyline" : {
"points" : "ogotGoxaw#`#`#j#h#"
},
"start_location" : {
"lat" : 45.4669622,
"lng" : 9.189359399999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,2 km",
"value" : 192
},
"duration" : {
"text" : "1 min",
"value" : 87
},
"end_location" : {
"lat" : 45.46495119999999,
"lng" : 9.1892874
},
"html_instructions" : "Svolta a \u003cb\u003esinistra\u003c/b\u003e e prendi \u003cb\u003eVia Silvio Pellico\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "aeotGcvaw#Xw#DCBAxC#~A?d#A"
},
"start_location" : {
"lat" : 45.4665738,
"lng" : 9.1889772
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "//collapsed//"
},
"summary" : "A1/E35 e A1",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
and if I use "transit" without an arrival time it works:
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835,12.496365500000024&destination=45.4642035,9.
189981999999986&mode=transit&key=++API_KEY++
and:
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "EjJQaWF6emEgZGVsbGEgUmVwdWJibGljYSwgMTAsIDAwMTg1IFJvbWEgUk0sIEl0YWxpYQ",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJRYxePKzGhkcRsgPwamn2Pfo",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 45.4969308,
"lng" : 12.5528041
},
"southwest" : {
"lat" : 41.892252,
"lng" : 9.188611
}
},
"copyrights" : "Dati mappa ©2017 Google",
"legs" : [
{
"arrival_time" : {
"text" : "13:55",
"time_zone" : "Europe/Rome",
"value" : 1513169733
},
"departure_time" : {
"text" : "10:10",
"time_zone" : "Europe/Rome",
"value" : 1513156231
},
"distance" : {
"text" : "571 km",
"value" : 571467
},
"duration" : {
"text" : "3 ore 45 min",
"value" : 13502
},
"end_address" : "Piazza del Duomo, 1, 20121 Milano MI, Italia",
"end_location" : {
"lat" : 45.4639037,
"lng" : 9.188611
},
"start_address" : "Piazza della Repubblica, 10, 00185 Roma RM, Italia",
"start_location" : {
"lat" : 41.9028429,
"lng" : 12.4964793
},
"steps" : [
{
"distance" : {
"text" : "0,1 km",
"value" : 102
},
"duration" : {
"text" : "1 min",
"value" : 87
},
"end_location" : {
"lat" : 41.902193,
"lng" : 12.4958041
},
"html_instructions" : "Cammina fino a Repubblica",
"polyline" : {
"points" : "wcw~F_vgkA#CBADABA#A#?B?D?B?B#B?B#BBB##BBBDH#D#H##?D?BDRBL?#DXBLBJDHBFVM"
},
"start_location" : {
"lat" : 41.9028429,
"lng" : 12.4964793
},
"steps" : [
{
"distance" : {
"text" : "54 m",
"value" : 54
},
"duration" : {
"text" : "1 min",
"value" : 49
},
"end_location" : {
"lat" : 41.9024775,
"lng" : 12.4962611
},
"html_instructions" : "Procedi in direzione \u003cb\u003esudest\u003c/b\u003e verso \u003cb\u003ePiazza della Repubblica\u003c/b\u003e",
"polyline" : {
"points" : "wcw~F_vgkA#CBADABA#A#?B?D?B?B#B?B#BBB##BBBDH#D#H##?D?B"
},
"start_location" : {
"lat" : 41.9028429,
"lng" : 12.4964793
},
"travel_mode" : "WALKING"
},
{
"distance" : {
"text" : "48 m",
"value" : 48
},
"duration" : {
"text" : "1 min",
"value" : 38
},
"end_location" : {
"lat" : 41.902193,
"lng" : 12.4958041
},
"html_instructions" : "Esci dalla rotonda e prendi \u003cb\u003ePiazza della Repubblica\u003c/b\u003e",
"polyline" : {
"points" : "oaw~FstgkADRBL?#DXBLBJDHBFVM"
},
"start_location" : {
"lat" : 41.9024775,
"lng" : 12.4962611
},
"travel_mode" : "WALKING"
}
],
"travel_mode" : "WALKING"
},
{
"distance" : {
"text" : "0,4 km",
"value" : 437
},
"duration" : {
"text" : "1 min",
"value" : 46
},
"end_location" : {
"lat" : 41.9011732,
"lng" : 12.4996019
},
"html_instructions" : "Metropolitana verso Anagnina",
"polyline" : {
"points" : "u_w~FwqgkAeBIBoA#I|ByM~CmELR"
},
"start_location" : {
"lat" : 41.902193,
"lng" : 12.4958041
},
"transit_details" : {
"arrival_stop" : {
"location" : {
"lat" : 41.9011732,
"lng" : 12.4996019
},
"name" : "Termini"
},
"arrival_time" : {
"text" : "10:12",
"time_zone" : "Europe/Rome",
"value" : 1513156353
},
"departure_stop" : {
"location" : {
"lat" : 41.902193,
"lng" : 12.4958041
},
"name" : "Repubblica"
},
"departure_time" : {
"text" : "10:11",
"time_zone" : "Europe/Rome",
"value" : 1513156307
},
"headsign" : "Anagnina",
"line" : {
"agencies" : [
{
"name" : "Atac",
"phone" : "011 39 06 57003",
"url" : "http://www.atac.roma.it/"
}
],
"color" : "#e27439",
"name" : "Metro A",
"short_name" : "MEA",
"url" : "http://muovi.roma.it/percorso/js?query=MEA&cl=1",
"vehicle" : {
"icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/subway2.png",
"local_icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/it-metro.png",
"name" : "Metropolitana",
"type" : "SUBWAY"
}
},
"num_stops" : 1
},
"travel_mode" : "TRANSIT"
},
{
"distance" : {
"text" : "2,6 km",
"value" : 2641
},
"duration" : {
"text" : "6 min",
"value" : 330
},
"end_location" : {
"lat" : 45.4639037,
"lng" : 9.188611
},
"html_instructions" : "Metropolitana verso San Donato",
"polyline" : {
"points" : "//collapsed//"
},
"start_location" : {
"lat" : 45.4844397,
"lng" : 9.202612799999999
},
"transit_details" : {
"arrival_stop" : {
"location" : {
"lat" : 45.4639037,
"lng" : 9.188611
},
"name" : "Duomo"
},
"arrival_time" : {
"text" : "13:55",
"time_zone" : "Europe/Rome",
"value" : 1513169730
},
"departure_stop" : {
"location" : {
"lat" : 45.4844397,
"lng" : 9.202612799999999
},
"name" : "Centrale FS"
},
"departure_time" : {
"text" : "13:50",
"time_zone" : "Europe/Rome",
"value" : 1513169400
},
"headsign" : "San Donato",
"line" : {
"agencies" : [
{
"name" : "COMUNE DI MILANO",
"phone" : "011 39 02 0202",
"url" : "http://www.muoversi.milano.it/"
}
],
"color" : "#ffea00",
"name" : "M3 - Linea Gialla",
"short_name" : "3",
"vehicle" : {
"icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/subway2.png",
"local_icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/it-metro.png",
"name" : "Metropolitana",
"type" : "SUBWAY"
}
},
"num_stops" : 4
},
"travel_mode" : "TRANSIT"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "//collapsed//"
},
"summary" : "",
"warnings" : [
"Le indicazioni per tragitti a piedi sono in versione beta. Presta attenzione – questo percorso potrebbe non disporre di marciapiede o aree pedonali."
],
"waypoint_order" : []
}
],
"status" : "OK"
}
The issue in your request is the value that you pass as an arrival_time parameter. Please note that this value must be in seconds:
arrival_time — Specifies the desired time of arrival for transit directions, in seconds since midnight, January 1, 1970 UTC. You can specify either departure_time or arrival_time, but not both. Note that arrival_time must be specified as an integer.
https://developers.google.com/maps/documentation/directions/intro#DirectionsRequests
You are passing milliseconds 1513162800000 that corresponds to 03/22/49920 # 8:00am (UTC), the correct value is 1513162800 that corresponds to 12/13/2017 # 11:00am (UTC).
The request should be:
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835%2C12.496365500000024&destination=45.4642035%2C9.189981999999986&mode=transit&arrival_time=1513162800&key=YOUR_API_KEY
The same request in Directions calculator:
https://directionsdebug.firebaseapp.com/?origin=41.9027835%2C12.496365500000024&destination=45.4642035%2C9.189981999999986&mode=transit&arrival_time=1513162800
I hope this helps!
In case it helps someone else, I also found that setting a departure_time too far in the future (more than a year in my case) led to a "ZERO_RESULTS" response. Making it closer to the present fixed this.

How to get parse json - SwiftyJSON

I'm using SwiftyJSON.
I have a json string like this:
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJkcemZ9r9jT8RCoy7cipVk3Q",
"types" : [ "route" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJzYyVGtcFjj8RMoV0IaoNhzA",
"types" : [ "route" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 35.7884236,
"lng" : 51.43490389999999
},
"southwest" : {
"lat" : 35.7056256,
"lng" : 51.3073962
}
},
"copyrights" : "Map data ©2017 Google",
"legs" : [
{
"distance" : {
"text" : "20.9 km",
"value" : 20893
},
"duration" : {
"text" : "32 mins",
"value" : 1936
},
"end_address" : "Tehran Province, Tehran, Hossein, Iran",
"end_location" : {
"lat" : 35.7883586,
"lng" : 51.43490389999999
},
"start_address" : "Tehran Province, Tehran, Kooy-e-Bimeh, Rostam, Iran",
"start_location" : {
"lat" : 35.7056307,
"lng" : 51.3167437
},
"steps" : [
{
"distance" : {
"text" : "59 m",
"value" : 59
},
"duration" : {
"text" : "1 min",
"value" : 21
},
"end_location" : {
"lat" : 35.70562779999999,
"lng" : 51.31739839999999
},
"html_instructions" : "Head \u003cb\u003eeast\u003c/b\u003e on \u003cb\u003eRostam\u003c/b\u003e toward \u003cb\u003e4th Bimeh St\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Burgerich (on the right)\u003c/div\u003e",
"polyline" : {
"points" : "ew|xEsxexH?W?w#?s#"
},
"start_location" : {
"lat" : 35.7056307,
"lng" : 51.3167437
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 km",
"value" : 135
},
"duration" : {
"text" : "1 min",
"value" : 30
},
"end_location" : {
"lat" : 35.7868183,
"lng" : 51.4303372
},
"html_instructions" : "Continue onto \u003cb\u003eSharifi Manesh St\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Embassy of Switzerland In Tehran (on the left)\u003c/div\u003e",
"polyline" : {
"points" : "cllyEyy{xHSIQSIIYWk#[_Ak#YQ"
},
"start_location" : {
"lat" : 35.7857769,
"lng" : 51.4295729
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.3 km",
"value" : 304
},
"duration" : {
"text" : "1 min",
"value" : 51
},
"end_location" : {
"lat" : 35.7852475,
"lng" : 51.4330904
},
"html_instructions" : "\u003cb\u003eSharifi Manesh St\u003c/b\u003e turns \u003cb\u003eright\u003c/b\u003e and becomes \u003cb\u003eElahiyeh St\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by باشگاه الهیه (on the left)\u003c/div\u003e",
"polyline" : {
"points" : "srlyEs~{xHpBwEdA}BxAqCf#}#"
},
"start_location" : {
"lat" : 35.7868183,
"lng" : 51.4303372
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.4 km",
"value" : 359
},
"duration" : {
"text" : "1 min",
"value" : 70
},
"end_location" : {
"lat" : 35.7884241,
"lng" : 51.4337388
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eKhazar St\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by MaryamHair (on the left)\u003c/div\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "yhlyEyo|xHwBYwCg#wA[}AQuAM}AC"
},
"start_location" : {
"lat" : 35.7852475,
"lng" : 51.4330904
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 km",
"value" : 105
},
"duration" : {
"text" : "1 min",
"value" : 39
},
"end_location" : {
"lat" : 35.7883586,
"lng" : 51.43490389999999
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e at هنرستان مفتح onto \u003cb\u003eHossein\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "s|lyE{s|xH?AJeF"
},
"start_location" : {
"lat" : 35.7884241,
"lng" : 51.4337388
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "ew|xEsxexH?oA"
},
"summary" : "Hakim Expy",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
what I want to get is routes->legs->distance->text.
my codes:
let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
if let data = json.data(using: String.Encoding.utf8) {
let json = JSON(data: data)
print("--->\(json["routes"]["legs"]["distance"]["text"].string)")
}
print returns --->nil.
routes and legs are arrays, you have to get the first entry first:
let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
if let data = json.data(using: String.Encoding.utf8) {
let json = JSON(data: data)
if let route = json["routes"].first {
if let leg = route["legs"].first {
print("--->\(leg["distance"]["text"].string)")
}
}
}
p.s. You may need to do some logic checking if you don't want the first entry in the arrays.

Same request to Google Maps API works differently on various machines

I registered API key, and compossed the following url with it:
https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=Belarus,Minsk,Kuprevicha,1/2&destinations=Belarus,Minsk,Kurchatova,8&key=AIzaSyBngdHtfv-gbJkhKbsgJMqU5T-BnacZuPI
I am getting responce where street information were erased:
{
"destination_addresses" : [ "Minsk, Belarus" ],
"origin_addresses" : [ "Minsk, Belarus" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1 m",
"value" : 0
},
"duration" : {
"text" : "1 min",
"value" : 0
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
My colleague sitting next to me firing same request recieves a correct full responce.
{
"destination_addresses" : [ "ул. Курчатова 8, Минск, Беларусь" ],
"origin_addresses" : [
"ул. Академика Купревича 1/2, Минск 220141, Беларусь"
],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "24,5 км",
"value" : 24526
},
"duration" : {
"text" : "20 мин.",
"value" : 1196
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
What is the reason of this weird behavior?

How can I find place address using google place search API

Using google place search API, I am able to find place name as shown below. However I am not able to get the address of the place.
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
alert(place.name);
If you are using the Google Place API and have the Server key for that, you can request the data from the API as follows:
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AddYourOwnKeyHere
Where the "place_id" is the parameter which is responsible for fetching out the details of that particular place. The output is in form of JSON/XML consisting of long name, short name, phone number, time, address, URL etc. You can parse the address and displace in your website/app.
For more details click this link
JSON format:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "48",
"short_name" : "48",
"types" : [ "street_number" ]
},
{
"long_name" : "Pirrama Road",
"short_name" : "Pirrama Road",
"types" : [ "route" ]
},
{
"long_name" : "Pyrmont",
"short_name" : "Pyrmont",
"types" : [ "locality", "political" ]
},
{
"long_name" : "NSW",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "AU",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "2009",
"short_name" : "2009",
"types" : [ "postal_code" ]
}
],
"events" : [
{
"event_id" : "9lJ_jK1GfhX",
"start_time" : 1293865200,
"summary" : "<p>A visit from author John Doe, who will read from his latest book.</p>
<p>A limited number of signed copies will be available.</p>",
"url" : "http://www.example.com/john_doe_visit.html"
}
],
"formatted_address" : "48 Pirrama Road, Pyrmont NSW, Australia",
"formatted_phone_number" : "(02) 9374 4000",
"geometry" : {
"location" : {
"lat" : -33.8669710,
"lng" : 151.1958750
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
"international_phone_number" : "+61 2 9374 4000",
"name" : "Google Sydney",
"place_id" : "ChIJN1t_tDeuEmsRUsoyG83frY4",
"scope" : "GOOGLE",
"alt_ids" : [
{
"place_id" : "D9iJyWEHuEmuEmsRm9hTkapTCrk",
"scope" : "APP"
}
],
"rating" : 4.70,
"reference" : "CnRsAAAA98C4wD-VFvzGq-KHVEFhlHuy1TD1W6UYZw7KjuvfVsKMRZkbCVBVDxXFOOCM108n9PuJMJxeAxix3WB6B16c1p2bY1ZQyOrcu1d9247xQhUmPgYjN37JMo5QBsWipTsnoIZA9yAzA-0pnxFM6yAcDhIQbU0z05f3xD3m9NQnhEDjvBoUw-BdcocVpXzKFcnMXUpf-nkyF1w",
"reviews" : [
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Simon Bengtsson",
"author_url" : "https://plus.google.com/104675092887960962573",
"language" : "en",
"rating" : 5,
"text" : "Just went inside to have a look at Google. Amazing.",
"time" : 1338440552869
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Felix Rauch Valenti",
"author_url" : "https://plus.google.com/103291556674373289857",
"language" : "en",
"rating" : 5,
"text" : "Best place to work :-)",
"time" : 1338411244325
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Chris",
"language" : "en",
"rating" : 5,
"text" : "Great place to work, always lots of free food!",
"time" : 1330467089039
}
],
"types" : [ "establishment" ],
"url" : "http://maps.google.com/maps/place?cid=10281119596374313554",
"vicinity" : "48 Pirrama Road, Pyrmont",
"website" : "http://www.google.com.au/"
},
"status" : "OK"
}
Hope this would help!!

Google Places bad data

Some Places Detail provided by Google Places are not correct.
For example if you type Serengeti National Park in the Autocomplete, you have :
{ "predictions" : [
{
"description" : "Serengeti National Park, Serengeti, Mara, Tanzania",
"id" : "08bd9d4d10a2c77c36987c118a638ba8f75fb2f1",
"matched_substrings" : [
{
"length" : 23,
"offset" : 0
}
],
"reference" : "CkRAAAAAWmw4d0BvC9XVNF73sDnNPiQ841LRJ02RiaLxFonlOJmvmbP01nFpaJVfQVTa1lJScXHCOGtu9P83kUOYAd7f2xIQ_KqR0A_Sbhg8qvEEaIwlEBoU1dU8yFyfIhcZFfRNpdNGpacv6V8",
"terms" : [
{
"offset" : 0,
"value" : "Serengeti National Park"
},
{
"offset" : 25,
"value" : "Serengeti"
},
{
"offset" : 36,
"value" : "Mara"
},
{
"offset" : 42,
"value" : "Tanzania"
}
],
"types" : [ "establishment", "geocode" ]
} ], "status" : "OK" }
Which is ok. Note the description: Serengeti National Park, Serengeti, Mara, Tanzania.
However if you access the Google Places Details you have this:
{
"debug_info" : [],
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Serengeti",
"short_name" : "Serengeti",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Mara",
"short_name" : "Mara",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "TZ",
"short_name" : "TZ",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Tanzania",
"geometry" : {
"location" : {
"lat" : -2.333333,
"lng" : 34.833333
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "08bd9d4d10a2c77c36987c118a638ba8f75fb2f1",
"name" : "Serengeti National Park",
...
},
"status" : "OK"
}
We can see that the formatted address isn't correct, the country name is TZ instead of Tanzania, etc.
I want to pass this data to the Geocoding so I can have a viewport and other geometry data, but for that I need a proper address, like the one given in the predictions.description.
Is there a way to get this data upon selction of this place in the autocomplete?
Or is there another way to get information needed to geocode this place?
Thanks a lot