此代码的位置返回始终为null。请检查我的代码并告诉我什么是错的。当我在调试模式下运行项目时,provider =“network”但位置始终为null。我使用DDMS发送纬度和经度值,但不会更改。我不明白什么是问题
onCreate方法的这一部分
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = lm.getBestProvider(criteria, true);
location = lm.getLastKnownLocation(provider);
updateLocation(location);
onResume方法的这一部分
@Override
protected void onResume() {
super.onResume();
lm.requestLocationUpdates(provider, 5000, 10, this);
}
我使用LocationListener(MapActivity实现了LocationListener),这是onLocationChanged部分
public void onLocationChanged(Location location) {
updateLocation(location);
}
最后这是我的updateLocation()
private void updateLocation(Location location){
if (location != null) {
Double lat = location.getLatitude()*1E6;
Double lng = location.getLongitude()*1E6;
point = new GeoPoint(lat.intValue(), lng.intValue());
mapView = (MapView) findViewById(R.id.mapView);
mc = mapView.getController();
mapView.setBuiltInZoomControls(true);
//List<Overlay> overlays = mapView.getOverlays();
myLocOverlay = new MyLocationOverlay(this, mapView);
//overlays.add(myLocOverlay);
mapView.getOverlays().add(myLocOverlay);
myLocOverlay.enableMyLocation();
myLocOverlay.runOnFirstFix(new Runnable() {
public void run() {
mc.animateTo(myLocOverlay.getMyLocation());
}
});
mc.setZoom(15);
mapView.invalidate();
}
}
我提供了必要的权限,我使用谷歌地图库
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-library
android:name="com.google.android.maps"
android:required="true" >
</uses-library>
答案 0 :(得分:1)
我认为你应该使用LocationManager.GPS_PROVIDER来请求。你可以测试我的代码:
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1l,1f, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateLocation(location);
}
});
祝你好运!
^ - ^