Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. private GoogleMap mMap;
  2. private GoogleApiClient googleApiClient;
  3. private LocationRequest locationRequest;
  4. private Location lastLocation;
  5. private Marker currentUserLocationMarker;
  6. private static final int Request_User_Location_Code=99;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_google_maps);
  11. if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
  12. checkUserLocationPermmision();
  13. }
  14. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  15. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  16. mapFragment.getMapAsync(this);
  17. }
  18.  
  19.  
  20. @Override
  21. public void onMapReady(GoogleMap googleMap) {
  22. mMap = googleMap;
  23. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
  24.  
  25. buildGoogleApiClient();
  26. mMap.setMyLocationEnabled(true);
  27. }
  28.  
  29. }
  30. public Boolean checkUserLocationPermmision(){
  31. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED ){
  32. if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION)){
  33. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Request_User_Location_Code );
  34. }
  35. else{
  36. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Request_User_Location_Code );
  37. }
  38. return false;
  39. }
  40. else{
  41. return true;
  42. }
  43. }
  44.  
  45. @Override
  46. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  47. switch(requestCode){
  48. case Request_User_Location_Code:
  49. if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
  50. if(ContextCompat.checkSelfPermission
  51. (this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
  52. if( googleApiClient== null){
  53. buildGoogleApiClient();
  54. }
  55. mMap.setMyLocationEnabled(true);
  56. }
  57. else{
  58. Toast.makeText(this, "pemission denied ", Toast.LENGTH_SHORT).show();
  59. }
  60. return;
  61. }
  62. }
  63. }
  64.  
  65. protected synchronized void buildGoogleApiClient(){
  66. googleApiClient= new GoogleApiClient.Builder(this)
  67. .addConnectionCallbacks(this)
  68. .addOnConnectionFailedListener(this)
  69. .addApi(LocationServices.API)
  70. .build();
  71. googleApiClient.connect();
  72. }
  73.  
  74. @Override
  75. public void onLocationChanged(Location location) {
  76. this.lastLocation= location;
  77. if(currentUserLocationMarker!= null){
  78. currentUserLocationMarker.remove();
  79. }
  80. LatLng latLng= new LatLng(location.getLatitude(),location.getLongitude());
  81. MarkerOptions markerOptions= new MarkerOptions();
  82. markerOptions.position(latLng);
  83. markerOptions.title("user current Location");
  84. markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
  85. currentUserLocationMarker= mMap.addMarker(markerOptions);
  86. mMap.moveCamera(CameraUpdateFactory.zoomBy(14));
  87.  
  88. if(googleApiClient!= null){
  89. LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
  90. }
  91.  
  92. }
  93.  
  94. public void onClick(View v){
  95. switch(v.getId()){
  96. case R.id.search_address:
  97. EditText addressField=(EditText)findViewById(R.id.location_search);
  98. String address= addressField.getText().toString();
  99. List<Address> addressList=null;
  100. MarkerOptions userMarkerOptions=new MarkerOptions();
  101. if(!TextUtils.isEmpty(address)){
  102. Geocoder geocoder= new Geocoder(this);
  103. try {
  104. addressList=geocoder.getFromLocationName(address,6);
  105. if(addressList!= null) {
  106. for (int i = 0; i < addressList.size(); i++) {
  107. Address userAddress = addressList.get(i);
  108. LatLng latLng = new LatLng(userAddress.getLatitude(), userAddress.getLongitude());
  109.  
  110. userMarkerOptions.position(latLng);
  111. userMarkerOptions.title(address);
  112. userMarkerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
  113. mMap.addMarker(userMarkerOptions);
  114. mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  115. mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
  116.  
  117.  
  118. }
  119.  
  120.  
  121. }
  122. else{
  123. Toast.makeText(this,"Location not found...",Toast.LENGTH_SHORT).show();
  124.  
  125. }
  126. }
  127. catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130.  
  131. }
  132. else{
  133. Toast.makeText(this,"please write any location name",Toast.LENGTH_SHORT).show();
  134. }
  135. break;
  136.  
  137. }
  138. }
  139.  
  140. @Override
  141. public void onConnected(@Nullable Bundle bundle) {
  142. locationRequest= new LocationRequest();
  143. locationRequest.setInterval(1100);
  144. locationRequest.setFastestInterval(1100);
  145. locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  146. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)
  147. LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,locationRequest,this);
  148.  
  149. }
  150.  
  151. @Override
  152. public void onConnectionSuspended(int i) {
  153.  
  154. }
  155.  
  156. @Override
  157. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement