Android:使用 LocationManager 和提供程序
我是 Android 新手,正在我的 Android 模拟器上尝试一些东西。在使用位置管理器和位置侦听器时,我可以使用模拟器测试更改位置的代码。位置监听器捕获位置变化并移动到特定位置。
我现在想使用我的程序复制相同的测试,即每 x 秒将新坐标(可能通过线程?)传递给位置侦听器,但通过我的程序而不是模拟器。
有人可以建议一种实现此功能的方法吗?
我在网上找到并正在尝试的位置侦听器的代码:
private LocationListener locationListener;
private MapView mapView;
private MapController mc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0,0,locationListener);
mapView = (MapView) findViewById(R.id.mpview);
mc = mapView.getController();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location locn) {
if (locn != null) {
GeoPoint point = new GeoPoint(
(int) (locn.getLatitude() * 1E6),
(int) (locn.getLongitude() * 1E6));
mc.animateTo(point);
mapView.invalidate();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
}
谢谢!
I am new to android and was trying out stuff on my android emulator. While playing with the location manager and location listener, I could test the code of changing location using the emulator. The locationlistener catches the location change and moves to the specific location.
I now want to replicate the same test using my program, i.e. pass new coordinates every x seconds (may be through a thread ?) to the locationlistener, but through my program and not the emulator.
Can someone please suggest a way to go about implementing this functionality ?
The code for location listener that I found somewhere on the net and was trying:
private LocationListener locationListener;
private MapView mapView;
private MapController mc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0,0,locationListener);
mapView = (MapView) findViewById(R.id.mpview);
mc = mapView.getController();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location locn) {
if (locn != null) {
GeoPoint point = new GeoPoint(
(int) (locn.getLatitude() * 1E6),
(int) (locn.getLongitude() * 1E6));
mc.animateTo(point);
mapView.invalidate();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论