Reputation: 2232
I am trying to get location using longitude and latitude on server side.
https://maps.googleapis.com/maps/api/geocode/json?latlng=28.520506,77.20151&key=AIzaSyCmh3btBq1PK-gx98R_akhab9xzRFjfAzc
It gives a response as bellow:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Mehrauli - Badarpur Road",
"short_name" : "Mehrauli - Badarpur Rd",
"types" : [ "route" ]
},
{
"long_name" : "Block A",
"short_name" : "Block A",
"types" : [ "sublocality_level_3", "sublocality", "political" ]
},
{
"long_name" : "DDA Flats",
"short_name" : "DDA Flats",
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"long_name" : "Saket",
"short_name" : "Saket",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "South Delhi",
"short_name" : "South Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "110044",
"short_name" : "110044",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Mehrauli - Badarpur Road, Block A, DDA Flats, Saket, New Delhi, Delhi 110044, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.5207757,
"lng" : 77.20210809999999
},
"southwest" : {
"lat" : 28.5206053,
"lng" : 77.2013937
}
},
"location" : {
"lat" : 28.5206905,
"lng" : 77.20175089999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.5220394802915,
"lng" : 77.2030998802915
},
"southwest" : {
"lat" : 28.5193415197085,
"lng" : 77.2004019197085
}
}
},
"place_id" : "ChIJBVdwbfvhDDkRPQndknfs_uY",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Saket",
"short_name" : "Saket Metro Station",
"types" : [ "subway_station", "train_station", "transit_station", "establishment" ]
},
{
"long_name" : "Mehrauli - Badarpur Road",
"short_name" : "Mehrauli - Badarpur Rd",
"types" : [ "route" ]
},
{
"long_name" : "Saiyad ul Ajaib",
"short_name" : "Saiyad ul Ajaib",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Saiyad Ul Ajaib Village",
"short_name" : "Saiyad Ul Ajaib Village",
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"long_name" : "Saket",
"short_name" : "Saket",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "South Delhi",
"short_name" : "South Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "110030",
"short_name" : "110030",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Saket, Mehrauli - Badarpur Road, Saiyad ul Ajaib, Saiyad Ul Ajaib Village, Saket, New Delhi, Delhi 110030, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.5205491,
"lng" : 77.20161709999999
},
"southwest" : {
"lat" : 28.5204151,
"lng" : 77.20142389999999
}
},
"location" : {
"lat" : 28.5205063,
"lng" : 77.2015098
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.5218310802915,
"lng" : 77.20286948029151
},
"southwest" : {
"lat" : 28.5191331197085,
"lng" : 77.20017151970849
}
}
},
"place_id" : "ChIJtRwRbfvhDDkRNdb4NK6iicg",
"types" : [ "subway_station", "train_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "Saket Metro Station",
"short_name" : "Saket Metro Station",
"types" : [ "point_of_interest", "establishment" ]
},
{
"long_name" : "Mehrauli - Badarpur Road",
"short_name" : "Mehrauli - Badarpur Rd",
"types" : [ "route" ]
},
{
"long_name" : "Saiyad ul Ajaib",
"short_name" : "Saiyad ul Ajaib",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Saiyad Ul Ajaib Village",
"short_name" : "Saiyad Ul Ajaib Village",
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"long_name" : "Sainik Farm",
"short_name" : "Sainik Farm",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "South Delhi",
"short_name" : "South Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "110017",
"short_name" : "110017",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Saket Metro Station, Mehrauli - Badarpur Road, Saiyad ul Ajaib, Saiyad Ul Ajaib Village, Sainik Farm, New Delhi, Delhi 110017, India",
"geometry" : {
"location" : {
"lat" : 28.520528,
"lng" : 77.201505
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.5218769802915,
"lng" : 77.20285398029151
},
"southwest" : {
"lat" : 28.5191790197085,
"lng" : 77.2001560197085
}
}
},
"place_id" : "ChIJBzy8HfvhDDkRa124f53C6hI",
"types" : [ "transit_station", "point_of_interest", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "Saiyad Ul Ajaib Village",
"short_name" : "Saiyad Ul Ajaib Village",
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"long_name" : "Sainik Farm",
"short_name" : "Sainik Farm",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "South Delhi",
"short_name" : "South Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Saiyad Ul Ajaib Village, Sainik Farm, New Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.5208191,
"lng" : 77.20672110000001
},
"southwest" : {
"lat" : 28.5159519,
"lng" : 77.1994859
}
},
"location" : {
"lat" : 28.5194062,
"lng" : 77.2024306
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.5208191,
"lng" : 77.20672110000001
},
"southwest" : {
"lat" : 28.5159519,
"lng" : 77.1994859
}
}
},
"place_id" : "ChIJu3kI1uThDDkR7iU8hablnbU",
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Saiyad ul Ajaib",
"short_name" : "Saiyad ul Ajaib",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Block E",
"short_name" : "Block E",
"types" : [ "sublocality_level_3", "sublocality", "political" ]
},
{
"long_name" : "Saiyad Ul Ajaib Village",
"short_name" : "Saiyad Ul Ajaib Village",
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"long_name" : "Sainik Farm",
"short_name" : "Sainik Farm",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "South Delhi",
"short_name" : "South Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Saiyad ul Ajaib, Block E, Saiyad Ul Ajaib Village, Sainik Farm, New Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.520712,
"lng" : 77.2065688
},
"southwest" : {
"lat" : 28.5112375,
"lng" : 77.18968389999999
}
},
"location" : {
"lat" : 28.517186,
"lng" : 77.20171189999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.520712,
"lng" : 77.2065688
},
"southwest" : {
"lat" : 28.5112375,
"lng" : 77.18968389999999
}
}
},
"place_id" : "ChIJnTMcyuThDDkRTUiS4Zu2qW4",
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "Sainik Farm",
"short_name" : "Sainik Farm",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "South Delhi",
"short_name" : "South Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Sainik Farm, New Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.5234901,
"lng" : 77.22944389999999
},
"southwest" : {
"lat" : 28.485256,
"lng" : 77.18350099999999
}
},
"location" : {
"lat" : 28.5068824,
"lng" : 77.211229
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.5234901,
"lng" : 77.22944389999999
},
"southwest" : {
"lat" : 28.485256,
"lng" : 77.18350099999999
}
}
},
"place_id" : "ChIJZ2itOb_hDDkRTq6aivCYp_Q",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "New Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.889816,
"lng" : 77.3418147
},
"southwest" : {
"lat" : 28.4010669,
"lng" : 76.8396999
}
},
"location" : {
"lat" : 28.6139391,
"lng" : 77.2090212
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.889816,
"lng" : 77.3418147
},
"southwest" : {
"lat" : 28.4010669,
"lng" : 76.8396999
}
}
},
"place_id" : "ChIJLbZ-NFv9DDkRzk0gTkm3wlI",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "110030",
"short_name" : "110030",
"types" : [ "postal_code" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "New Delhi, Delhi 110030, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.5347099,
"lng" : 77.20646719999999
},
"southwest" : {
"lat" : 28.4761952,
"lng" : 77.1228993
}
},
"location" : {
"lat" : 28.4956835,
"lng" : 77.1664945
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.5347099,
"lng" : 77.20646719999999
},
"southwest" : {
"lat" : 28.4761952,
"lng" : 77.1228993
}
}
},
"place_id" : "ChIJYaOVHDceDTkRyvLz3dSSVIk",
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "South Delhi",
"short_name" : "South Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "South Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.6118999,
"lng" : 77.34496009999999
},
"southwest" : {
"lat" : 28.4041,
"lng" : 77.11156
}
},
"location" : {
"lat" : 28.5086497,
"lng" : 77.2175223
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.6118999,
"lng" : 77.34496009999999
},
"southwest" : {
"lat" : 28.4041,
"lng" : 77.11156
}
}
},
"place_id" : "ChIJqX7ja73hDDkRRA9T2LXUAJg",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.881338,
"lng" : 77.34845780000001
},
"southwest" : {
"lat" : 28.412593,
"lng" : 76.83806899999999
}
},
"location" : {
"lat" : 28.7040592,
"lng" : 77.10249019999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.881338,
"lng" : 77.34845780000001
},
"southwest" : {
"lat" : 28.412593,
"lng" : 76.83806899999999
}
}
},
"place_id" : "ChIJLbZ-NFv9DDkRQJY4FbcFcgM",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.5087008,
"lng" : 97.395561
},
"southwest" : {
"lat" : 6.7535159,
"lng" : 68.162386
}
},
"location" : {
"lat" : 20.593684,
"lng" : 78.96288
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.5087008,
"lng" : 97.395561
},
"southwest" : {
"lat" : 6.7535159,
"lng" : 68.16288519999999
}
}
},
"place_id" : "ChIJkbeSa_BfYzARphNChaFPjNc",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
This response contains multiple addresses nearby the location specified by the latitude and longitude. I am interested in extracting the locality from this response.
For example in case of above query, the location is Saket which is located in Delhi, India.
If I try to limit the results based on filtering on address type, it only limits the number of addresses in response and still returns multiple addresses.
How can I extract exact or approximate location?
Any help appreciated.
Upvotes: 0
Views: 1518
Reputation: 2487
Geocoding is not one-to-one; a single lat/lon corresponds to multiple things. The best you can do is to rank them on importance (Google already does this) and pick the most important one. You can also sort them based on your own parameters, such as the distance to your query point. But there is no way to guarantee exactly one result.
Upvotes: 1