how to get the place details from place ID in android(kotlin) - android

Hi i want to get place details with placeID and add marker in android maps activity using kotlin. i have done it with latlng but am not getting with placeID . can any pleease help.
my code with latlng .
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
val mdis= LatLng(1.301440, 103.847980)
mMap.addMarker(MarkerOptions().position(mdis).title("MDIS").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)))
mMap.moveCamera(CameraUpdateFactory.newLatLng(mdis))
mMap.animateCamera(CameraUpdateFactory.zoomIn())
mMap.animateCamera(CameraUpdateFactory.zoomTo(15F),2000,null)
mMap.uiSettings.isZoomControlsEnabled=true
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.isMyLocationEnabled = true
}
else {//condition for Marshmello and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), MY_PERMISSION_FINE_LOCATION)
}
}
mMap.setOnMarkerClickListener(this)
}
Tried this and am getting error at placesClient.fetchPlace(request).addOnSuccessListener(response) ->{
Error says Type mismatch Required: OnSuccessListener
found : FetchplaceResponse
This is my updated code :
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
var mdisplaceId = "ChIJRwCirzca2jERhd68E52R_2Q"
//val placesClient = Places.createClient(this)
var placeFields : List<Place.Field>
placeFields = Arrays.asList(Place.Field.ID , Place.Field.NAME)
var request : FetchPlaceRequest
request = FetchPlaceRequest.newInstance(mdisplaceId, placeFields)
lateinit var response : FetchPlaceResponse
lateinit var placesClient : PlacesClient
placesClient.fetchPlace(request).addOnSuccessListener(response) ->{ //error at this line near response
var place : Place
place = response.getPlace()
})
}

