-
-
Save anonymous/07ea5c0d0c05817da1bfa416e6a7b23b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ie.doubleh.mapsdemo; | |
import android.content.pm.PackageManager; | |
import android.location.Location; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.app.FragmentActivity; | |
import android.os.Bundle; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.LocationListener; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationServices; | |
import com.google.android.gms.maps.CameraUpdateFactory; | |
import com.google.android.gms.maps.GoogleMap; | |
import com.google.android.gms.maps.OnMapReadyCallback; | |
import com.google.android.gms.maps.SupportMapFragment; | |
import com.google.android.gms.maps.model.BitmapDescriptorFactory; | |
import com.google.android.gms.maps.model.LatLng; | |
import com.google.android.gms.maps.model.MarkerOptions; | |
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, | |
GoogleApiClient.OnConnectionFailedListener, LocationListener { | |
private static final int LOCATION_PERMISSION = 0; | |
private static final int LOCATION_UPDATES_PERMISSION = 1; | |
private GoogleMap mMap; | |
private GoogleApiClient mGoogleApiClient; | |
private LocationRequest mLocationRequest; | |
private Location currentLocation; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_maps); | |
// Obtain the SupportMapFragment and get notified when the map is ready to be used. | |
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() | |
.findFragmentById(R.id.map); | |
mapFragment.getMapAsync(this); | |
buildGoogleApiClient(); | |
createLocationRequest(); | |
} | |
/** | |
* Manipulates the map once available. | |
* This callback is triggered when the map is ready to be used. | |
* This is where we can add markers or lines, add listeners or move the camera. In this case, | |
* we just add a marker near Sydney, Australia. | |
* If Google Play services is not installed on the device, the user will be prompted to install | |
* it inside the SupportMapFragment. This method will only be triggered once the user has | |
* installed Google Play services and returned to the app. | |
*/ | |
@Override | |
public void onMapReady(GoogleMap googleMap) { | |
mMap = googleMap; | |
if (currentLocation != null) { | |
updateMapLocation(); | |
} | |
} | |
private void updateMapLocation() { | |
LatLng currentLatLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()); | |
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); | |
mMap.clear(); | |
mMap.addMarker(new MarkerOptions().position(currentLatLng).title("Current Location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); | |
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 12)); | |
} | |
private void buildGoogleApiClient() { | |
// Create an instance of GoogleAPIClient. | |
if (mGoogleApiClient == null) { | |
mGoogleApiClient = new GoogleApiClient.Builder(this) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.addApi(LocationServices.API) | |
.build(); | |
} | |
} | |
protected void onStart() { | |
mGoogleApiClient.connect(); | |
super.onStart(); | |
} | |
protected void onStop() { | |
mGoogleApiClient.disconnect(); | |
super.onStop(); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
stopLocationUpdates(); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
if (mGoogleApiClient.isConnected()) { | |
startLocationUpdates(); | |
} | |
} | |
protected void stopLocationUpdates() { | |
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); | |
} | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION); | |
} else { | |
currentLocation = LocationServices.FusedLocationApi.getLastLocation( | |
mGoogleApiClient); | |
startLocationUpdates(); | |
if (mMap != null) { | |
updateMapLocation(); | |
} | |
} | |
} | |
protected void startLocationUpdates() { | |
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_UPDATES_PERMISSION); | |
} else { | |
LocationServices.FusedLocationApi.requestLocationUpdates( | |
mGoogleApiClient, mLocationRequest, this); | |
} | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
switch (requestCode) { | |
case LOCATION_PERMISSION: | |
{ | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
onConnected(null); | |
} | |
} | |
return; | |
case LOCATION_UPDATES_PERMISSION: | |
{ | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
startLocationUpdates(); | |
} | |
} | |
} | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
buildGoogleApiClient(); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
currentLocation = location; | |
updateMapLocation(); | |
} | |
protected void createLocationRequest() { | |
mLocationRequest = new LocationRequest(); | |
mLocationRequest.setInterval(100); | |
mLocationRequest.setSmallestDisplacement(1F); | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment