Android版谷歌地图setMyLocationEnabled(真) [英] Android Google Maps setMyLocationEnabled(true)

查看:812
本文介绍了Android版谷歌地图setMyLocationEnabled(真)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个教程工作,我的应用程序基本上工作,除了一个小细节。我有一个选项菜单证明像显示在地图作为卫星视图,缩放到特定的地方等等的东西。

本教程有两个菜单选项如下所示,从而有效地需要以缩放地图,在那里我目前所在显示的顺序使用。我想通过添加相同的功能进入第二个菜单项进行的第一项冗余(getcurrentlocation)(显示当前loaction),但是这会导致应用程序崩溃。

有没有人有任何想法,为什么,以及如何克服它。
我的感谢。

code片断如下

 情况下R.id.menu_getcurrentlocation:
        // ---让你的当前位置,并显示一个蓝色圆点---
        map.setMyLocationEnabled(真);        打破;    案例R.id.menu_showcurrentlocation:
            //添加下一行打破了应用程序。
            // map.setMyLocationEnabled(真);
        位置myLocation = map.getMyLocation();
        经纬度myLatLng =新的经纬度(myLocation.getLatitude(),myLocation.getLongitude());
        。CameraPosition myPosition =新CameraPosition.Builder()目标(myLatLng).zoom(17).bearing(90).tilt(30).build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));        打破;


解决方案

最初,当第一次你的的GoogleMap 负荷需要一段时间,有前位置将是空的。

因此​​,对于你这两个菜单选项检查这样的。

 如果(图!= NULL){
 map.setMyLocationEnabled(真);
}
如果(图!= NULL){
    位置myLocation = map.getMyLocation();    如果(myLocation!= NULL){
    经纬度myLatLng =新的经纬度(myLocation.getLatitude()
            myLocation.getLongitude());
    CameraPosition myPosition =新CameraPosition.Builder()
            .bearing .TARGET(myLatLng).zoom(17)(90).tilt(30).build();
    map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));
   }
}

I'm working through a tutorial and my app is basically working except for one small detail. I have an Options menu to demonstrate things like showing the map as a Satellite view, zooming to specific places etc.

The tutorial has two menu options as shown below which effectively need to be used in the order shown in order to zoom the map to where I'm currently located. I wanted to make the first item redundant (getcurrentlocation) by adding in the same functionality into the second menu item (show current loaction) but this causes the app to crash.

Does anyone have any ideas why and how to overcome it. My thanks.

Code snippet follows

    case R.id.menu_getcurrentlocation:
        // ---get your current location and display a blue dot---
        map.setMyLocationEnabled(true);

        break;

    case R.id.menu_showcurrentlocation:
            // Adding the next line breaks the app.
            // map.setMyLocationEnabled(true);
        Location myLocation = map.getMyLocation();
        LatLng myLatLng = new LatLng(myLocation.getLatitude(),myLocation.getLongitude());


        CameraPosition myPosition = new CameraPosition.Builder().target(myLatLng).zoom(17).bearing(90).tilt(30).build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));

        break;

解决方案

Initially when first time your GoogleMap loads takes some time and there fore Location will be null.

So for your both menu options check this way.

if(map!=null){
 map.setMyLocationEnabled(true);
}


if(map!=null){
    Location myLocation = map.getMyLocation();

    if(myLocation !=null){
    LatLng myLatLng = new LatLng(myLocation.getLatitude(),
            myLocation.getLongitude());


    CameraPosition myPosition = new CameraPosition.Builder()
            .target(myLatLng).zoom(17).bearing(90).tilt(30).build();
    map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));
   }
}

这篇关于Android版谷歌地图setMyLocationEnabled(真)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