How to implement search functionality for Google Map Api V2 Android?

旧城冷巷雨未停 提交于 2019-11-29 14:41:33

I just need to enter a city name, press a "Search" button and then the map should move to the city and show a marker on it. Just like the Google Maps application does.

So for that you have to make the functionality on Click of search in which you have to do as follows:

  • The city name entered by the user Will be sent to the google api - It will get back the lat-long to you.
  • You will use that lat-long to add marker as well as,
  • You will use that lat-long to animate the Gmaps Camara. That's it.
  • In this way you are able to fullfill your above requirement.

As far as I can see, moving the map to a certain point is done by calling the GoogleMap object's moveCamera() method:

 GoogleMap map = ((MapFragment) getFragmentManager()
            .findFragmentById(R.id.map)).getMap();

 LatLng sydney = new LatLng(-33.867, 151.206);

 map.setMyLocationEnabled(true);
 map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

Taken from this example.

As you can see, you'll need the latitude and longitude coordinates for creating a LatLng object. I believe you can use place search from google to obtain those coordinates, using http-requests. Example:

https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&sensor=true&key=AddYourOwnKeyHere

taken from here

which should return JSON-data for you.

For autocomplete, see this link

Edit: Wops, the place search would return xml, try using ...json?query=... instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!