Advertisement
Guest User

MapsActivity.java

a guest
Oct 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. package com.example.deanb.findmyseshmap;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.location.Location;
  6. import android.os.Build;
  7. import android.support.annotation.NonNull;
  8. import android.support.annotation.Nullable;
  9. import android.support.v4.app.ActivityCompat;
  10. import android.support.v4.app.FragmentActivity;
  11. import android.os.Bundle;
  12. import android.support.v4.content.ContentResolverCompat;
  13. import android.support.v4.content.ContextCompat;
  14. import android.widget.Toast;
  15.  
  16. import com.google.android.gms.common.ConnectionResult;
  17. import com.google.android.gms.location.LocationListener;
  18. import com.google.android.gms.common.api.GoogleApiClient;
  19. import com.google.android.gms.location.LocationRequest;
  20. import com.google.android.gms.location.LocationServices;
  21. import com.google.android.gms.maps.CameraUpdateFactory;
  22. import com.google.android.gms.maps.GoogleMap;
  23. import com.google.android.gms.maps.OnMapReadyCallback;
  24. import com.google.android.gms.maps.SupportMapFragment;
  25. import com.google.android.gms.maps.model.BitmapDescriptor;
  26. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  27. import com.google.android.gms.maps.model.LatLng;
  28. import com.google.android.gms.maps.model.Marker;
  29. import com.google.android.gms.maps.model.MarkerOptions;
  30.  
  31. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
  32. GoogleApiClient.ConnectionCallbacks,
  33. GoogleApiClient.OnConnectionFailedListener,
  34. LocationListener {
  35.  
  36. private GoogleMap mMap;
  37. private GoogleApiClient client;
  38. private LocationRequest locationRequest;
  39. private Location lastLocation;
  40. private Marker currentLocationMarker;
  41. public static final int REQUEST_LOCATION_CODE = 99;
  42.  
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.activity_maps);
  47.  
  48. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  49. checkLocationPermission();
  50. }
  51.  
  52. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  53. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  54. .findFragmentById(R.id.map);
  55. mapFragment.getMapAsync(this);
  56. }
  57.  
  58. @Override
  59. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  60. switch(requestCode) {
  61. case REQUEST_LOCATION_CODE:
  62. if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  63. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  64. if(client == null) {
  65. buildGoogleApiClient();
  66. }
  67. mMap.setMyLocationEnabled(true);
  68. }
  69. }
  70. else {
  71. Toast.makeText(this, "Permission Denied", Toast.LENGTH_LONG).show();
  72. }
  73. return;
  74. }
  75. }
  76.  
  77.  
  78. /**
  79. * Manipulates the map once available.
  80. * This callback is triggered when the map is ready to be used.
  81. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  82. * we just add a marker near Sydney, Australia.
  83. * If Google Play services is not installed on the device, the user will be prompted to install
  84. * it inside the SupportMapFragment. This method will only be triggered once the user has
  85. * installed Google Play services and returned to the app.
  86. */
  87. @Override
  88. public void onMapReady(GoogleMap googleMap) {
  89. mMap = googleMap;
  90.  
  91. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  92. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  93. buildGoogleApiClient();
  94. mMap.setMyLocationEnabled(true);
  95. }
  96. }
  97. else {
  98. buildGoogleApiClient();
  99. mMap.setMyLocationEnabled(true);
  100. }
  101. // Add a marker in Sydney and move the camera
  102. LatLng sydney = new LatLng(-34, 151);
  103. mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
  104. mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
  105. }
  106.  
  107. protected synchronized void buildGoogleApiClient() {
  108. client = new GoogleApiClient.Builder(this)
  109. .addConnectionCallbacks(this)
  110. .addOnConnectionFailedListener(this)
  111. .addApi(LocationServices.API)
  112. .build();
  113.  
  114. client.connect();
  115. }
  116.  
  117. @Override
  118. public void onLocationChanged(Location location) {
  119. lastLocation = location;
  120.  
  121. if(currentLocationMarker != null) {
  122. currentLocationMarker.remove();
  123. }
  124.  
  125. LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
  126.  
  127. MarkerOptions markerOptions = new MarkerOptions();
  128. markerOptions.position(latLng);
  129. markerOptions.title("Current Location");
  130. markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
  131.  
  132. currentLocationMarker = mMap.addMarker(markerOptions);
  133.  
  134. mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  135. mMap.animateCamera(CameraUpdateFactory.zoomBy(10));
  136.  
  137. if(client != null) {
  138. LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
  139. }
  140.  
  141. }
  142.  
  143.  
  144. @Override
  145. public void onConnected(@Nullable Bundle bundle) {
  146. locationRequest = new LocationRequest();
  147.  
  148. locationRequest.setInterval(100);
  149. locationRequest.setFastestInterval(100);
  150. locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  151.  
  152. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  153. LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
  154. }
  155.  
  156. }
  157.  
  158. public boolean checkLocationPermission() {
  159. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  160. if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
  161. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_CODE);
  162. }
  163. else {
  164. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_CODE);
  165.  
  166. }
  167. return false;
  168. }
  169. else {
  170. return true;
  171. }
  172. }
  173.  
  174. @Override
  175. public void onConnectionSuspended(int i) {
  176.  
  177. }
  178.  
  179. @Override
  180. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  181.  
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement