开发者

How to navigate to the Current location in Google Maps using Android Framework?

开发者 https://www.devze.com 2023-04-06 00:10 出处:网络
Can someone help me out in navigating from some location to my current location ? Actually I was able to see my current location on my application. But if I see another location which is far away fro

Can someone help me out in navigating from some location to my current location ?

Actually I was able to see my current location on my application. But if I see another location which is far away from my current location in the Google map then I want to navigate to the current location by clicking on a Button.

I have a Menu Item in my Options menu called My Location. On click this Item I want to navigate to my location from the other location that I am watching on Google Maps.

    @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());
    }

All the others are working fine. But when I click on My Location(id = mylocation) its giving exception on the Emulator.

It gives the following Error on my 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(1开发者_如何转开发634):     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)


Use LocationManager to obtain coordinates of current location and pass them to mapView.getController().animateTo(GeoPoint). Be aware when translating obtained Location to GeoPoint: the latitude and longitude obtained from Location should be multiplied by 1,e+6 (1 000 000).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号