Skip to content Skip to sidebar Skip to footer

Android-how To Set Location In Map

I have this map and I want to set the location to the lat , lon that I receive in the Intent extras.How can I do it? public class MapActivity extends AppCompatActivity implements O

Solution 1:

put this code in onMapReady

double lat = location.getLatitude();
double lng= location.getLongitude();
// set up marker and add
googleMap.addMarker(newMarkerOptions().position(newLatLng(lat , lng))
            .title("Location").icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
// move camera to it
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(newLatLng(lat, lng), 8));

always do all map-related features like drawing polygons, adding markers, moving camera etc. after onMapReady call, so when googleMap won't be null. And remember that even if it gets called every time for you - in some circulumstances it may not get fired on some device and every googleMap.someMethod() call (e.g. under onClick) will throw you NullPointerException. be prepared for that

Post a Comment for "Android-how To Set Location In Map"