【问题标题】:Get precise position of user device with GPS使用 GPS 获取用户设备的精确位置
【发布时间】:2017-11-12 13:24:20
【问题描述】:

在我的 Android 应用程序中,我需要使用汽车获取已行驶的道路,然后使用 google android 方向 API 和 GPS 进行检查。

但问题是,有时 GPS 位置不精确,我可能会在一些讲座中得到 10 公里的误差,这意味着在 150 公里的总行程中,我的“行驶距离”可能是 250 公里,这是错误的!

昨天我在高速公路上测试它,问题是当标记位于高速公路外时,计算从我当前位置到最后一个标记在道路上的距离是非常错误的(在这种情况下在高速公路外)。

有什么最好的方法可以通过手机获得汽车的行驶距离? 也许一些更好的代码可以获得更精确的 GPS 位置? 这是我的代码:

private void getUserLocation() {

    Log.d(TAG, "getUserLocation()");
    checkLocationPermission();
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this can be null.
                    if (location != null) {
                        // Logic to handle location object
                        String stringStartLat, stringStartLng;

                        double lat = location.getLatitude();
                        double lng = location.getLongitude();

                        //Start position of device
                        if (travelInfo.getStartLat() == 0 && travelInfo.getStartLng() == 0) {
                            travelInfo.setStartLat(lat);
                            travelInfo.setStartLng(lng);
                            //Set lastcoordinates equals to start
                            travelInfo.setLastLat(lat);
                            travelInfo.setLastLng(lng);
                        }
                        //Current position of device
                        travelInfo.setCurrentLat(lat);
                        travelInfo.setCurrentLng(lng);

                        stringStartLat = Double.toString(travelInfo.getStartLat());
                        stringStartLng = Double.toString(travelInfo.getStartLng());
                        //Set the TextView in the fragment with start coordinates
                        Log.d(TAG,"LatitudeStart: " + stringStartLat);
                        Log.d(TAG,"LongitudeStart: " + stringStartLng);

                        if (startMarker == null) {
                            //Add the user location to the map with a marker
                            LatLng userLoction = new LatLng(travelInfo.getStartLat(), travelInfo.getStartLng());
                            startMarker = googleMap.addMarker(new MarkerOptions()
                                    .position(userLoction)
                                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
                                    .title("Start Position")
                                    .snippet("Show the start position of user"));

                            // For zooming automatically to the location of the marker
                            CameraPosition cameraPosition = new CameraPosition.Builder().target(userLoction).zoom(9).build();
                            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                        } else {

                            LatLng currentLocation = new LatLng(travelInfo.getCurrentLat(), travelInfo.getCurrentLng());
                            currentPositionMarker = googleMap.addMarker(new MarkerOptions()
                                    .position(currentLocation)
                                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
                                    .title("Current Position")
                                    .snippet("Show the current position of user during travel"));

                            // For zooming automatically to the location of the marker
                            CameraPosition cameraPosition = new CameraPosition.Builder().target(currentLocation).zoom(11).build();
                            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                        }

                        if (travelInfo.getEndLat() != 0 && travelInfo.getEndLng() != 0) {
                            Log.i(TAG, "Dentro if userlocation");
                            //Get total distance of travel
                            getTotalDistanceTime(travelInfo);
                            //Get the percurred distance from last known coordinates
                            getPercurredDistance(travelInfo);
                        }
                    }
                }
            });
}

并且 getpercurredDistance 使用这个 url 的 google 方向 API

    String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + obj.getLastLat() + "," + obj.getLastLng() + "&destination=" + obj.getCurrentLat() + "," + obj.getCurrentLng() + "&mode=driving&key=AI*******************I";

用于获取从最后一个标记和当前位置标记的距离。 但有时它会将本地位置与实际位置相距太远......如何避免这种情况?

编辑

我使用的方法对我的工作有好处吗? mFusedLocationClient.getLastLocation()

【问题讨论】:

    标签: android google-maps gps


    【解决方案1】:

    无法从安卓设备的 GPS 传感器获得更精确的 GPS 位置,但您可以通过以下方式过滤“错误”点:

    1) 对每个行程点获取多个LatLng的值,舍弃最小值和最大值,对剩余的取平均值;

    2) 记录每个旅行点的时间戳并将其与两个相邻点进行比较:速度(两点之间的距离除以该点的时间间隔)大于最大值。汽车速度(例如 250 公里/小时),这意味着第二点是错误的(如果第一点当然是好的)。

    3) 使用Google Maps Roads API 为给定的一组 GPS 坐标找到最适合的道路几何形状。

    另请查看this 问题。

    【讨论】:

    • 如何在同一个通话中获得更多的地理点?只需多次调用方法 mFusedLocationClient.getLastLocation()?喜欢 for( . . .) { mFusedLocationClient.getLastLocation()} ?
    • 谷歌地图道路没有其他通道就足够了......我看到用地图街道调整了percurred道路......这对我的问题非常有用!
    • @Dario Points 1 和 2 用于通过 LocationListener 记录位置更新。
    • @Dario 也看看this的问题。
    • 所以我的方法 getUserLocation() 做同样的事情 mFusedLocationClient.requestLocationUpdates :) ?
    【解决方案2】:

    为了实时定位,位置更新允许您以不同的时间间隔请求位置更新。请参阅android developer location documentation。你想要的函数是FusedLocationProviderClient.requestLocationUpdates

    GPS(或任何其他广泛使用的定位技术)没有理由让您在一次位置估计中出现 10 公里的误差。如果您收到此信息,则表明您进行位置估计的时间与您请求位置估计的时间之间存在很大的不匹配。

    【讨论】:

    • 出现错误是因为如果标记设置在 highstreet 之外(在意大利是 autostrada),那么从新点和旧点的道路距离很长,因为该点在 highstreet 之外!结果是好的,但错误的是我在大街上,并且点设置在这条街之外:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多