I want to add a "search bar" at the top on Google Maps v2
as shown on this little picture I snipped from Google play.
How do I go about doing that? thanks
Search for a place on Google MapsAt the top, tap the search box and enter an address, name of a place, or choose a category, like gas stations or groceries.
If you are using the app, tap the 3 dots in the top right and select Add stop. If you're on a computer, click Add destination below the first destination. Type in the name of the new location. Add a third location.
In the Google Cloud Console, go to the Map Styles page. Select the style you want, and click Customize Style.
It's going to be a late answer but there should be an answer to this very common need.
In this what we basically need to do is use Google Geocoder
to search for the address mentioned by user in the text box.
We can do it this way,
Geocoder geo = new Geocoder(getBaseContext());
List<Address> gotAddresses = null;
try {
gotAddresses = geocoder.getFromLocationName(locationName[0], 1);
} catch (IOException e) {
e.printStackTrace();
}
This method returns a list of available addresses, in this code we are accepting only 1 address, we can get more than one addresses if needed by changing the value of last parameter in the above method. Then,
Address address = (Address) gotAddresses.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
String properAddress = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
mMap.addMarker(new MarkerOptions()
.position(new LatLng(address.getLatitude(), address.getLongitude())).draggable(true)
.title(properAddress)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With