Elina Mohanty Elina Mohanty - 5 months ago 42
Android Question

Getting current Location in Android API level 23

Hello i am completely new in android location concept, i have created one map activity in android studio. this is the code.

public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

mMap.setMyLocationEnabled(true);

Location location = null;
LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(currentPosition).title("Current Location"));


Is this is correct @Abhishek

Answer

You can use like this

@Override
public void onMapReady(GoogleMap map) {
    // TODO Auto-generated method stub
    map.setMyLocationEnabled(true);
    LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude());
    map.addMarker(new MarkerOptions().position(currentPosition).title("Current Location"));
}

location which get from onLocationChanged method

@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
 location = new LatLng(loc.getLatitude(), loc.getLongitude());

 ----------

}
Comments