Use Geocoding api,
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
which will return the following json response,
{
"results" : [
{
"address_components" : [
{
"long_name" : "277",
"short_name" : "277",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "277 Bedford Ave, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.7142205,
"lng" : -73.9612903
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.71556948029149,
"lng" : -73.95994131970849
},
"southwest" : {
"lat" : 40.7128715197085,
"lng" : -73.9626392802915
}
}
},
"place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
Get the lat long from geometry object and add marker on the google maps.

Related

Google Map API: How to manage different "address_components" in gecoding results?

Consider below JSON response of google map gecoding response.
It consists of an array of address_components with five elements.
Usually developer use its first item (item with index 0) to find address elements such ac route and locality and formatted address. But this is not the best alternative. For example in this case, 2nd item is more descriptive address than others. Sometime there are same address with different localities in same response.
How can I choose the better one (E.g. Google Map API in android selects one among available item but it is not always item with index 0, but it select among alternatives, I want to know how it will do that and select the best match)?
Besides, I was wondering to iterate through all elements of different address_components to extract and bring together finer pieces of information but the problem is information for same type of elements like locality have different information in different address_components which result in inconsistency of address when elements of information gathered from different address_component items.
As an example, In below sample we have two values شهر جدید اندیشه and Karaj in different locality elements which are two different city labels (in the same neighborhood).
https://maps.googleapis.com/maps/api/geocode/json?latlng=35.7163931455472,51.01335000246763&key=xxxxx&language=fa
{
"results" : [
{
"address_components" : [
{
"long_name" : "Unnamed Road",
"short_name" : "Unnamed Road",
"types" : [ "route" ]
},
{
"long_name" : "Karaj",
"short_name" : "Karaj",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Shahriar County",
"short_name" : "Shahriar County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Tehran Province",
"short_name" : "Tehran Province",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "ایران",
"short_name" : "IR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Tehran Province, Karaj, Unnamed Road, ایران",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.7221382,
"lng" : 51.0148178
},
"southwest" : {
"lat" : 35.716435,
"lng" : 51.0095103
}
},
"location" : {
"lat" : 35.719286,
"lng" : 51.012165
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 35.7221382,
"lng" : 51.0148178
},
"southwest" : {
"lat" : 35.716435,
"lng" : 51.0095103
}
}
},
"place_id" : "ChIJmf7_zEyTjT8RkM8-nK6dTm0",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "فاز ۶ شهر جدید اندیشه",
"short_name" : "فاز ۶ شهر جدید اندیشه",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "شهر جدید اندیشه",
"short_name" : "شهر جدید اندیشه",
"types" : [ "locality", "political" ]
},
{
"long_name" : "شهرستان شهریار",
"short_name" : "شهرستان شهریار",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "استان تهران",
"short_name" : "استان تهران",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "ایران",
"short_name" : "IR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "فاز ۶ شهر جدید اندیشه، شهر جدید اندیشه، استان تهران، ایران",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.7389537,
"lng" : 51.0349971
},
"southwest" : {
"lat" : 35.7039031,
"lng" : 51.0044146
}
},
"location" : {
"lat" : 35.7210753,
"lng" : 51.014934
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.7389537,
"lng" : 51.0349971
},
"southwest" : {
"lat" : 35.7039031,
"lng" : 51.0044146
}
}
},
"place_id" : "ChIJiYlwJLHsjT8RruE39U9NMoQ",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"address_components" : [
{
"long_name" : "شهر جدید اندیشه",
"short_name" : "شهر جدید اندیشه",
"types" : [ "locality", "political" ]
},
{
"long_name" : "شهرستان شهریار",
"short_name" : "شهرستان شهریار",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "استان تهران",
"short_name" : "استان تهران",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "ایران",
"short_name" : "IR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "شهر جدید اندیشه، استان تهران، ایران",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.7388144,
"lng" : 51.04460479999999
},
"southwest" : {
"lat" : 35.6838973,
"lng" : 50.9894371
}
},
"location" : {
"lat" : 35.7078282,
"lng" : 51.0227587
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.7388144,
"lng" : 51.04460479999999
},
"southwest" : {
"lat" : 35.6838973,
"lng" : 50.9894371
}
}
},
"place_id" : "ChIJIQwwRcnsjT8RnTJfLJ3QUAg",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "شهرستان شهریار",
"short_name" : "شهرستان شهریار",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "استان تهران",
"short_name" : "استان تهران",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "ایران",
"short_name" : "IR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "شهرستان شهریار، استان تهران، ایران",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.7389885,
"lng" : 51.23044970000001
},
"southwest" : {
"lat" : 35.5333437,
"lng" : 50.8859253
}
},
"location" : {
"lat" : 35.6096201,
"lng" : 51.03319330000001
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.7389885,
"lng" : 51.23044970000001
},
"southwest" : {
"lat" : 35.5333437,
"lng" : 50.8859253
}
}
},
"place_id" : "ChIJQ6KQjY7xjT8RoYD9gJh8_CY",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "استان تهران",
"short_name" : "استان تهران",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "ایران",
"short_name" : "IR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "استان تهران، ایران",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 36.07789210000001,
"lng" : 53.216228
},
"southwest" : {
"lat" : 34.909543,
"lng" : 50.3186971
}
},
"location" : {
"lat" : 35.7248416,
"lng" : 51.381653
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 36.07789210000001,
"lng" : 53.216228
},
"southwest" : {
"lat" : 34.909543,
"lng" : 50.3186971
}
}
},
"place_id" : "ChIJf5Us9YQBjj8R0OohvHQms1U",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "ایران",
"short_name" : "IR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "ایران",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 39.782056,
"lng" : 63.3333366
},
"southwest" : {
"lat" : 24.8066999,
"lng" : 44.0326949
}
},
"location" : {
"lat" : 32.427908,
"lng" : 53.688046
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 39.782056,
"lng" : 63.3333366
},
"southwest" : {
"lat" : 24.8066999,
"lng" : 44.0326949
}
}
},
"place_id" : "ChIJ8R1rwS7s9z4RzvpRntOVsEA",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
It looks like there are two different questions in one, so the scope is a bit broad. I will try to address your doubts.
First, you are using the reverse geocoding request to resolve coordinate 35.7163931455472,51.01335000246763 into street address. Note that reverse geocoding is designed to resolve the given coordinate to the nearest available address. Also the service will suggest different types of results in addition to street address: route, sublocality, locality, administrative areas, country.
Sometimes if there are no street addresses close to the given point (approx. within 50 meters from the point), the result of type street address is not present in the response. In your example the result of type street address is not available, so the response contains different types of results starting from the route level. Have a look at my screenshot that represents first three items from your response.
As you can see the first item has a type route, the second item has a type sublocality and the third one has a type locality.
It is difficult to say what is the "best" criteria to chose the address. You can see that the route item is the closest one, but unfortunately this road is unnamed, so you qualify it as not good enough. Technically this is the closest item, so the service supposes this is the best match. I would suggest checking the type of results, if the item 0 has a type street_address is should be good enough to go, if the item 0 has a type route, check if the road has a name, if this is unnamed road check the item 1 that might be more detailed.
Anyway the unnamed road is the data issue and you can report it to the Google data team following the help center:
https://support.google.com/maps/answer/3094088
Second, if I understand correctly you noticed that item 0 and item 1 have different value of locality address component, although both of them (marker 1 and marker 2 in my screenshot) seem to belong to the Andisheh New Town. The route (place ID ChIJmf7_zEyTjT8RkM8-nK6dTm0) is reported as the part of the Karaj locality. I can see the boundaries of Karaj on Google Maps as in the following screenshot
https://www.google.com/maps/place/Karaj,+Alborz+Province,+Iran/#35.7700272,50.95899,12.41z/data=!4m5!3m4!1s0x3f8dbf95ef45f011:0x722a04e54eba9bcd!8m2!3d35.8400188!4d50.9390906
And I can see the boundaries of Andisheh New Town as shown in the following screenshot
https://www.google.com/maps/place/Andisheh+New+Town,+Tehran+Province,+Iran/#35.6998795,51.0216598,14.08z/data=!4m5!3m4!1s0x3f8decc945300c21:0x850d09d2c5f329d!8m2!3d35.7078282!4d51.0227587
Please note that the route from your response is clearly situated inside the Andisheh New Town polygon, so at this point it looks like we are facing a data issue as well. The place ID ChIJmf7_zEyTjT8RkM8-nK6dTm0 should have locality component of Andisheh New Town, not the Karaj.
Feel free to send feedback to Google using this page:
https://www.google.com/maps/place/Tehran+Province,+Karaj,+Unnamed+Road,+Iran/#35.719286,51.0077876,17z/data=!3m1!4b1!4m5!3m4!1s0x3f8d934cccfffe99:0x6d4e9dae9c3ecf90!8m2!3d35.719286!4d51.012165!10m2!1e3!2e3
If you would like to play with reverse geocoding, please use Geocoder tool:
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D35.716393%252C51.01335
I hope my answer clarifies your doubts!

