原文參考在此
===================跳去權限設定畫面=======================
註:
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.
- 
- private void checkLocationProviders(){  
-       
-        if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){  
-              
-            Toast.makeText(EnableLocationActivity.this, "GPS provider Enabled: ",Toast.LENGTH_LONG).show();  
-           
-        }else if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){  
-              
-            Toast.makeText(EnableLocationActivity.this, "Network provider Enabled: ",Toast.LENGTH_LONG).show();  
-              
-        }else{  
-            AlertDialog.Builder builder = new AlertDialog.Builder(this);  
-            builder.setMessage("Location providers are not available. Enable GPS or network providers.")  
-                   .setCancelable(false)  
-                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
-                       public void onClick(DialogInterface dialog, int id) {  
-                           Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
-                       startActivityForResult(intent, 1);  
-                       }  
-                   })  
-                   .setNegativeButton("No", new DialogInterface.OnClickListener() {  
-                       public void onClick(DialogInterface dialog, int id) {  
-                           EnableLocationActivity.this.finish();  
-                       }  
-                   }).show();  
-              
-              
-        }  
-   
-    }  
-      
-    @Override  
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
-        checkLocationProviders();  
- super.onActivityResult(requestCode, resultCode, data);  
- }  
以下原文參考
=========設定好之後,可以進行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"));
}
    
         
        
    
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