【问题标题】:Problems displaying a location and centering the camera on it显示位置并将相机居中定位时出现问题
【发布时间】:2017-03-11 23:21:08
【问题描述】:

由于奇怪的原因,CameraUpdateFactory 出现了一些问题。

所以我将此代码合并到导航片段上 GpsButton 上的 onclickListener 中:

 if (ContextCompat.checkSelfPermission(mapFragment.getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                    PackageManager.PERMISSION_GRANTED) {
                Log.d("Permission checked", "checkSelfPermission passed with no errors");
                map.setMyLocationEnabled(true);
                Log.d("Permission checked", "Location Layer implementation successful");
            } else {
                //Request the Permission
                ActivityCompat.requestPermissions(mapFragment.getActivity(), new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }

这基本上使该位置仅在按下 GPS 按钮时才在地图上显示为蓝点。到目前为止,它的功能非常完善。

我还加入了一种将相机移动到我当前位置的方法:

 public void locateMe() {

    LocationManager locationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); // Getting LocationManager object from System Service LOCATION_SERVICE
    Criteria criteria = new Criteria();// Creating a criteria object to retrieve provider
    String provider = locationManager.getBestProvider(criteria, true);// Getting the name of the best provider
    if (ContextCompat.checkSelfPermission(mapFragment.getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(mapFragment.getActivity(), new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 2);
    }
    Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {

        double latitude = location.getLatitude(); //Getting latitude of the current location
        double longitude = location.getLongitude(); // Getting longitude of the current location
        myPosition = new LatLng(latitude, longitude); // Creating a LatLng object for the current location
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(myPosition, CAMPUS_DEFAULT_ZOOM_LEVEL));//Camera Update method
    }
}

出于某种原因,这是命中注定的。 3 天前,它锁定了我当前所在的位置,而在过去的 2 天里,它运行良好。没有任何代码被更改。有人可以解释发生了什么吗?任何修复或建议将不胜感激。

【问题讨论】:

    标签: java android google-maps geolocation locationmanager


    【解决方案1】:

    这是因为您正在使用 getLastKnownLocation。

    这可以在不启动提供程序的情况下完成。请注意,此位置可能已过时,例如,如果设备已关闭并移动到另一个位置。 如果提供程序当前被禁用,则返回 null。

    documentation

    如果您想检索用户的当前文档,您必须使用 requestLocation 更新。 requestLocationUpdates documentation

    【讨论】:

    • 我明白了,但其中一个问题似乎是,它正确地获取了我的位置,然后它显示我的位置已经过去了一个小时。这正常吗?
    • 位置有时正确,有时不正确是正常的。如果另一个应用程序最近没有请求位置,则您的位置可能已经过时,而其他时候它会是正确的。但是,如果您的位置是正确的,通常几分钟后它也应该是正确的。您应该记录使用了哪个 Locationprovider,因为您使用 getBestLocationProvider,它可能会在您每次调用此方法时返回不同的 locationprovider,并且可能会更高或更低的准确性,因此是不同的位置。
    • 有没有办法以编程方式在 googlemap.setmyLocationEnabled 上锁定相机?
    • 如果您使用 setMyLocationEnabled,我认为地图已经以用户当前位置为中心。您可能可以在此站点上找到类似的问题,否则您应该创建一个新问题。如果我的回答回答了您最初的问题,请将其标记为已接受。
    • 想通了。 setMyLocationEnabled 只显示当前位置并且没有居中。有一种方法可以调用 UI 按钮,但无法以编程方式访问该按钮。感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多