Irlance_Dox

Untitled

Mar 18th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1. package com.example.hoa.maptest;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.location.Location;
  6. import android.location.LocationListener;
  7. import android.location.LocationManager;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.support.annotation.NonNull;
  11. import android.support.v4.app.ActivityCompat;
  12. import android.support.v4.app.FragmentActivity;
  13. import android.widget.Toast;
  14.  
  15. import com.google.android.gms.maps.CameraUpdateFactory;
  16. import com.google.android.gms.maps.GoogleMap;
  17. import com.google.android.gms.maps.OnMapReadyCallback;
  18. import com.google.android.gms.maps.SupportMapFragment;
  19. import com.google.android.gms.maps.model.LatLng;
  20.  
  21.  
  22. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  23.  
  24.     private GoogleMap mMap;
  25.     private final static int MY_PRECISSION_FINE_LOCATION = 101;
  26.     public static final int LOCATION_UPDATE_MIN_DISTANCE = 10;
  27.     public static final int LOCATION_UPDATE_MIN_TIME = 50;
  28.  
  29.     //位置管理器
  30.     private LocationManager mLocationManager;
  31.     //位置監控器
  32.     private LocationListener mLocationListener = new LocationListener() {
  33.         @Override
  34.         public void onLocationChanged(Location current) {
  35.             if (current != null) {
  36.                 mLocationManager.removeUpdates(mLocationListener);
  37.             }
  38.         }
  39.         @Override
  40.         public void onStatusChanged(String s, int i, Bundle bundle) {
  41.         }
  42.         @Override
  43.         public void onProviderEnabled(String s) {
  44.         }
  45.         @Override
  46.         public void onProviderDisabled(String s) {
  47.         }
  48.     };
  49.  
  50.     @Override
  51.     protected void onCreate(Bundle savedInstanceState) {
  52.         super.onCreate(savedInstanceState);
  53.         setContentView(R.layout.activity_maps);
  54.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  55.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  56.                 .findFragmentById(R.id.map);
  57.         mapFragment.getMapAsync(this);
  58.     }
  59.  
  60.     /**
  61.      * Manipulates the map once available.
  62.      * This callback is triggered when the map is ready to be used.
  63.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  64.      * we just add a marker near Sydney, Australia.
  65.      * If Google Play services is not installed on the device, the user will be prompted to install
  66.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  67.      * installed Google Play services and returned to the app.
  68.      */
  69.  
  70.     @Override
  71.     public void onMapReady(GoogleMap googleMap) {
  72.         mMap = googleMap;
  73.         /*
  74.         // Add a marker in Sydney and move the camera
  75.         LatLng sydney = new LatLng(-34, 151);
  76.         mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
  77.         mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
  78.         */
  79.         //向手機取得權限
  80.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  81.             mMap.setMyLocationEnabled(true);
  82.         } else {
  83.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  84.                 requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PRECISSION_FINE_LOCATION);
  85.             }
  86.         }
  87.  
  88.         mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  89.         getCurrentLocation();
  90.     }
  91.  
  92.  
  93.  
  94.     //取得目前座標
  95.     private void getCurrentLocation() {
  96.         boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  97.         boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  98.  
  99.         Location currentLocation = null;
  100.  
  101.         if (!(isGPSEnabled || isNetworkEnabled)) {
  102.             //Snackbar.make(mMap, R.string.error_location_provider, Snackbar.LENGTH_INDEFINITE).show();
  103.         }
  104.         else {
  105.             if (isNetworkEnabled) {
  106.                 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  107.                     return;
  108.                 }
  109.                 mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
  110.                         LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
  111.                 currentLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  112.             }
  113.  
  114.             if (isGPSEnabled) {
  115.                 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  116.                         LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
  117.                 currentLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  118.             }
  119.         }
  120.  
  121.         if (currentLocation != null){
  122.             double lat, lng;
  123.             lat = currentLocation.getLatitude();
  124.             lng = currentLocation.getLongitude();
  125.             LatLng sydney = new LatLng(lat, lng);
  126.             mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney,15));
  127.         }
  128.  
  129.     }
  130.  
  131.     //請求權限
  132.     @Override
  133.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  134.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  135.         switch (requestCode) {
  136.             case MY_PRECISSION_FINE_LOCATION:
  137.                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  138.                     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  139.                         mMap.setMyLocationEnabled(true);
  140.                     } else {
  141.                         Toast.makeText(getApplicationContext(), "This app reuires location permission to be granted", Toast.LENGTH_LONG).show();
  142.                         finish();
  143.                     }
  144.                     break;
  145.                 }
  146.         }
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment