Keywords: Android | Google Maps | Location Services | Runtime Permissions | FusedLocationProvider
Abstract: This technical article explores the challenges and solutions for showing the user's current location on Google Maps in Android Marshmallow. It covers runtime permission handling, the use of FusedLocationProvider for efficient location retrieval, and provides a comprehensive code example with step-by-step explanations.
Introduction
With the introduction of Android Marshmallow (API level 23), developers faced new challenges in handling permissions, particularly for location-based features. This article addresses how to effectively display the current location on Google Maps while adhering to modern Android standards.
Runtime Permission Handling in Android 6.0+
Android Marshmallow requires apps to request permissions at runtime for sensitive data like location. This section explains the necessity of checking and requesting the ACCESS_FINE_LOCATION permission using ContextCompat.checkSelfPermission() and ActivityCompat.requestPermissions(). It is crucial to handle user responses and provide explanations when needed.
Leveraging FusedLocationProvider for Location Data
Google Play Services offers the FusedLocationProvider API, which is recommended over the traditional LocationManager due to its battery efficiency and accuracy. This API combines GPS, Wi-Fi, and cell tower data to provide optimal location updates.
Step-by-Step Implementation
To integrate current location display in Google Maps, follow these steps:
- Set up the Google Maps API in your project and add necessary permissions in the manifest.
- Implement the
OnMapReadyCallbackinterface and initialize the map. - Check for location permission; if granted, enable location on the map and request updates.
- Build a
GoogleApiClientto connect to Location Services. - Handle location changes and update the map with a marker at the current position.
Below is a rewritten code example based on the core concepts:
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, LocationListener {
private GoogleMap googleMap;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
this.googleMap = map;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
initializeLocationServices();
} else {
requestLocationPermission();
}
}
private void initializeLocationServices() {
buildGoogleApiClient();
googleMap.setMyLocationEnabled(true);
startLocationUpdates();
}
private void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.build();
googleApiClient.connect();
}
private void startLocationUpdates() {
locationRequest = LocationRequest.create()
.setInterval(1000)
.setFastestInterval(1000)
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (googleApiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
}
@Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
googleMap.addMarker(new MarkerOptions().position(latLng).title("Current Location"));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
}
// Other methods for permission handling and callbacks
}This code demonstrates a simplified version focusing on key aspects. Ensure to handle permission callbacks and error cases appropriately.
Conclusion
Successfully displaying the current location on Google Maps in Android Marshmallow requires careful attention to runtime permissions and the use of efficient location APIs like FusedLocationProvider. By following the outlined steps and best practices, developers can create robust and user-friendly location-based applications.