2018年5月21日 星期一

(app)實作定位個人位置

  1. package com.xin.mygooglemap;  
  2.   
  3. import android.Manifest;  
  4. import android.content.DialogInterface;  
  5. import android.content.Intent;  
  6. import android.content.pm.PackageManager;  
  7. import android.location.Criteria;  
  8. import android.location.Location;  
  9. import android.location.LocationListener;  
  10. import android.location.LocationManager;  
  11. import android.net.Uri;  
  12. import android.os.Build;  
  13. import android.provider.Settings;  
  14. import android.support.annotation.NonNull;  
  15. import android.support.v4.app.ActivityCompat;  
  16. import android.support.v7.app.AlertDialog;  
  17. import android.support.v7.app.AppCompatActivity;  
  18. import android.os.Bundle;  
  19. import android.util.Log;  
  20. import android.view.View;  
  21. import android.widget.TextView;  
  22. import android.widget.Toast;  
  23.   
  24. public class MainActivity extends AppCompatActivity {  
  25.     private LocationManager manager;  
  26.     private final int ACCESS_FINE_LPCATION = 1678;  
  27.     private String best;  
  28.     private Location currentLocation;  
  29.     private LocationListener listener = new LocationListener() {  
  30.         @Override  
  31.         public void onLocationChanged(Location location) {  
  32.             //when location change  
  33.             currentLocation = location;  
  34.             updatePosition();  
  35.         }  
  36.   
  37.         @Override  
  38.         public void onStatusChanged(String s, int i, Bundle bundle) {  
  39.   
  40.         }  
  41.   
  42.         @Override  
  43.         public void onProviderEnabled(String s) {  
  44.   
  45.         }  
  46.   
  47.         @Override  
  48.         public void onProviderDisabled(String s) {  
  49.   
  50.         }  
  51.     };  
  52.   
  53.     @Override  
  54.     protected void onCreate(Bundle savedInstanceState) {  
  55.         super.onCreate(savedInstanceState);  
  56.         setContentView(R.layout.activity_main);  
  57.         manager = (LocationManager) getSystemService(LOCATION_SERVICE);  
  58.         if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {//check Location permissions is open .  
  59.             AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  60.             builder.setMessage("is Enabled GPS ? ")  
  61.                     .setTitle("Message")  
  62.                     .setPositiveButton("yes"new DialogInterface.OnClickListener() {  
  63.                         @Override  
  64.                         public void onClick(DialogInterface dialogInterface, int i) {  
  65.                             startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));  
  66.                         }  
  67.                     })  
  68.                     .setNegativeButton("No"null)  
  69.                     .create()  
  70.                     .show();  
  71.         }  
  72.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {  
  73.             requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, ACCESS_FINE_LPCATION);  
  74.         }  
  75.   
  76.     }  
  77.   
  78.     @Override  
  79.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {  
  80.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);  
  81.         if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
  82.             Toast.makeText(this"have open Location Managetion !!", Toast.LENGTH_SHORT);  
  83.         } else {  
  84.             finish();  
  85.         }  
  86.     }  
  87.   
  88.     @Override  
  89.     protected void onResume() {  
  90.         super.onResume();  
  91.         best = manager.getBestProvider(new Criteria(), true);//get best Result  
  92.         try {  
  93.             if (manager != null) {  
  94.                 currentLocation = manager.getLastKnownLocation(best);//get last Read Location  
  95.                 manager.requestLocationUpdates(best,5000,5,listener);//自動更新 5sec更新一次,5m以上更新  
  96.             }else{  
  97.                 currentLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  98.                 manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,5,listener);  
  99.             }  
  100.         }catch (SecurityException ex){  
  101.             Log.d("","GPS permission has not been activated");  
  102.         }  
  103.        updatePosition();  
  104.     }  
  105.   
  106.     private void updatePosition() {  
  107.         TextView LocationText = findViewById(R.id.locationText);  
  108.         if(currentLocation!=null){  
  109.             //output  
  110.             LocationText.setText(String.format("Latitude: %f ,%n Longitude: %f,%n Altitude:" +  
  111.                     " %f",currentLocation.getLatitude(),currentLocation.getLongitude(),  
  112.                     currentLocation.getAltitude()));  
  113.         }else{  
  114.             LocationText.setText("Reading .....");  
  115.         }  
  116.     }  
  117.     public void openMap(View view) {  
  118.         Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(String.format("geo:%f, %f",currentLocation.getLatitude(),currentLocation.getLongitude())));  
  119.         startActivity(intent);  
  120.     }  

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。