A.L
A.L

Reputation: 112

How to make a marker to a location in Google Maps Activity?

I'm making an app with a Google Maps Activity and I want to place a marker there. How do I set a marker to a location, for example the White House, so that it opens up the info page in google maps when you press the maps button in the right corner? My code:

LatLng latLng = new LatLng(38.897677, -77.036531);
    mMap.addMarker(new MarkerOptions().position(latLng).title("The White House"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

Upvotes: 0

Views: 716

Answers (2)

Muhammad Usman
Muhammad Usman

Reputation: 923

Here is answer of your question what I am perceived that actually you wants to implement a click Listener.

There are two ways to do it

    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener( ) {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Toast.makeText(StoreMap.this, "Info window clicked", Toast.LENGTH_SHORT).show();
    }

or use this one if you are intend to direct user just by clicking on marker.

GoogleMap.setOnMarkerClickListener(OnMarkerClickListener)

just implement as similar as above more details

Upvotes: 1

Lendl Leyba
Lendl Leyba

Reputation: 2287

So you want your app to search for places? Try Geocoding. Here's my super simplified search method:

public void onSearch(String searchLocation) {
    mGoogleMap.clear();
    List<Address> addressList = null;
    Address address = null;
    LatLng latLng = null;

    Geocoder geocoder = new Geocoder(mContext);
    try {
        addressList = geocoder.getFromLocationName(searchLocation, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    address = addressList.get(0);
    latLng = new LatLng(address.getLatitude(), address.getLongitude());
    mGoogleMap.addMarker(new MarkerOptions().position(latLng).title(searchLocation));
    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}

Upvotes: 0

Related Questions