2013年6月28日 星期五

android : 確認google定位權限

原文參考在此
===================跳去權限設定畫面=======================
註:
Explanation of above code.

1. First we need to create LocationManager.

LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
2. using isProviderEnabled Check location providers are already enabled or not.
  1. private void checkLocationProviders(){  
  2.     //String provider = Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);  
  3.        if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){  
  4.              
  5.            Toast.makeText(EnableLocationActivity.this"GPS provider Enabled: ",Toast.LENGTH_LONG).show();  
  6.           
  7.        }else if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){  
  8.              
  9.            Toast.makeText(EnableLocationActivity.this"Network provider Enabled: ",Toast.LENGTH_LONG).show();  
  10.              
  11.        }else{  
  12.            AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  13.            builder.setMessage("Location providers are not available. Enable GPS or network providers.")  
  14.                   .setCancelable(false)  
  15.                   .setPositiveButton("Yes"new DialogInterface.OnClickListener() {  
  16.                       public void onClick(DialogInterface dialog, int id) {  
  17.                           Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
  18.                       startActivityForResult(intent, 1);  
  19.                       }  
  20.                   })  
  21.                   .setNegativeButton("No"new DialogInterface.OnClickListener() {  
  22.                       public void onClick(DialogInterface dialog, int id) {  
  23.                           EnableLocationActivity.this.finish();  
  24.                       }  
  25.                   }).show();  
  26.              
  27.              
  28.        }  
  29.   
  30.    }  
  31.      
  32.    @Override  
  33. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  34.        checkLocationProviders();  
  35. super.onActivityResult(requestCode, resultCode, data);  
  36. }  
以下原文參考

=========設定好之後,可以進行Google location功能了=================================
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Getting Google Play availability status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();

    }else { // Google Play Services are available

        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);

        LocationListener locationListener = new LocationListener() {
          void onLocationChanged(Location location) {
          // redraw the marker when get location update.
          drawMarker(location);
        }

        if(location!=null){
           //PLACE THE INITIAL MARKER              
           drawMarker(location);
        }
        locationManager.requestLocationUpdates(provider, 20000, 0, locationListener);
    }
}

private void drawMarker(Location location){
googleMap.clear();
LatLng currentPosition = new LatLng(location.getLatitude(),
location.getLongitude());
googleMap.addMarker(new MarkerOptions()
.position(currentPosition)
.snippet("Lat:" + location.getLatitude() + "Lng:"+ location.getLongitude()));
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
    .title("ME"));
}

沒有留言:

張貼留言