2014-09-25 16 views
0

如何使條件,如果我的GPS未打開,我的應用程序將首先打開GPS選項,然後打開我的GPS後,它將繼續搜索我的位置。請提前幫助並感謝您的答案。如何使條件,如果我的GPS未打開

這裏是我的java文件

static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
static final LatLng KIEL = new LatLng(53.551, 9.993); 

GoogleMap googlemap; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 



    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map); 
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 
    if(status!=ConnectionResult.SUCCESS){ 
     int requestCode = 10; 
     Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
     dialog.show(); 
    } 
    else{ 
     SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     googlemap = mf.getMap(); 
     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); 

     if(location!=null){ 
      onLocationChanged(location); 
     } 
     locationManager.requestLocationUpdates(provider, 20000, 0, this); 

    } 
} 
@Override 
public void onLocationChanged(Location location) { 


    // Getting latitude of the current location 
    double latitude = location.getLatitude(); 

    // Getting longitude of the current location 
    double longitude = location.getLongitude(); 

    // Creating a LatLng object for the current location 
    LatLng latLng = new LatLng(latitude, longitude); 

    // Showing the current location in Google Map 
    googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

    // Zoom in the Google Map 

    googlemap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
    googlemap.addMarker(new MarkerOptions() 
    .position(new LatLng(latitude, longitude)) 
    .title("Your Here!")); 

     } 


@Override 
public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 

} 
@Override 
public void onProviderEnabled(String arg0) { 
    // TODO Auto-generated method stub 

} 
@Override 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 

} 









} 

回答

0

我推了這一點的方法如下:

/** 
* Sets up the devices location services to report location and updates the lat and long of the device if available 
*/ 
public void initialiseLocationServices() { 
    // Initialise objects 
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    boolean gps_enabled = false; 
    boolean network_enabled = false; 
    boolean hasLocation; 

    try { 
     gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    } catch (Exception ignored) { 

    } 

    try { 
     network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    } catch (Exception ignored) { 

    } 

    // I am only interested in at least one being on. 
    // hasLocation = gps_enabled || network_enabled; 
    //if(hasLocation) { my way of doing things 

    // You want to do it like this: 
    if(!gps_enabled) { 
     Intent gpsOptionsIntent = new Intent( 
     android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(gpsOptionsIntent); 
    } else { 
     SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     googlemap = mf.getMap(); 
     googlemap.setMyLocationEnabled(true); 

     // 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); 

     if(location!=null){ 
      onLocationChanged(location); 
     } 
     locationManager.requestLocationUpdates(provider, 20000, 0, this); 
    } 

注:這是僞而已,你將需要處理GPS的變化設置等,我還沒有測試過這個代碼。

0

您可以使用位置經理這個

private LocationManager locationManager; 
public boolean isGPSEnabled; 

locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE); 
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

if(isGPSEnabled) 
{ 
    if(locationManager != null) 
    { 
     //TODO 
    } 
} 
+0

可我知道我應該在哪裏插入這個代碼請感謝。 – christianbagtas 2014-09-25 02:12:40

+0

如果你想,當你的應用程序啓動,那麼你可以把這個在您的onCreate()方法:) – 2014-09-25 02:17:26

+0

我插入它下面的保護無效的onCreate(捆綁savedInstanceState){ 爲什麼它在線路錯誤訪問GPS 私人LocationManager locationManager; public boolean isGPSEnabled; locationManager =(LocationManager)mContext.getSystemService(LOCATION_SERVICE); 它說非法修飾符。 – christianbagtas 2014-09-25 02:23:16