如何使用Android Framework导航到Google地图中的当前位置?

时间:2011-09-19 08:26:32

标签: android google-maps navigation

有人可以帮助我从某个地方导航到我当前的位置吗?

实际上我能够在我的应用程序上看到我当前的位置。但是,如果我在Google地图中看到远离我当前位置的其他位置,那么我想通过点击按钮导航到当前位置。

我的“选项”菜单中有一个名为“我的位置”的菜单项。点击此项目后,我想从我在Google地图上观看的其他位置导航到我的位置。

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.mylocation:
                showCurrentLocation();
                return true;
            case R.id.menuItem1:
                showSatelliteView();
                return true;
            case R.id.menuItem2:
                showTrafficView();
                return true;
            case R.id.menuItem3:
                exitApplication();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void showCurrentLocation() {
       mapView.getController().animateTo(myLocationOverlay.getMyLocation());
    }

所有其他人都工作正常。但是,当我点击我的位置(id = mylocation)时,它会在模拟器上给出异常。

它在我的LogCat上出现以下错误。

09-19 15:04:58.445: ERROR/AndroidRuntime(1634): Uncaught handler: thread main exiting due to uncaught exception
09-19 15:04:58.455: ERROR/AndroidRuntime(1634): java.lang.NullPointerException
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.google.android.maps.MapController.setCenter(MapController.java:345)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.location.mylocation.MyLocation.showCurrentLocation(MyLocation.java:126)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.location.mylocation.MyLocation.onOptionsItemSelected(MyLocation.java:108)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.app.Activity.onMenuItemSelected(Activity.java:2170)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:525)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.view.View.onTouchEvent(View.java:4179)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.widget.TextView.onTouchEvent(TextView.java:6540)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.view.View.dispatchTouchEvent(View.java:3709)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.os.Looper.loop(Looper.java:123)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at android.app.ActivityThread.main(ActivityThread.java:4363)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at java.lang.reflect.Method.invoke(Method.java:521)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-19 15:04:58.455: ERROR/AndroidRuntime(1634):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

使用LocationManager获取当前位置的坐标并将其传递给mapView.getController().animateTo(GeoPoint)。将获得的Location翻译为GeoPoint时请注意:从Location获得的纬度和经度应乘以1,e + 6(1 000 000)。