Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. package com.deep.profilemaper;
  2.  
  3. import android.content.Intent;
  4. import android.content.pm.PackageManager;
  5. import android.location.Location;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.support.annotation.NonNull;
  9. import android.support.annotation.Nullable;
  10. import android.support.v4.app.ActivityCompat;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.Button;
  15.  
  16. import com.google.android.gms.common.ConnectionResult;
  17. import com.google.android.gms.common.api.GoogleApiClient;
  18. import com.google.android.gms.location.LocationServices;
  19. import com.google.android.gms.maps.CameraUpdateFactory;
  20. import com.google.android.gms.maps.GoogleMap;
  21. import com.google.android.gms.maps.OnMapReadyCallback;
  22. import com.google.android.gms.maps.SupportMapFragment;
  23. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  24. import com.google.android.gms.maps.model.LatLng;
  25. import com.google.android.gms.maps.model.Marker;
  26. import com.google.android.gms.maps.model.MarkerOptions;
  27.  
  28.  
  29. public class AddVenueActivity extends AppCompatActivity implements OnMapReadyCallback,
  30. GoogleApiClient.ConnectionCallbacks,
  31. GoogleApiClient.OnConnectionFailedListener,
  32. GoogleMap.OnMarkerDragListener,
  33. GoogleMap.OnMarkerClickListener,View.OnClickListener {
  34.  
  35.  
  36. private GoogleApiClient mGoogleApiClient;
  37. private GoogleMap googleMap;
  38.  
  39. private Double latitude = 0d, longitude = 0d;
  40. private Button saveLocationButton, viewSavedLocations;
  41.  
  42. private static final String TAG = "AddVenueActivity";
  43.  
  44. private static final int REQUEST_LOCATION = 1;
  45.  
  46.  
  47. @Override
  48. protected void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. requestLocationPermission();
  51. setContentView(R.layout.activity_add_venue);
  52.  
  53.  
  54. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  55. .findFragmentById(R.id.map);
  56. mapFragment.getMapAsync(this);
  57.  
  58. //Initializing googleapi client
  59. mGoogleApiClient = new GoogleApiClient.Builder(this)
  60. .addConnectionCallbacks(this)
  61. .addOnConnectionFailedListener(this)
  62. .addApi(LocationServices.API)
  63. .build();
  64.  
  65. saveLocationButton = (Button) findViewById(R.id.save_btn);
  66. saveLocationButton.setOnClickListener(this);
  67.  
  68. Intent targetIntent = new Intent(this, LocationsUpdateService.class);
  69. startService(targetIntent);
  70.  
  71. }
  72.  
  73. @Override
  74. protected void onStart() {
  75. mGoogleApiClient.connect();
  76. super.onStart();
  77. }
  78.  
  79. @Override
  80. protected void onStop() {
  81. mGoogleApiClient.disconnect();
  82. super.onStop();
  83. }
  84.  
  85. @Override
  86. public void onMapReady(GoogleMap googleMap) {
  87. //Initializing our map
  88. this.googleMap = googleMap;
  89. 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) {
  90. googleMap.setMyLocationEnabled(true);
  91. Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  92. // Toast.makeText(this,"Location"+location.getLongitude()+": "+location.getLatitude(),Toast.LENGTH_LONG).show();
  93. if (location != null) {
  94.  
  95. longitude = location.getLongitude();
  96. latitude = location.getLatitude();
  97.  
  98. if (latitude != 0 && longitude != 0) {
  99. googleMap.clear();
  100.  
  101. Marker currentLocationMarker = googleMap.addMarker(new MarkerOptions().position(
  102. new LatLng(latitude, longitude)).icon(
  103. BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
  104.  
  105. googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));
  106.  
  107. googleMap.animateCamera(CameraUpdateFactory.zoomTo(17));
  108.  
  109. currentLocationMarker.setDraggable(true);
  110. googleMap.setOnMarkerClickListener(this);
  111. googleMap.setOnMarkerDragListener(this);
  112.  
  113. }
  114.  
  115. }
  116.  
  117. }
  118. }
  119.  
  120. @Override
  121. public void onConnected(@Nullable Bundle bundle) {
  122.  
  123. getCurrentLocation();
  124.  
  125. }
  126.  
  127.  
  128. @Override
  129. public void onConnectionSuspended(int i) {
  130.  
  131. }
  132.  
  133. @Override
  134. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  135.  
  136. }
  137.  
  138. @Override
  139. public void onMarkerDragStart(Marker marker) {
  140.  
  141. }
  142.  
  143. @Override
  144. public void onMarkerDrag(Marker marker) {
  145.  
  146. }
  147.  
  148. @Override
  149. public void onMarkerDragEnd(Marker marker) {
  150.  
  151. LatLng lastMarker = marker.getPosition();
  152. latitude = lastMarker.latitude;
  153. longitude = lastMarker.longitude;
  154.  
  155. Log.d("Custom Location :" ,"Latitude :"+latitude+" Longitude : "+longitude);
  156. }
  157.  
  158. //Getting current location
  159. private void getCurrentLocation() {
  160. //Creating a location object
  161.  
  162. 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) {
  163.  
  164. Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  165. // Toast.makeText(this,"Location"+location.getLongitude()+": "+location.getLatitude(),Toast.LENGTH_LONG).show();
  166. if (location != null) {
  167.  
  168. longitude = location.getLongitude();
  169. latitude = location.getLatitude();
  170.  
  171. if (latitude != 0 && longitude != 0) {
  172. googleMap.clear();
  173.  
  174. Marker currentLocationMarker = googleMap.addMarker(new MarkerOptions().position(
  175. new LatLng(latitude, longitude)).icon(
  176. BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
  177.  
  178. googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));
  179.  
  180. googleMap.animateCamera(CameraUpdateFactory.zoomTo(17));
  181.  
  182. currentLocationMarker.setDraggable(true);
  183. googleMap.setOnMarkerClickListener(this);
  184. googleMap.setOnMarkerDragListener(this);
  185.  
  186. }
  187.  
  188. }
  189.  
  190. }
  191. }
  192.  
  193. @Override
  194. public boolean onMarkerClick(Marker marker) {
  195.  
  196. Log.d("Current Location :" ,"Latitude :"+latitude+" Longitude : "+longitude);
  197.  
  198. Intent i = new Intent(this, AddZoneActivity.class);
  199. i.putExtra(AppConstants.KEY_LATITUDE, latitude);
  200. i.putExtra(AppConstants.KEY_LONGITUDE, longitude);
  201. startActivity(i);
  202. return true;
  203. }
  204.  
  205. @Override
  206. public void onClick(View view) {
  207.  
  208. switch (view.getId())
  209. {
  210. case R.id.save_btn:
  211. Intent i = new Intent(this, AddZoneActivity.class);
  212. i.putExtra(AppConstants.KEY_LATITUDE, latitude);
  213. i.putExtra(AppConstants.KEY_LONGITUDE, longitude);
  214. startActivity(i);
  215. break;
  216. }
  217.  
  218. }
  219.  
  220. public boolean requestLocationPermission() {
  221. if (Build.VERSION.SDK_INT >= 23) {
  222. if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
  223. == PackageManager.PERMISSION_GRANTED) {
  224. Log.v(TAG, "Permission is granted");
  225. return true;
  226. } else {
  227.  
  228. Log.v(TAG, "Permission is revoked");
  229. ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
  230. return false;
  231. }
  232. } else {
  233. //permission is automatically granted on sdk<23 upon installation
  234. Log.v(TAG, "Permission is granted");
  235. return true;
  236. }
  237. }
  238.  
  239. @Override
  240. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  241. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  242.  
  243. switch (requestCode) {
  244.  
  245. case REQUEST_LOCATION:
  246.  
  247. if (grantResults.length > 0
  248. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  249.  
  250.  
  251. 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) {
  252.  
  253. Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  254.  
  255. if (location != null) {
  256.  
  257. longitude = location.getLongitude();
  258. latitude = location.getLatitude();
  259.  
  260. if (latitude != 0 && longitude != 0) {
  261. googleMap.clear();
  262.  
  263. Marker currentLocationMarker = googleMap.addMarker(new MarkerOptions().position(
  264. new LatLng(latitude, longitude)).icon(
  265. BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
  266.  
  267. googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));
  268.  
  269. googleMap.animateCamera(CameraUpdateFactory.zoomTo(17));
  270.  
  271. currentLocationMarker.setDraggable(true);
  272. googleMap.setOnMarkerClickListener(this);
  273. googleMap.setOnMarkerDragListener(this);
  274.  
  275. }
  276.  
  277. }
  278.  
  279. }
  280. }
  281. else
  282. {
  283. this.finish();
  284. }
  285. break;
  286.  
  287.  
  288. default:
  289. break;
  290. }
  291. }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement