Faz*_*gal 10 gps android location geolocation
我正在尝试为我的新导航应用程序获取用户位置.我想经常检查用户位置,它必须准确.我使用示例中的以下代码来获取位置.
public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;
private String provider;
private TextView a;
private TextView b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a = (TextView) findViewById(R.id.a);
b = (TextView) findViewById(R.id.b);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
a.setText("Location not available");
b.setText("Location not available");
}
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider,0, 1, this);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
a.setText(String.valueOf(lat));
b.setText(String.valueOf(lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我移动时,位置坐标没有更新,我总是得到一个恒定值......有没有更好的方法来获取位置?根据Android docs,我可以使用Google Play位置服务或Android平台位置API.我检查了两者的示例代码,它实现了LocationListener接口以获取用户位置.那么它们之间的实际差异是什么?
nic*_*fox 23
绝对使用新的谷歌播放位置服务.他们在室内工作比老位置经理好得多.我创建了一个使用新的Google Play定位服务的示例,并定期在后台运行.看看这个:
https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android
归档时间: |
|
查看次数: |
20761 次 |
最近记录: |