Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. public class MapsActivity extends FragmentActivity implements com.google.android.gms.location.LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  2.  
  3. String[] perms={"android.permission.ACCESS_FINE_LOCATION","android.permission.WRITE_EXTERNAL_STORAGE"};
  4. int permsRequestCode = 200;
  5.  
  6.  
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. mGeofences = new ArrayList<Geofence>();
  12. mGeofenceCoordinates = new ArrayList<LatLng>();
  13. // double string0=prefenceSettings.getString("LatLng0", "34");
  14.  
  15. setContentView(R.layout.activity_maps);
  16. showHelpForFirstLaunch();
  17. SupportMapFragment supportMapFragment =
  18. (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
  19. LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
  20. // getting GPS status
  21. boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  22. if(!isGPSEnabled)
  23. {
  24. Utils.displayPromptForEnablingGPS(this);
  25. }
  26. Log.i("My activity", "gps is" + isGPSEnabled);
  27.  
  28. // getting network status
  29. boolean isNetworkEnabled = locationManager
  30. .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  31. Log.i("My activity", "network is" + isNetworkEnabled);
  32.  
  33. Criteria crta = new Criteria();
  34. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) {
  35. crta.setAccuracy(Criteria.ACCURACY_FINE);
  36. } else {
  37. crta.setAccuracy(Criteria.ACCURACY_MEDIUM);
  38. }
  39. /**
  40. * we have used .setAccuracy as fine for higher SDks than gingerbread .Gingerbread is used as a reference because in apks lower
  41. * than gingerbread there is very poor geo-fencing, with gingerbread google made it a lot easier for location services to be used for devleopers.
  42. * it had improved set of tools for Location Services, which included geofencing and substantially improved location discovery.
  43. */
  44. crta.setPowerRequirement(Criteria.POWER_LOW);
  45. String provider = locationManager.getBestProvider(crta, true);
  46.  
  47. /**
  48. * It request Location updates after every 5 sec or if the user traveled 10m
  49. */
  50. Log.i("My activity", "manager is " + locationManager);
  51. Log.i("My activity", "provider is " + provider);
  52. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  53. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  54.  
  55. requestPermissions(perms, permsRequestCode);
  56. // here to request the missing permissions, and then overriding
  57. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  58. // int[] grantResults)
  59. // to handle the case where the user grants the permission. See the documentation
  60. // for Activity#requestPermissions for more details.
  61. return;
  62. }
  63. }
  64. if (mGoogleApiClient == null) {
  65. mGoogleApiClient = new GoogleApiClient.Builder(this)
  66. .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
  67. .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
  68. .addApi(LocationServices.API)
  69. .build();
  70. mGoogleApiClient.connect();
  71. Log.i("Api is", "" + mGoogleApiClient);
  72. }
  73. mLocationRequest = new LocationRequest();
  74. // We want a location update every 10 seconds.
  75. mLocationRequest.setInterval(10000);
  76. // We want the location to be as accurate as possible.
  77. mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  78.  
  79.  
  80.  
  81. }
  82.  
  83.  
  84. public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
  85.  
  86. switch(permsRequestCode){
  87.  
  88. case 200:
  89.  
  90. boolean gpsAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;
  91. Toast.makeText(this,"Thanks for the permission",Toast.LENGTH_SHORT).show();
  92. boolean memoryAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;
  93.  
  94. break;
  95.  
  96. }
  97.  
  98. }
  99.  
  100. @Override
  101. public void onLocationChanged(Location location) {
  102. latitude=location.getLatitude();
  103. longitude=location.getLongitude();
  104. CameraPosition INIT =
  105. new CameraPosition.Builder()
  106. .target(new LatLng(latitude, longitude))
  107. .zoom(17.5F)
  108. .bearing(300F) // orientation
  109. .tilt(50F) // viewing angle
  110. .build();
  111. // use GooggleMap mMap to move camera into position
  112. googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(INIT));
  113. }
  114.  
  115. @Override
  116. protected void onStart() {
  117.  
  118. super.onStart();
  119. mGoogleApiClient.connect();
  120. }
  121.  
  122. @Override
  123. protected void onStop() {
  124.  
  125. super.onStop();
  126. mGoogleApiClient.disconnect();
  127.  
  128. }
  129.  
  130.  
  131. public void Add(View view) {
  132.  
  133. if (request < 3) {
  134. String[] arguments = new String[]{String.valueOf(request)};
  135. ContentValues values = new ContentValues();
  136. values.put("latitude", latitude);
  137. values.put("true","1");
  138. values.put("longitude", longitude);
  139. db.update("Coordinates", values, "id=?", arguments);
  140.  
  141. mGeofenceCoordinates.add(new LatLng(latitude, longitude));
  142. Log.i("The id is", "" + valueindex);
  143. mGeofences.add(new Geofence.Builder()
  144. // The coordinates of the center of the geofence and the radius in meters.
  145. .setRequestId("" + valueindex)
  146. .setCircularRegion(latitude, longitude, 100)
  147. .setExpirationDuration(Geofence.NEVER_EXPIRE)
  148. // Required when we use the transition type of GEOFENCE_TRANSITION_DWELL
  149. .setLoiteringDelay(50000)
  150. .setTransitionTypes(
  151. Geofence.GEOFENCE_TRANSITION_ENTER
  152. | Geofence.GEOFENCE_TRANSITION_DWELL
  153. | Geofence.GEOFENCE_TRANSITION_EXIT).build());
  154. mGeofenceStore = new GeofenceStore(this, mGeofences);
  155. valueindex++;
  156. request++;
  157.  
  158.  
  159.  
  160.  
  161. googleMap.addMarker(new MarkerOptions().snippet("Radius:100m").draggable(false).title(valueindex + "").position(new LatLng(latitude, longitude))).showInfoWindow();
  162. } else {
  163. Toast.makeText(this, "Maximum limit exceeded", Toast.LENGTH_LONG).show();
  164. }
  165. }
  166.  
  167. @Override
  168. public void onConnected(Bundle bundle) {
  169. Location mlastlocation;
  170. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  171. if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  172. requestPermissions(perms, permsRequestCode);
  173.  
  174.  
  175. return;
  176. }
  177. }
  178. mlastlocation = LocationServices.FusedLocationApi.getLastLocation(
  179. mGoogleApiClient);
  180. startLocationUpdates();
  181. if (mlastlocation != null) {
  182. Log.i("the last location:", "" + mlastlocation);
  183. // Toast.makeText(this, "Get last location first asshole!", Toast.LENGTH_LONG).show();
  184. }
  185.  
  186.  
  187. }
  188.  
  189.  
  190. protected void startLocationUpdates() {
  191. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  192. if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  193. requestPermissions(perms, permsRequestCode);
  194.  
  195. return;
  196. }
  197. LocationRequest mLocationRequest = new LocationRequest();
  198.  
  199. LocationServices.FusedLocationApi.requestLocationUpdates(
  200. mGoogleApiClient, mLocationRequest, this);
  201. }
  202. }
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement