对您的位置进行地理编码发现 android eclipse

发布于 2024-10-31 12:04:18 字数 540 浏览 0 评论 0原文

我的代码当前在地图上显示用户位置...使用下面的代码

private void initMyLocation() {
    final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
    overlay.enableMyLocation();

    overlay.runOnFirstFix(new Runnable() {
        public void run() {
            controller.setZoom(8);
            controller.animateTo(overlay.getMyLocation());
        }
    });
    map.getOverlays().add(overlay);
}

我想使用用户的当前位置并对其执行地理编码以将地址显示为吐司消息..我是否必须删除上面的代码并通过单独的经度和纬度值检索位置?最好我不想去更改上面的代码,因为它已经可以工作了。我似乎找不到任何好的教程。有人可以指导我一个吗? 谢谢

My code currently displays the users location on a map... using the code below

private void initMyLocation() {
    final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
    overlay.enableMyLocation();

    overlay.runOnFirstFix(new Runnable() {
        public void run() {
            controller.setZoom(8);
            controller.animateTo(overlay.getMyLocation());
        }
    });
    map.getOverlays().add(overlay);
}

I want to use the current location of the user and perform geocding on it to display the address as a toast message.. Would i have to delete the code above and retrieve the location by separate longitude and latitude values? Preferably i wouldnt want to go changing the code above as it already works.. I can't seem to find any good tutorials out there.. Could someone please direct me to one?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

养猫人 2024-11-07 12:04:18
  1. 由于 MyLocationOverlay 已经实现了 LocationListener,因此您可以简单地对其进行子类化并重写 onLocationChanged 侦听器方法,并在那里执行地理编码(最好在新线程/异步任务以避免阻塞覆盖处理!)。

    类似于:

    private void initMyLocation() {
        最终 MyLocationOverlay 覆盖 = new MyLocationOverlay(this, 地图){
            public void onLocationChanged(android.location.Location 位置)
            {
                //子类AsyncTask来获取位置参数 
                //doInBackground 进行地理编码,然后传递一个字符串
                //到onPostExecute,您可以在其中显示toast
                新的 MyAsyncTask().execute(location);
    
                //记住调用父实现以避免破坏
                //MyLocationOverlay默认功能
                super.onLocationChanged(位置);
            }
        };
        覆盖.enableMyLocation();
    
        覆盖.runOnFirstFix(新的Runnable(){
            公共无效运行(){
                控制器.setZoom(8);
                controller.animateTo(overlay.getMyLocation());
            }
        });
        map.getOverlays().add(overlay);
    }
    
  2. 另一个选项可能是编写一个自定义 LocationListener 并注册位置更新,并执行与上面代码中几乎相同的操作。 IMO,这可能是多余的并且不必要地昂贵,因为如上所述,您已经有一个 LocationListener 实现可以挂钩,那么为什么还要编写另一个实现呢?

  1. Since MyLocationOverlay already implements LocationListener, you could simply subclass it and override the onLocationChanged listener method and perform your geocoding there (preferably in a new thread/async task to avoid blocking the overlay processing!).

    Something like:

    private void initMyLocation() {
        final MyLocationOverlay overlay = new MyLocationOverlay(this, map){
            public void onLocationChanged(android.location.Location location)
            {
                //subclass AsyncTask to take a location parameter for 
                //doInBackground where it geocodes and then passes a String
                //to onPostExecute where you can display a toast
                new MyAsyncTask().execute(location);
    
                //remember to call the parent implementation to avoid breaking
                //MyLocationOverlay default functionality
                super.onLocationChanged(location);
            }
        };
        overlay.enableMyLocation();
    
        overlay.runOnFirstFix(new Runnable() {
            public void run() {
                controller.setZoom(8);
                controller.animateTo(overlay.getMyLocation());
            }
        });
        map.getOverlays().add(overlay);
    }
    
  2. The other option might be to write a custom LocationListener and register for updates for location and do pretty much the same thing as in the code above. IMO, this is probably redundant and unnecessarily expensive since, as explained above, you already have a LocationListener implementation to hook into so why write another?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文