Android 存储 GPS 纬度和经度
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new DbAdapter(getBaseContext());
db.open();
android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getBaseContext(), "Waiting for location..." , Toast.LENGTH_SHORT).show();
}
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();
Toast.makeText(getBaseContext(), latlong, Toast.LENGTH_SHORT).show();
db.insertGPSCoordinates(android_id, Double.toString(loc.getLatitude()), Double.toString(loc.getLongitude()));
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};
//pauses listener while app is inactive
@Override
public void onPause() {
super.onPause();
locmgr.removeUpdates(onLocationChange);
}
//reactivates listener when app is resumed
@Override
public void onResume() {
super.onResume();
locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
}
这段代码对我有用,但它只获得一次经度和纬度。我想知道“LocationListener”如何工作?我启动应用程序并散步,它只存储了一组。我做错了什么...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new DbAdapter(getBaseContext());
db.open();
android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getBaseContext(), "Waiting for location..." , Toast.LENGTH_SHORT).show();
}
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();
Toast.makeText(getBaseContext(), latlong, Toast.LENGTH_SHORT).show();
db.insertGPSCoordinates(android_id, Double.toString(loc.getLatitude()), Double.toString(loc.getLongitude()));
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};
//pauses listener while app is inactive
@Override
public void onPause() {
super.onPause();
locmgr.removeUpdates(onLocationChange);
}
//reactivates listener when app is resumed
@Override
public void onResume() {
super.onResume();
locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
}
This code is working for me but it gets Lats and Longs only once. I would like to know how "LocationListener" works ? I fired up the app and took a walk, it only stored 1 set. What am I doing wrong...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅当您的位置差异为 10000m、10km 时,您的应用程序才会更新位置变化。尝试使用:
Your application only will update location change when your difference of position be of 10000m, 10km. Try with:
您可以将 GPS 事物放入计时器线程中以强制其更新。不同的设备对 GPS 侦听器的行为有所不同。
还要确保您的 insertDBCooperatives 不仅仅是每次都对同一行进行更新
You can put the GPS thing in a timer thread to force it to update. Different devices behave differently with the GPS listener.
Also make sure your insertDBCoordinates isn't simply doing an update to the same row every time