距离计跟踪器 GPS

问题描述 投票:0回答:1

几周以来,我一直在尝试构建一个简单的出租车计价器之类的应用程序。在达到第一个 5 公里后,需要做一些简单的事情,添加 xx$,然后每 1 公里添加 xx$。但是我找不到正确的方法来使会议跟踪器准确工作。

到目前为止,我尝试过使用带有半正弦公式的地理定位、谷歌地图几何、尝试使用 React Native,但到目前为止我无法使距离跟踪器准确工作。我在配速时不断增加米数,大多数时候行驶的距离与真实距离相去甚远。 所以我的问题是:有这方面经验的人可以帮助我吗?往哪个方向走(要集中精力的具体事情),最好的方法是什么,JS,一些框架,Swift 等。我最初的想法是构建一个 PWA,但到目前为止我一无所获。

Google API 不是故意添加的。我的项目中确实有它们

<script>
    let map;
    let currentPositionMarker;
    let previousPosition = null;
    let totalDistance = 0;
    const distanceThreshold = 20; // 20 meters threshold
    const minAccuracy = 30; // Minimum accuracy to consider valid position

    function initMap() {
        // Initialize the map centered on a default location
        map = new google.maps.Map(document.getElementById('map'), {
            center: { lat: 37.7749, lng: -122.4194 }, // San Francisco
            zoom: 13
        });

        // Try HTML5 geolocation
        if (navigator.geolocation) {
            navigator.geolocation.watchPosition(position => {
                const accuracy = position.coords.accuracy;

                if (accuracy > minAccuracy) {
                    // Ignore positions with poor accuracy
                    console.warn(`Ignoring position with poor accuracy: ${accuracy} meters`);
                    return;
                }

                const pos = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                };

                // If there's no marker, create one
                if (!currentPositionMarker) {
                    currentPositionMarker = new google.maps.Marker({
                        position: pos,
                        map: map,
                        title: "Current Position"
                    });
                    map.setCenter(pos);
                } else {
                    currentPositionMarker.setPosition(pos);
                }

                // Calculate the distance if there's a previous position
                if (previousPosition) {
                    const previousLatLng = new google.maps.LatLng(previousPosition.lat, previousPosition.lng);
                    const currentLatLng = new google.maps.LatLng(pos.lat, pos.lng);
                    const distance = google.maps.geometry.spherical.computeDistanceBetween(previousLatLng, currentLatLng);

                    // Only add to total distance if it's above the threshold
                    if (distance > distanceThreshold) {
                        totalDistance += distance;
                    }
                }

                // Update the previous position
                previousPosition = pos;

                // Display the total distance
                document.getElementById('output').innerText = `Total Distance: ${totalDistance.toFixed(2)} meters`;
            }, () => {
                handleLocationError(true, map.getCenter());
            }, {
                enableHighAccuracy: true,
                maximumAge: 10000, // Maximum age of cached position
                timeout: 5000 // Timeout for obtaining position
            });
        } else {
            // Browser doesn't support Geolocation
            handleLocationError(false, map.getCenter());
        }
    }

    function handleLocationError(browserHasGeolocation, pos) {
        alert(browserHasGeolocation ?
              'Error: The Geolocation service failed.' :
              'Error: Your browser doesn\'t support geolocation.');
        map.setCenter(pos);
    }
</script>
javascript react-native gps
1个回答
0
投票

我在一家从事跟踪软件领域的公司工作,我们使用嵌入车辆中的硬件来获取准确的位置坐标。对于地图,我们使用开源库 leaflet。

© www.soinside.com 2019 - 2024. All rights reserved.