Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.hoa.maptest;
- import android.Manifest;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.app.FragmentActivity;
- import android.widget.Toast;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.LatLng;
- public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
- private GoogleMap mMap;
- private final static int MY_PRECISSION_FINE_LOCATION = 101;
- public static final int LOCATION_UPDATE_MIN_DISTANCE = 10;
- public static final int LOCATION_UPDATE_MIN_TIME = 50;
- //位置管理器
- private LocationManager mLocationManager;
- //位置監控器
- private LocationListener mLocationListener = new LocationListener() {
- @Override
- public void onLocationChanged(Location current) {
- if (current != null) {
- mLocationManager.removeUpdates(mLocationListener);
- }
- }
- @Override
- public void onStatusChanged(String s, int i, Bundle bundle) {
- }
- @Override
- public void onProviderEnabled(String s) {
- }
- @Override
- public void onProviderDisabled(String s) {
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_maps);
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- }
- /**
- * Manipulates the map once available.
- * This callback is triggered when the map is ready to be used.
- * This is where we can add markers or lines, add listeners or move the camera. In this case,
- * we just add a marker near Sydney, Australia.
- * If Google Play services is not installed on the device, the user will be prompted to install
- * it inside the SupportMapFragment. This method will only be triggered once the user has
- * installed Google Play services and returned to the app.
- */
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- /*
- // Add a marker in Sydney and move the camera
- LatLng sydney = new LatLng(-34, 151);
- mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
- */
- //向手機取得權限
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
- mMap.setMyLocationEnabled(true);
- } else {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PRECISSION_FINE_LOCATION);
- }
- }
- mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
- getCurrentLocation();
- }
- //取得目前座標
- private void getCurrentLocation() {
- boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
- boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- Location currentLocation = null;
- if (!(isGPSEnabled || isNetworkEnabled)) {
- //Snackbar.make(mMap, R.string.error_location_provider, Snackbar.LENGTH_INDEFINITE).show();
- }
- else {
- if (isNetworkEnabled) {
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- return;
- }
- mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
- LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
- currentLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- }
- if (isGPSEnabled) {
- mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
- LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
- currentLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- }
- }
- if (currentLocation != null){
- double lat, lng;
- lat = currentLocation.getLatitude();
- lng = currentLocation.getLongitude();
- LatLng sydney = new LatLng(lat, lng);
- mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney,15));
- }
- }
- //請求權限
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- switch (requestCode) {
- case MY_PRECISSION_FINE_LOCATION:
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
- mMap.setMyLocationEnabled(true);
- } else {
- Toast.makeText(getApplicationContext(), "This app reuires location permission to be granted", Toast.LENGTH_LONG).show();
- finish();
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment