Android GPS获取地理位置并计算距离
2010-06-12 19:52:00 来源:WEB开发网criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
preTime=System.currentTimeMillis();
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, step, 10,
locationListener);
}
public void stopLocationService(){
if(locationManager!=null){
locationManager.removeUpdates(locationListener);
}
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView) findViewById(R.id.result);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
long subTime=(System.currentTimeMillis()-preTime)/1000;
float v=(subTime==0||preLocation==null)?0:(preLocation.distanceTo(location)/subTime);
latLongString = "纬度:" + lat + " 经度:" + lng + " 速度:"+v+" m/s , "+v*3.6+" km/h";
preLocation=location;
preTime=System.currentTimeMillis();
} else {
latLongString = "无法获取地理信息";
}
myLocationText.setText(System.currentTimeMillis()+": "+latLongString);
}
}
更多精彩
赞助商链接