Friday, June 19, 2015

Android Map, Markers

Makers can be added to the Map using GoogleMap.addMarker(markerOptions) API. Markers can receive click events, and often used as event listeners to bring up the Info Window. Marker can have draggable settings which will allow user to drag the marker. 

static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker perth = mMap.addMarker(new MarkerOptions()
        .position(PERTH)
        .draggable(true));

Below code can be used for adding marker to the map. 

SupportMapFragment fragment =  (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
Log.v(LOG_TAG, "fragment is " + fragment);
if(fragment != null)
{
    Log.v(LOG_TAG,"map framgment is not null");
    GoogleMap map = fragment.getMap();
    if(map != null)
    {
        Log.v(LOG_TAG,"map is not null");
        map.setMyLocationEnabled(true);
    }
    fragment.getMapAsync(new OnMapReadyCallback() {
        @Override
       
public void onMapReady(GoogleMap googleMap) {
            Marker marker = googleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(10,10))
                    .title("Hello world"));
        }
    });
}

Marker class also have a method remove() to remove the marker from the map.
References:

No comments:

Post a Comment