我想在android中每秒显示位置更新。问题是,在一部移动电话上使用此代码,位置每30-40秒更新一次,另一次每5-6秒更新一次。如何解决每秒更新的问题?我的代码有什么问题?我有以下代码
private LocationManager mLocationManager = null;
private String provider = LocationManager.GPS_PROVIDER;
private void locateCurrentPosition() {
int status = getActivity().getPackageManager().checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION,
getActivity().getPackageName());
if (status == PackageManager.PERMISSION_GRANTED) {
Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (ApplicationState.choice_gps_loc == 0&&location!=null) {
ApplicationState.latitude = String.valueOf(location.getLatitude());
ApplicationState.longitude = String.valueOf(location.getLongitude());
addBoundaryToCurrentPosition(Double.parseDouble(ApplicationState.latitude)
, Double.parseDouble(ApplicationState.longitude), map);
}
Log.e("Location", "nuuuu");
// mLocationManager.addGpsStatusListener(this);
long minTime = 1000;// ms
float minDist = 1.0f;// meter
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}
private boolean isProviderAvailable() {
mLocationManager = (LocationManager) getActivity().getSystemService(
Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
provider = mLocationManager.getBestProvider(criteria, true);
if (mLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
return true;
}
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
return true;
}
if (provider != null) {
return true;
}
return false;
}
private void updateWithNewLocation(Location location) {
if (location != null && provider != null) {
double lng = location.getLongitude();
double lat = location.getLatitude();
Log.e("alta", lng + " " + lat);
if (ApplicationState.choice_gps_loc == 0)
addBoundaryToCurrentPosition(lat, lng, map);
// CameraPosition camPosition = new CameraPosition.Builder()
// .target(new LatLng(lat, lng)).zoom(10f).build();
if (ApplicationState.choice_gps_loc == 0) {
ApplicationState.latitude = String.valueOf(lat);
ApplicationState.longitude = String.valueOf(lng);
}
if (myMarker != null) myMarker.remove();
myMarker = map.addMarker(new MarkerOptions()
.title("My position/my future destination")
.position(new LatLng(Double.parseDouble(ApplicationState.latitude),
Double.parseDouble(ApplicationState.longitude)))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
//if (map != null)
// map.animateCamera(CameraUpdateFactory
// .newCameraPosition(camPosition));
} else {
Log.d("Location error", "Something went wrong");
}
}
public void addBoundaryToCurrentPosition(double lat, double lang, GoogleMap map1) {
if (updatecerc == 0) {
if (mOptions != null) mOptions.visible(false);
mOptions = new CircleOptions()
.center(new LatLng(lat, lang)).radius(Integer.parseInt(ApplicationState.radius) * 1000)
.strokeColor(0x110000FF).strokeWidth(1).fillColor(0x110000FF);
map1.addCircle(mOptions);
updatecerc++;
}
}
@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
break;
case LocationProvider.AVAILABLE:
break;
}
}