WEB开发网
开发学院手机开发Android 开发 Android GPS获取地理位置并计算距离 阅读

Android GPS获取地理位置并计算距离

 2010-06-12 19:52:00 来源:WEB开发网   
核心提示:criteria.setPowerRequirement(Criteria.POWER_LOW);String provider = locationManager.getBestProvider(criteria, true);Location location = locationManager.getLastKn

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

}

}

上一页  1 2 

Tags:Android GPS 获取

编辑录入:coldstar [复制链接] [打 印]
赞助商链接