Android:使用 LocationManager 和提供程序

发布于 2024-10-02 01:33:42 字数 1617 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文