Pankaj
Pankaj

Reputation: 2110

Implementing Android Google Map Search functionality V2

enter image description here

I want to implement search for the country/state/city or nearby places in my android map Application, I know this question has already been asked here but thought to raise it once more as I'm still struggling to find any elegant solution here on SO.I've also referred to this post but after reading some comments I lost hope.So if anyone has implemented places api in android??

Upvotes: 2

Views: 1284

Answers (1)

Kartik_Koro
Kartik_Koro

Reputation: 1287

The straightforward way is to use the Google Geocoding API. You can access it with

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA

That will return you a JSON with the location data for that address including latitude and longitude. Here's some code snippets from what I've done:

               try{
                    JSONConnectorGet jget=new JSONConnectorGet(url);
                    returnval=jget.connectClient();
                    results=returnval.getJSONArray("results");
                    resultobj=results.getJSONObject(0);
                    System.out.println(resultobj.toString());
                    geometry=resultobj.getJSONObject("geometry");
                    location=geometry.getJSONObject("location");


                    lati=location.getDouble("lat");
                    longi=location.getDouble("lng");

                }
                catch(Exception e){
                    e.printStackTrace();
                }

                a=new LatLng(lati, longi);

Here JSONConnectorGet is a class I wrote that can be used to send a HttpGet request and receive back a JSONObject. returnval is a JSONObject I've declared. url is the URL given above with address replaced with whatever is searched.

Then you have to extract the LatLng from the JSONObject. The JSONObjects and JSONArrays I've used are declared outside the try-catch block. Now I have the Latitude and Longitude of my search place in a.

Now I simply move the camera to that location.

eventmap.animateCamera(CameraUpdateFactory.newLatLngZoom(a, 3.0f));

You can add a marker at that LatLng as well if you want.

Upvotes: 2

Related Questions