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
}
}
可我知道我應該在哪裏插入這個代碼請感謝。 – christianbagtas 2014-09-25 02:12:40
如果你想,當你的應用程序啓動,那麼你可以把這個在您的onCreate()方法:) – 2014-09-25 02:17:26
我插入它下面的保護無效的onCreate(捆綁savedInstanceState){ 爲什麼它在線路錯誤訪問GPS 私人LocationManager locationManager; public boolean isGPSEnabled; locationManager =(LocationManager)mContext.getSystemService(LOCATION_SERVICE); 它說非法修飾符。 – christianbagtas 2014-09-25 02:23:16