I'm building an app where the user writes his location and the map zoom on it. For the map i'm ok but my problem is take informations from the Search View. SO, my scope is: the user writes his location, presses on search button on keyboard and the map zoom on his loaction
1 Answers
Google maps already has a location button that zooms the camera to the users location. Though I'm assuming that's not what you're looking for
I'm going to describe how to use the android widget SearchView to input text, but you'll need to use Geocoder to find the location from that text.
First, to use the search view, you must declare it in your XML file.
<SearchView
android:id="@+id/searchview_map_search"
android:width="fill_parent"
android:height="wrap_content" />
This creates a search view bar that expands when a user clicks on it, you can also use an EditText field to allow a user to input a searchable string. I'll continue with SearchView
In your MapFragment or Activity where you will be searching from, set a QueryListener to the searchView
SearchView searchView = (SearchView) item.getActionView().findViewById(R.id.serchview_map_search);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// Do something when user his enter on keyboard
view.clearFocus();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// Do something while user is entering text
return false;
}
});
String query is the input string that the user has typed. You can use that string the then move the camera to the location of the typed string. Doing this though requires another class or system. The easiest way to convert the name of a location to the LatLng coordinates of the location is to use the Geocode class: http://developer.android.com/reference/android/location/Geocoder.html I won't go into that, there are plenty of StackOverflow questions on that topic

- 6,116
- 2
- 26
- 26