[java] Code

Viewer

  1. package com.example.mapandlocationdemo;
  2.  
  3. import androidx.annotation.NonNull;
  4. import androidx.core.app.ActivityCompat;
  5. import androidx.core.content.ContextCompat;
  6. import androidx.fragment.app.FragmentActivity;
  7.  
  8. import android.Manifest;
  9. import android.content.pm.PackageManager;
  10. import android.location.Location;
  11. import android.location.LocationListener;
  12. import android.location.LocationManager;
  13. import android.os.Build;
  14. import android.os.Bundle;
  15. import android.widget.Toast;
  16.  
  17. import com.google.android.gms.maps.CameraUpdateFactory;
  18. import com.google.android.gms.maps.GoogleMap;
  19. import com.google.android.gms.maps.OnMapReadyCallback;
  20. import com.google.android.gms.maps.SupportMapFragment;
  21. import com.google.android.gms.maps.model.LatLng;
  22. import com.google.android.gms.maps.model.MarkerOptions;
  23.  
  24. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  25.  
  26.  
  27.     @Override
  28.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  29.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  30.         if (requestCode==1){
  31.             if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
  32.                 if (ContextCompat.checkSelfPermission(thisManifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  33.                     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER0,10,locationListener);
  34.                 }
  35.             }
  36.         }
  37.     }
  38.  
  39.     private GoogleMap mMap;
  40.     LocationManager locationManager;
  41.     LocationListener locationListener;
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_maps);
  46.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  47.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  48.                 .findFragmentById(R.id.map);
  49.         mapFragment.getMapAsync(this);
  50.  
  51.  
  52.     }
  53.  
  54.  
  55.     /**
  56.      * Manipulates the map once available.
  57.      * This callback is triggered when the map is ready to be used.
  58.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  59.      * we just add a marker near Sydney, Australia.
  60.      * If Google Play services is not installed on the device, the user will be prompted to install
  61.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  62.      * installed Google Play services and returned to the app.
  63.      */
  64.     @Override
  65.     public void onMapReady(GoogleMap googleMap) {
  66.         mMap = googleMap;
  67.  
  68.         locationManager= (LocationManager) this.getSystemService(LOCATION_SERVICE);
  69.         locationListener=new LocationListener() {
  70.             @Override
  71.             public void onLocationChanged(@NonNull Location location) {
  72.                 Toast.makeText(MapsActivity.this, location.toString(), Toast.LENGTH_SHORT).show();
  73.             }
  74.  
  75.             @Override
  76.             public void onStatusChanged(String provider, int status, Bundle extras) {
  77.  
  78.             }
  79.  
  80.             @Override
  81.             public void onProviderEnabled(@NonNull String provider) {
  82.  
  83.             }
  84.  
  85.             @Override
  86.             public void onProviderDisabled(@NonNull String provider) {
  87.  
  88.             }
  89.  
  90.  
  91.         };
  92.         if (ContextCompat.checkSelfPermission(thisManifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
  93.             ActivityCompat.requestPermissions(thisnew String[]{Manifest.permission.ACCESS_FINE_LOCATION}1);
  94.         }else {
  95.             locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER0,0,locationListener);
  96.         }
  97.         // Add a marker in Sydney and move the camera
  98.         LatLng sydney = new LatLng(-34151);
  99.         mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
  100.         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 20));
  101.     }
  102. }

Editor

You can edit this paste and save as new:


File Description
  • Code
  • Paste Code
  • 21 Oct-2020
  • 4.03 Kb
You can Share it: