Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Google Map Markers outside of onMapReady() in Android?

I have the following function that returns me the current location of device:

void getCurrentLocation()
{
    Location myLocation  = map.getMyLocation();
    if(myLocation!=null)
    {
        double dLatitude = myLocation.getLatitude();
        double dLongitude = myLocation.getLongitude();
        map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                .title("My Location").icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

    }
    else
    {
        Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
    }
}

but some methods are showing in red like it were undefined: this

As you can notice, these methods are related with map, it work in onMapReady() function but out of it show it unrecognized. Why is that? What libraries I have to add? I declare map like this:

private MapFragment map;
like image 232
Rene Limon Avatar asked Jul 22 '15 18:07

Rene Limon


2 Answers

Here is what your general code structure should look like. The important part is to assign your local map reference to the one returned in the onMapReady() callback.

public class MainActivity extends Activity 
        implements OnMapReadyCallback {

    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);

        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap retMap) {

        map = retMap;

        setUpMap();

    }

    public void setUpMap(){

        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        map.setMyLocationEnabled(true);
    }

    void getCurrentLocation()
    {
        Location myLocation  = map.getMyLocation();
        if(myLocation!=null)
        {
            double dLatitude = myLocation.getLatitude();
            double dLongitude = myLocation.getLongitude();
            map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                    .title("My Location").icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

        }
        else
        {
            Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
        }
    }

}
like image 142
Daniel Nugent Avatar answered Nov 03 '22 00:11

Daniel Nugent


Why are you using

 private MapFragment map;

Your map should be of type

com.google.android.gms.maps.GoogleMap

Just change

private MapFragment map;

to

private GoogleMap map;

and get the map like the following:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

It will work fine.

like image 39
Karthik Avatar answered Nov 03 '22 00:11

Karthik