Skip to content Skip to sidebar Skip to footer

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) {

Solution 1:

You can use like this

@OverridepublicvoidonMapReady(GoogleMap map) {
    // TODO Auto-generated method stub
    map.setMyLocationEnabled(true);
    LatLngcurrentPosition=newLatLng(location.getLatitude(),location.getLongitude());
    map.addMarker(newMarkerOptions().position(currentPosition).title("Current Location"));
}

location which get from onLocationChanged method

@OverridepublicvoidonLocationChanged(Location loc) {
// TODO Auto-generated method stub
 location = newLatLng(loc.getLatitude(), loc.getLongitude());

 ----------

}

Solution 2:

The Google Maps API location now works, even has listeners, you can do it using that, for example:

private GoogleMap.OnMyLocationChangeListenermyLocationChangeListener=newGoogleMap.OnMyLocationChangeListener() {
    @OverridepublicvoidonMyLocationChange(Location location) {
        LatLngloc=newLatLng(location.getLatitude(), location.getLongitude());
        mMarker = mMap.addMarker(newMarkerOptions().position(loc));
        if(mMap != null){
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
        }
    }
};

and then set the listener for the map:

mMap.setOnMyLocationChangeListener(myLocationChangeListener);

Post a Comment for "Getting Current Location In Android Api Level 23"