Google Places: Android API: Place class missing some data

I find that Place class from Android Library for Google Places API misses a lot of data which is available in Web-Service API.
Android class:
https://developers.google.com/android/reference/com/google/android/gms/location/places/Place
Web-Service reply:
https://developers.google.com/places/web-service/details?hl=en
For example, class definition has nothing about Reviews or Opening Hours. Why it is so? Is it possible to fix it without converting all the app to Web-Service API calls and json-parsing? Perhaps there is an ability to convert Place object to its raw JSON-form to find some additional data?
Places api in android only returns a few details like place name, latitude, longitude etc. if you want more details you have to call webservice api . This is the URl
https://maps.googleapis.com/maps/api/place/details/json?key={API_KEY}&placeid={PLACE_ID}
Here PLACE_ID will get from Place Object in Android Places Api
Sample output:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "7167",
"short_name" : "7167",
"types" : [ "street_number" ]
},
{
"long_name" : "Sunset Boulevard",
"short_name" : "Sunset Blvd",
"types" : [ "route" ]
},
{
"long_name" : "Central LA",
"short_name" : "Central LA",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Los Angeles",
"short_name" : "Los Angeles",
"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" : "90046",
"short_name" : "90046",
"types" : [ "postal_code" ]
}
],
"adr_address" : "\u003cspan class=\"street-address\"\u003e7167 Sunset Blvd\u003c/span\u003e, \u003cspan class=\"locality\"\u003eLos Angeles\u003c/span\u003e, \u003cspan class=\"region\"\u003eCA\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e90046\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUSA\u003c/span\u003e",
"formatted_address" : "7167 Sunset Blvd, Los Angeles, CA 90046, USA",
"geometry" : {
"location" : {
"lat" : 34.098341,
"lng" : -118.346263
},
"viewport" : {
"northeast" : {
"lat" : 34.0995122302915,
"lng" : -118.3449154697085
},
"southwest" : {
"lat" : 34.0968142697085,
"lng" : -118.3476134302915
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "7143e0743707f66cfa1cda945779f18e8350060e",
"name" : "7167 Sunset Blvd",
"place_id" : "ChIJn_0bqti-woARRtpNvG7kuq4",
"reference" : "CmRbAAAAuU6CDhPuLqrS9F4XCZIV61wveIeU44RMua7FSCZHtwJeC58_OJksQvLI9Nudb9-N8RhfJDUECkxvdB-llxe6WIqrkDQMBs2vz3mBky0DH70jIcFS7f0_7yivdsxmmLzaEhAtwgzDs0va2soGuFyzuNTGGhR-xVrIvyeZwY0OGDmEp3FJmrbbeQ",
"scope" : "GOOGLE",
"types" : [ "street_address" ],
"url" : "https://maps.google.com/?q=7167+Sunset+Blvd,+Los+Angeles,+CA+90046,+USA&ftid=0x80c2bed8aa1bfd9f:0xaebae46ebc4dda46",
"utc_offset" : -420,
"vicinity" : "Los Angeles"
},
"status" : "OK"
}
Sample JSON Parsing in android
String postalCode="";
String city="";
String state="";
String address="";
JSONObject jsonObj=new JSONObject(rs);
JSONArray addressComponents = jsonObj.getJSONObject("result").getJSONArray("address_components");
for(int i = 0; i < addressComponents.length(); i++) {
JSONArray typesArray = addressComponents.getJSONObject(i).getJSONArray("types");
for (int j = 0; j < typesArray.length(); j++) {
if (typesArray.get(j).toString().equalsIgnoreCase("postal_code")) {
postalCode = addressComponents.getJSONObject(i).getString("long_name");
}else if (typesArray.get(j).toString().equalsIgnoreCase("locality")) {
city = addressComponents.getJSONObject(i).getString("long_name");
}else if (typesArray.get(j).toString().equalsIgnoreCase("administrative_area_level_1")) {
state = addressComponents.getJSONObject(i).getString("long_name");
}else {
String types=typesArray.get(j).toString();
if(types.equalsIgnoreCase("street_number")
||types.equalsIgnoreCase("route")
||types.equalsIgnoreCase("neighborhood")
||types.equalsIgnoreCase("sublocality_level_1")
)
address+=addressComponents.getJSONObject(i).getString("long_name")+", ";
}
}
}

Json parsing to get lat long

I have a response from a google web service like the follwing
{
"html_attributions" : [],
"next_page_token" : "CpQCBgEAALxtLOBsfDviXsb8WHg4nGHrCETBydo0YcgOw-IHvFS4CQI1ZaM331dtA8Y3CxPeZZlF0IYjwQMp2A8W5A5UKtTrR4sQq1Um6FJgUNCpZzrcT6RwaPJKzOjbaFrPt5GqnQM6W1vxxdK9nKu5lyBbvLr0yJnzBWEAqyLyT2MFvak-_qDIR8b3yK3Efy34SsoHNnBnaANVc5hMztz7aWGphkTDNtEfuSZnQQ72jPg5_ey5F2G29in_QJXJlR9a3YYNGFmefLta2e0T34OGOhtvCinrdE7dcEUuaK55LV8TnP33HlGKC7PruXkv4AF8Xvxnlsk9ALVFdzECmJ4br6RTq3iWjBZ0z5FPNzqjfa6NNFiqEhDItZbJiBr9cEOaefilqJQSGhSybiB6SRFA8b-p86rFbiNEHjEo3w",
"results" : [
{
"geometry" : {
"location" : {
"lat" : 47.610399,
"lng" : -122.335791
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "8f7e7d038dbd13f283d1875ecf68d04fc2255561",
"name" : "GNC",
"opening_hours" : {
"open_now" : false
},
"place_id" : "ChIJ3zJ_hLRqkFQR0AxDtprgAjI",
"price_level" : 2,
"reference" : "CnRlAAAAoWRGwTPL4nLYI7qPZ2ukmXEdDWAScl5XASmeogPbQsOEvbFdXvb2cMYWsQZ1LR3QUYDo8djSpfKQc0BFLkA37iw_iTW2XRZy4568H5kReNd1xqVDjZyVmHPaWnKkJrTl0bJh_1P1Vpg-ntqUq-zVkRIQDVbTyrun8WsRA1eftayO8RoUSwjCBqkVaPwmyLJtXfJfk9qmF2I",
"scope" : "GOOGLE",
"types" : [ "health", "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "421 Pike St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.61,
"lng" : -122.33
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "82e45a03dca8dcb2192cc250777a11f2e06452de",
"name" : "Deluxe Foods LLC",
"place_id" : "ChIJ9dKBxbVqkFQRoDLJeSe0CZo",
"reference" : "CoQBcwAAAD1dI5pifx1zH03cyQyPsJL10GZc7UFMHEKgZVzmIbZ77ZUdoXAme2iE3WzBgcCUuwg3zv735Pey9MrLWXdbKBmBGl_mvs_dD6aMmNYEhwft54gVpabn0ZVNLrflNEyun-dUkoeHvx5GEWNj1UvYGB1zbCY1CSUwbiqH4Ouay9GpEhBHwGUT7yCYw9IC3hGGUUxgGhRHU_AjT-jsRrHxoC6o7YKVpCLyCg",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "PO Box 30102, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.607573,
"lng" : -122.333167
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "695bf0d73be3b5f55ca43fe14abce48f9853ef15",
"name" : "Community Grocery",
"opening_hours" : {
"open_now" : false
},
"place_id" : "ChIJNeh4UrFqkFQRV3OXAHsHZxg",
"reference" : "CoQBcwAAALpG-2YSWEiceu-5h5km-SYuQelZR70nMSMg9uhs38uPusDF4s_5hnf6eQ8ORGJf7JGv9w9il4cL9to8r0bcA7HxK2ghxkrWlBbcP2asPWJTCdzrQB-uUs9TKxHc-kldayis9zmLQtHoDW_J8sSXlZHnHNYPol9VjKZIUiIlpYTNEhD9a-VcIRldCMVZhHNow03fGhRsw5lDMDZOcVEHdRBz2NAuhTdZ3Q",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "415 Seneca St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608163,
"lng" : -122.335371
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/wine-71.png",
"id" : "fde562e2d1e74e8ea18d3f758a05fe78c45d9346",
"name" : "Essential Market",
"opening_hours" : {
"open_now" : false
},
"place_id" : "ChIJvYQm8LNqkFQRjJM6WIcYE1A",
"rating" : 3.8,
"reference" : "CoQBcgAAALd33oAy_-Vxbcbf_gMywCqPHjyZixFi-yc1glcFFxbtpTDywxT-vPFKNExST7K305Ohv0AkhA_Br2Eh8AeetN-D7PxuW3mK5vsYPUbIfOTRWoM_CX2YH84cXOj9eFwGXwTiuXEOvOjhz8SYj59N4g78IUUDi-mfmGzd_C_jq90REhDJ3y45nfvr6IbfvapMnK0ZGhQDCjyU7Zu8ymTuQLGmoEsJRTVVGQ",
"scope" : "GOOGLE",
"types" : [
"liquor_store",
"grocery_or_supermarket",
"food",
"store",
"establishment"
],
"vicinity" : "1301 4th Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.612122,
"lng" : -122.332044
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "6b4f8dc7039510bbbfca8fd12b359d444eef2be9",
"name" : "Clay's Market",
"place_id" : "ChIJdxjOE7VqkFQR4oDXOXnQEQ8",
"reference" : "CnRvAAAACC-kzX3pJo0x3OjptaM_wqWs5kbrk53V4xa6gYiOj3PP8Fo8L_DqSTjKRMqrF8bRGVfnTKtaLujZLvxkIgj7pNjyTAauRPKv0TL7dHpru66CyaJRNjoRSHME0xYHJJNj7VQDeLl5HO8dab0FZFrevxIQ6tZzbeqqRp6QEPf_Ue7XhBoU4Y3VkFBSCx_4b1pu1ApLYwXzLDQ",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "815 Pike St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.609336,
"lng" : -122.337791
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "691548827b64fe2609fb7c27abbb1bcbe224f327",
"name" : "Kress IGA Supermarket",
"opening_hours" : {
"open_now" : false
},
"photos" : [
{
"height" : 600,
"html_attributions" : [],
"photo_reference" : "CnRnAAAAl-_QCXEqC8UaTUMrQDzG_bDcP56Lf2i2CPbBmxYj71VlemLiDrQpC38wmDTwsHvoFF_pGjLBo4vQHfPjM2nSLrPmHQeotYySYTwBhOcXPF9ISdvUNjBbyGUwCUFyBDvdcdIzaChOO0bEi5JVZRxNhRIQVsyOvUEat6gFROdcoh95HhoUS-Mac5cLzJFt12vSx_E7m_VUIfI",
"width" : 398
}
],
"place_id" : "ChIJbdIhoLNqkFQRMTiHCN6W4nU",
"rating" : 4.4,
"reference" : "CoQBdwAAAN3wdCAgdfatcILheBD3VwNP0Z_Zqjq8niNljnqpzHYJ2ieG6VEgxvfMQCU8BfawcRxO7MTu08gXtElThcYPdrQy5KFfAgdj1dFqi8PUh5R7wKKChjGN-sPj2cqRSIR-K853WjMlNMAhZSJMc2XWAhlbmy2AbXoe_9LsPoQfYE72EhCOBHIK_E0gTGPetycfRP6MGhSlBGpKKlZL00qldn6Xd44Y1TDj7A",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1427 3rd Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.606546,
"lng" : -122.335449
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "3f23cf0c8bfdbe746a69d518c031dcafbd266d64",
"name" : "Netzel Associates",
"place_id" : "ChIJ1Q6vb7FqkFQR0PCOSJtLQpM",
"reference" : "CoQBdAAAABiMSr5ILBmkVjTeRpVAVHij7jMxhTacAJvz9YTFXVQYCP8he5wD4pAfaOu5dBlI4oagjaPPq8qUif9_hYN_IHTKpYnHaFWqCoHsHe-6kKIO1sIiE5qdLG-Buz2tQkVIPmzPqk67X60dgRux31vZjf10dg6BvizIiMTk5Q8D4AZWEhCJKbWcknUroTKKyatmH0JwGhS2i7O8iONEf9A5_qTBl3N1K8v4fg",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1111 3rd Ave # 2500, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.612607,
"lng" : -122.336984
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "1672b9dd46c6dcb5ae1409d0f87b23f1c38f33d0",
"name" : "Market Fresh Winter Garden",
"place_id" : "ChIJrdhDMksVkFQR0ugNKk-w7Fk",
"reference" : "CoQBfAAAAAxvLHmqlrTabI8g1bBkEMySU_pDqmYpmKmWNie37kRzknuKxRQkbPiVOs7cCB4yxiJGZ2X79qQW1dTFpJ9EE4jclrSKMFqzDqzPET1VHCR_imCSIWRLz_51iveURUW8r2GszBwCDISlu1iB1GBuLT58UB8Ghs-N3_yGxqaE_1JAEhBJn_a6kPPhJqGq9_pr4AifGhQNEEknt7JSyB1mvXCDgZ4o83aNng",
"scope" : "GOOGLE",
"types" : [
"meal_takeaway",
"restaurant",
"grocery_or_supermarket",
"food",
"store",
"establishment"
],
"vicinity" : "509 Olive Way, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.613069,
"lng" : -122.329411
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "08e8916d1ade44b3403b1a1268fc038bd449952b",
"name" : "Pike Grocery",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 384,
"html_attributions" : [],
"photo_reference" : "CnRnAAAAmx9YeUnwUAcOesWmlueyy2ZJQT-GLpRSd0_WXWHV0pP7c42-FvQYcfkpRoDPCHiqlzJSWa_fPcqADGvAi_HaLFCcUhcIbbrTeJOSvlP1bA_ACdWmPBDF26vYc6xAJqmtR37EXiZ5j-sVYh09_hpEzRIQap8C87zKuwIYfsR6zsAg2RoUsAbkV4FLlFmsKTthHEbVdd0G9VM",
"width" : 512
}
],
"place_id" : "ChIJMwdemspqkFQRUqQ91N-ufeo",
"reference" : "CnRvAAAAPclUek8vRV5Wcsbp4G08nttAphmKfTASUhTAR4I8vUnTFkSp7NP06JC4y3Xj7qTr9I105d7HpVkQJhuH6HDyLzej-FM5yDc7LqNMmkrAN3CcmQezcd9MhSZkdXdTDKTsYaZV4GgWogi4lqFcUgfhDBIQd1M7Sq4-EWJ4Y6cNUJPKHBoUnHoVxfWyl3lK7imAxJkxGkKNH9k",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1011 Pike St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.606521,
"lng" : -122.337704
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "110c00b34dc635662716772ab4a0e0f6deced28a",
"name" : "Young's Market",
"place_id" : "ChIJ44qIiLFqkFQRw1TsXz9H2kg",
"reference" : "CnRwAAAAxMdsj6FXUIP8S9c42j9nSu7XtMyNY612TS6njqubsoZLBJwpr6GmxsrRKREPttLtBSYWOmq4gP2ovwO2utvqtLpjHcWs2YCFhNKj4k8Sz7F_d3aoAzOwNKmIp88bPxMbPUJj9eKI5gvTxct9CfRe4BIQolTWxCAybHKlUTMnfVgNfhoUlO1jmn7wrjUBVBcnZYe5QubTl_w",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1210 1st Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608777,
"lng" : -122.33973
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "d2f9218e208a84da7c7a7e2980cebd3671a8f386",
"name" : "Double Dorjee",
"place_id" : "ChIJkX3s-LJqkFQRQRexqCN0jQY",
"reference" : "CnRvAAAAoRDVxZ8W_qmEWPmjxXulAz9lS562rRQgirkEIhgXesLoXNgcJsw2iQIymEUKRmvQwrAOuLYrOIa8t1yPQdYYAvEajqIxcZ0PWzMEUeaDuCKL37p_WGzKOJuHhv0ki_3Z10BOPwmCNvo5XiOfz7j1ABIQK9UdmY_KTIYTP8tnoG5T_RoULYEoZrNBYy8EULQD7LUGs_E7krU",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1501 Pike St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608248,
"lng" : -122.339523
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "efeff82fc657a72cf8a3079aabe0e27d0d91a6c5",
"name" : "Lina's Fruit & Produce",
"place_id" : "ChIJD22J-LJqkFQRxhEAGumCphs",
"reference" : "CoQBeAAAAFcDpYes7w9SDBBHP_bOYNQrM1CUQichslNR-hyXATeyWMptKnIEdS27q0HR5BLfQAzQvgP_yIM2ElDrfSjtSdTVgiJlWZuVhtsBRIdV4n8cthl6uihZJnmtevrBYRq7GEid2Bp_IZiLJ4K6nYIpsyKlJNpBBvcKASbEsn4TAtM-EhCYsE2fgYPwEgX4ZHAkU6t7GhS2-N0cGpHzmVMAgNMKYP2GCW23-g",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1431 1st Ave # 7, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.605109,
"lng" : -122.333955
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "d81b188da1c901ec10149c5224e6fd1beac9ef9c",
"name" : "Second Avenue Sundries",
"place_id" : "ChIJexG9_rBqkFQRi7Zb8KM4P98",
"reference" : "CoQBeQAAAOH07x4jtyj3WTa6sWNSHXCPGItPL7kbSJbQFreF2VUJ-vxjenChmIoGcvsIREYEsV_dZYyXG3RoMpezUCUkuIvLsaEt1wxJcK6t0tM8QHylTdcc9IJRmjE_npy9rTbIehBixa5ESV1EGmF_rhyHQJyhPjAz7hmDcdlysEBRuySsEhAq3UlVuENA0aC8L59q8ZEFGhSUrFU9dmUkkl24SQ6RMQvA74mB5w",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "999 3rd Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.614062,
"lng" : -122.335451
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "69ff94699431b37223bdea4fd7792c7244a6b36c",
"name" : "Sphere Foods",
"place_id" : "ChIJ1ef53EoVkFQRlYzOwPY7DhA",
"reference" : "CnRuAAAA6qjCWZS9h_Eb5x5UOAN2J99LIsJ9NEVR-LtbjUoLKx5OJmLmGGYEazQmbf02Rp8TrqyEVeE41rHimDGdvnQkj5WbLrxQCVM195Y3bLssymy7kUTFVaiclgZjVdEPf5WLcC9Xl6JjfiWxqN-5I1oEihIQZtGMYRH317G1c7rRQWd9MxoUmSFfTX5FwgECzfDH7hRFX4F5mJA",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1700 7th Ave # 2100, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.614425,
"lng" : -122.334307
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
"id" : "e5be7cde6eb52cb0a5a7aa975ea915e2f80bd7dc",
"name" : "Stewart Street Market",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 1152,
"html_attributions" : [],
"photo_reference" : "CnRnAAAAqeZQn6gaLGIJib2WaNAcw7GdNLJuCc6AvrMCF543GBpl9tgLJ99CS0DmrdbHyWVMcw_H7q_LZgOqqKLwkilB95SjXsJqu4O3KMVQQAqtQWHW27u2vKVkTOqxAWTDppdManOPMUlFP2LLN1zya33z2xIQY-si5NTVqW9CpTd2AhP9NxoURwp9wSSgmZY2dtC1q6OezNsUvQw",
"width" : 2048
}
],
"place_id" : "ChIJ7yBfx0oVkFQRcLjVXNF_3qg",
"reference" : "CoQBeAAAADTkEpb503o9qN93lxGtIlh8x-uyIgRIs0Pj_-gGXpxQoQkAnIGcmVYgDLEzcFtpgzDchjxRWmns1RPUhqnThNJp4_VmG3ZP42zFNDAO_Q2_YowPiM7Wgz3oQsr66uIEBKZnNW9QRrd_GMbA64OdLZGrn7wZwvdp5UWCCnJJe0NFEhBKdwGNlHZTu2Y4BOi6Zl-lGhSbbDS4npeTaLx_cwgpmTmKUpp43g",
"scope" : "GOOGLE",
"types" : [
"convenience_store",
"grocery_or_supermarket",
"food",
"store",
"establishment"
],
"vicinity" : "1812 8th Ave, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.609322,
"lng" : -122.325655
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "87b467979468e6d2b5b203c7486d84e08505dd17",
"name" : "Plaza Select Foods",
"photos" : [
{
"height" : 720,
"html_attributions" : [ "From a Google User" ],
"photo_reference" : "CnRnAAAAhzdJBafPaRrh7mLSuB87bVza6ErJKRSvyNKU6pGa1EcblihzmqNx67HZQog-EO7wkvag7KP0bNXWvNXo9RF5u2vYNxZZ-5v7Z7KhL7tOmfWi7U7n6aFr9Sls_fZ_KcpBmH-m63Nl4H6E5GDFZwwNuxIQJUHOGAtXDDa9YT62KVkEjhoUVenpQc8oBGHYJOwTPiJDxSf2aH8",
"width" : 960
}
],
"place_id" : "ChIJy1bFH7ZqkFQRXpKoZ2NFh2Y",
"reference" : "CoQBdAAAAJZ0WNOu_ProGJ7t62FbE2ADOx6sGGH0YIR1KNGRqiZ9scWKjBjMhUHb3VNKZzEuvFUIBPOjrfBtkbjpEUthCC8HHbe_hsuLCyp2FUi2AFDVgBWB49X4fPla0IcNoOECfEzwRskswcKgcNyob1wt_mB2nEEbQVFDr8W6vhRsl1EyEhBgAGQ9WXQzLHZzzRs84JEyGhRM0Rx5TiF0g11uFT5mg6v6uHr97g",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1024 Madison St, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608802,
"lng" : -122.340552
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "14a2e0601f06734e7c8be82ed00d3806e3f9ecd4",
"name" : "Corner Produce",
"place_id" : "ChIJn9FR9rJqkFQR89xobVdunBE",
"reference" : "CnRwAAAAFad6cCCAww55VmSDqvH9XCZzHx5LoyFH2MHT5YkFM9NZNr7L3eKhd02_VnSEunZ3sMlCyvT37k-Tg6EZV4MuWVOZ2imbVrPX1OeRgJWS6OgXx-FHabcAj2W5jgRkCsHwEhjwTW0DNYNDX5GEn-a1HRIQNsCEN53Q1PC3NAHCTutgARoU-4Qq8ZOFJfF3Lyyc1Xf66FN8KQ0",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1500 Pike Pl, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.60884,
"lng" : -122.340616
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
"id" : "2a7a890254bcb2b1ae409cbf78d4e741800f036f",
"name" : "Oriental Mart",
"photos" : [
{
"height" : 460,
"html_attributions" : [
"\u003ca href=\"https://plus.google.com/115115924044576716973\"\u003eMatt Good\u003c/a\u003e"
],
"photo_reference" : "CnRtAAAAYab-XNz1Tm-iiB1qfxZJiC-T--LI7Mr5haR62p-RlGTrAIAPk2lhWogWOTQcFjPIHCiM2aYN2Iu-qkqPVGsEGony955zdEo6T3oOUKgNvT5WZvw6w_WU3aNdyLNh0xh-wRi4TNi6T70jutWPs6qkrRIQbdKBlQ6Yk1ROLxYnfSYF0BoUn9chrbaMj73dJleIqEzjxmi-9nY",
"width" : 816
}
],
"place_id" : "ChIJt7FX9rJqkFQR-R2iORwxyXQ",
"reference" : "CnRvAAAA8b_D2XlfYEVl6Oi5uYGRdJILg6dW3oRrLu8Q9xd9EukH2omaTGNlupKgahjJREN5jiNKdj9FSmpR4F0Du2XMbW9xmMEpO6CmPEG8zbBHQVNBs2zHzU2_m0WFnLv21mdxfAd20GVX6f6cdZ2COkb_KhIQhh6kZ-vny430yF0EvmH7eBoUZ3TVjq_RHxXlY_jpaN1qk2Yc3UM",
"scope" : "GOOGLE",
"types" : [
"convenience_store",
"grocery_or_supermarket",
"food",
"store",
"establishment"
],
"vicinity" : "1506 Pike Pl #509, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.608918,
"lng" : -122.340729
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "301d23a6579067833036e39ef46357e1d8e5ba68",
"name" : "Frank's Quality Produce",
"place_id" : "ChIJpdHb8LJqkFQRo8X7RoXoz2U",
"reference" : "CoQBeQAAAKEBHFRM9OPUb4CWC5bC1HYamXOFc0lzWyWCX-4eyhtOOIEvcxHIsj4y1xkBiXTIsttPetJYNr5KKux3Y3q5ZF7SKutTqiRYbk0hESohet1_EdcnPU31_IBRLo9W-T_7wiDb3FnN24rYmz9J9Kz7pxhlyKyR2VLMuTLMALNLJC2YEhChDDy463zp0HRiZaAsFSG3GhTe1n0deejJDnxNB2AK4zOf1HGkMw",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1508 Pike Pl, Seattle"
},
{
"geometry" : {
"location" : {
"lat" : 47.609231,
"lng" : -122.340848
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "228c792a04f0d9c3b8086b9f857521945f87c975",
"name" : "Choice Produce",
"place_id" : "ChIJmbBY97JqkFQR4JkuKXVDT3A",
"reference" : "CnRwAAAAWE1aK6eOe-rdRjgs2HJZHB__viCdWu2u7Qx0chME1CgDnnOycW_dcim98Szb2bzUjTy12wGk9fGO42jpywL-Zevm_ilfqi6q7jvRQxSGvsybbZau_Tc_orGj41Gz8fWK1R6_YvAT1n0II9DPFca1lxIQviRIxzWL3GRqyK3dun_4zRoUrNDpGHpGZHKUngGPiBLLH46zOqI",
"scope" : "GOOGLE",
"types" : [ "grocery_or_supermarket", "food", "store", "establishment" ],
"vicinity" : "1514 Pike Pl, Seattle"
}
],
"status" : "OK"
}
I need to parse it and get the lat and long values
I have assigned it like this
JSONArray JA=new JSONArray(result);
JSONObject json= null;
Where result contains the web service response. How can I parse it and get the required values?
try this code.you will get lat and lng value:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
results = jsonObj.getJSONArray("result");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
JSONObject geo = c.getJSONObject("geomatrics");
JSONObject place = geo.getJSONObject("location");
String id = place.getString(lat);
String name = place.getString(lng);
}}}
FYI, the response JSON is an Object, not an Array.
Steps to get lat/lng values:
Get results JSON array from received JSON Object response
Iterate through all the sub JSON Objects, then first get geometry sub object.
From geometry sub object, get location object. From location object, parse and get lat/lng values.
Please don't ask for code as there are many JSON tutorials exist on web but you can use below methods for parsing:
getJSONObject()
getString()
getLong()
There are multiple locations in the JSON so you will need and ArrayList to store all of them.
I recomment using ArrayList<HashMap<String,Double>>
Assuming you have the result as a String object named as result:
ArrayList<HashMap<String, Double>> list = new ArrayList<HashMap<String,Double>>();
JSONObject jsonObj= new JSONObject(result);
JSONArray results = jsonObj.getJSONArray("results");
for(int i=0;i<results.length();i++){
JSONObject temp = results.getJSONObject(i).getJSONObject("geometry").getJSONObject("location");
Double lat = temp.getDouble("lat");
Double lng = temp.getDouble("lng");
HashMap map = new HashMap<String, Double>();
map.put("lat",lat);
map.put("lng",lng);
list.add(map);
}
now you can retrive any position by using
Double lat=list.get(4).get("lat");
Double lng=list.get(4).get("lng");
Hope it helps. cheers :)
Cast the response to JsonObject like this
JSONObject jsonObj = new JSONObject(result);// where result is the response you got
Get the result array like this
JSONArray results = jsonObj.getJSONArray("results"); // where results is the tag in resposne
Now you have the entire array in results
for loop the entire stuff
for(int i=0;i<results.length();i++)
{
JSONObject location = results.getJSONObject(i).getJSONObject("geometry").getJSONObject("location");;
String lat = location.optString("lat");
String lng = location.optString("lng");
}
If you need the lat and long inside an array, add those to an array.

