Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. package com.example.stayfound;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.pm.PackageManager;
  6. import android.location.Criteria;
  7. import android.location.Location;
  8. import android.location.LocationListener;
  9. import android.location.LocationManager;
  10. import android.os.Build;
  11. import android.support.v4.app.ActivityCompat;
  12. import android.support.v4.app.FragmentActivity;
  13. import android.os.Bundle;
  14. import android.support.v4.app.FragmentManager;
  15. import android.support.v4.content.ContextCompat;
  16. import android.support.v7.app.AppCompatActivity;
  17. import android.widget.Toast;
  18.  
  19. import com.google.android.gms.maps.CameraUpdate;
  20. import com.google.android.gms.maps.CameraUpdateFactory;
  21. import com.google.android.gms.maps.GoogleMap;
  22. import com.google.android.gms.maps.MapFragment;
  23. import com.google.android.gms.maps.OnMapReadyCallback;
  24. import com.google.android.gms.maps.SupportMapFragment;
  25. import com.google.android.gms.maps.model.LatLng;
  26. import com.google.android.gms.maps.model.MarkerOptions;
  27.  
  28. public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
  29.  
  30. GoogleMap mGoogleMap;
  31. SupportMapFragment mapFrag;
  32.  
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_maps);
  37.  
  38. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  39. checkLocationPermission();
  40. }
  41.  
  42. mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  43. mapFrag.getMapAsync(this);
  44.  
  45. }
  46.  
  47. @Override
  48. public void onMapReady(GoogleMap googleMap) {
  49. mGoogleMap = googleMap;
  50. mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  51. //mGoogleMap.setTrafficEnabled(true);
  52. mGoogleMap.setIndoorEnabled(true);
  53. mGoogleMap.setBuildingsEnabled(true);
  54. mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
  55. mGoogleMap.getCameraPosition();
  56.  
  57. // Get LocationManager object from System Service LOCATION_SERVICE
  58. LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  59.  
  60. // Create a criteria object to retrieve provider
  61. Criteria criteria = new Criteria();
  62.  
  63. // Get the name of the best provider
  64. String provider = locationManager.getBestProvider(criteria, true);
  65.  
  66. // Get Current Location
  67. Location myLocation = locationManager.getLastKnownLocation(provider);
  68.  
  69. //set map type
  70. googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  71.  
  72. // Get latitude of the current location
  73. double latitude = myLocation.getLatitude();
  74.  
  75. // Get longitude of the current location
  76. double longitude = myLocation.getLongitude();
  77.  
  78. // Create a LatLng object for the current location
  79. LatLng latLng = new LatLng(latitude, longitude);
  80.  
  81. // Show the current location in Google Map
  82. googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  83.  
  84. // Zoom in the Google Map
  85. googleMap.animateCamera(CameraUpdateFactory.zoomTo(8));
  86. googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
  87.  
  88.  
  89.  
  90.  
  91. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  92. //User has previously accepted this permission
  93. if (ActivityCompat.checkSelfPermission(this,
  94. android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  95. mGoogleMap.setMyLocationEnabled(true);
  96. }
  97. } else {
  98. //Not in api-23, no need to prompt
  99. mGoogleMap.setMyLocationEnabled(true);
  100. }
  101. }
  102.  
  103. public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
  104.  
  105. public boolean checkLocationPermission() {
  106. if (ContextCompat.checkSelfPermission(this,
  107. android.Manifest.permission.ACCESS_FINE_LOCATION)
  108. != PackageManager.PERMISSION_GRANTED) {
  109.  
  110. // Should we show an explanation?
  111. if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  112. android.Manifest.permission.ACCESS_FINE_LOCATION)) {
  113.  
  114. // Show an expanation to the user *asynchronously* -- don't block
  115. // this thread waiting for the user's response! After the user
  116. // sees the explanation, try again to request the permission.
  117. // TODO: Prompt with explanation!
  118.  
  119. //Prompt the user once explanation has been shown
  120. ActivityCompat.requestPermissions(this,
  121. new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
  122. MY_PERMISSIONS_REQUEST_LOCATION);
  123.  
  124. } else {
  125. // No explanation needed, we can request the permission.
  126. ActivityCompat.requestPermissions(this,
  127. new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
  128. MY_PERMISSIONS_REQUEST_LOCATION);
  129. }
  130. return false;
  131. } else {
  132. return true;
  133. }
  134. }
  135.  
  136. @Override
  137. public void onRequestPermissionsResult(int requestCode,
  138. String permissions[], int[] grantResults) {
  139. switch (requestCode) {
  140. case MY_PERMISSIONS_REQUEST_LOCATION: {
  141. // If request is cancelled, the result arrays are empty.
  142. if (grantResults.length > 0
  143. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  144.  
  145. // permission was granted, yay!
  146. if (ActivityCompat.checkSelfPermission(this,
  147. android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  148. mGoogleMap.setMyLocationEnabled(true);
  149. }
  150. } else {
  151. // permission denied, boo! Disable the
  152. // functionality that depends on this permission.
  153. Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
  154. }
  155. return;
  156. }
  157.  
  158. }
  159. }
  160.  
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement