Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map Search Bar in Google Maps v2

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.

Google Map bar..

How do I go about doing that? thanks

like image 279
Muli Avatar asked Jun 12 '13 08:06

Muli


People also ask

Where is the search bar in Google Maps?

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.

Can you search 2 things at once on Google Maps?

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.

Can you change the look of Google Maps?

In the Google Cloud Console, go to the Map Styles page. Select the style you want, and click Customize Style.


1 Answers

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)));
like image 113
gprathour Avatar answered Nov 10 '22 09:11

gprathour