Storing longitude and latitude data in android

I have to store longitude and latitude of all cities of a country in android application
when user will select a city i have to return the longitude and latitude of selected city.
what will be the most efficient way to do this?
or is there any resources where i enter a country and it will show all cities with their longitude and latitude..?
You can use Google Geocoding API : https://developers.google.com/maps/documentation/geocoding/
For example :
https://maps.googleapis.com/maps/api/geocode/json?address=Berlin&sensor=true
And parse output using GSON
{
"results" : [
{
"address_components" : [
{
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Niemcy",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Berlin, Niemcy",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.6754542,
"lng" : 13.7611176
},
"southwest" : {
"lat" : 52.33962959999999,
"lng" : 13.0911663
}
},
"location" : {
"lat" : 52.52000659999999,
"lng" : 13.404954
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 52.6754542,
"lng" : 13.7611176
},
"southwest" : {
"lat" : 52.33962959999999,
"lng" : 13.0911663
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}

Geting json response from Google places API in android

My simple android application uses google places api to get the latitude and longitude of a place. I am getting the json response from the google api as below
{
"results" : [
{
"address_components" : [
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Chennai, Tamil Nadu, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 13.23408510,
"lng" : 80.33229129999999
},
"southwest" : {
"lat" : 12.94592670,
"lng" : 80.14878270
}
},
"location" : {
"lat" : 13.0604220,
"lng" : 80.2495830
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 13.14736150,
"lng" : 80.37764240
},
"southwest" : {
"lat" : 12.97345190,
"lng" : 80.12152360
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
Now i want to get the this part in the json response,
"location" : {
"lat" : 13.0604220,
"lng" : 80.2495830
}
I only get the whole "geometry" part as string through this method
JSONObject e = address.getJSONObject(0);
e.getString("geometry")
How to use the only location values. Any help is appreciated. I'm not sure what is going wrong.
JSONObject e = address.getJSONObject(0);
JSONObject jsonLocation = e.getJSONObject("geometry").getJSONObject("location");
and use
String lat = jsonLocation.getString("lat");
String lng = jsonLocation.getString("lng");
to get latitude and longitude.
You can also get location values as follows:
For Longitude:
Double lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
For Latitude:
Double lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
Try this. It worked for me.
JSONObject result = jsonObject.getJSONObject("result");
Double lat = result.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
Double lng = result.getJSONObject("geometry").getJSONObject("location").getDouble("lng");

Categories

Resources