Map Search Bar in Google Maps v2
Asked Answered
K

1

9

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

Kenaz answered 12/6, 2013 at 8:34 Comment(4)
I have the maps already as well as added the Location 'button' at the far right using the setLocationEnabled()Kenaz
maybe this can help you wptrafficanalyzer.in/blog/…Angelenaangeleno
@Yume117, you should write your comment as an answer. It worked for me too. Thanks for the link!Adolfoadolph
@Angelenaangeleno I am using wptrafficanalyzer Instead of using public class MainActivity extends FragmentActivity , I am using public class MainActivity extends Activity. Everything is working fine except when I click the button to search my application gets crashed. What is going wrong ?Idel
P
3

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)));
Pharisaic answered 2/12, 2014 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.