如何将当前GPS导航器位置设置为destinationA的变量,以查找距离和时间

How do you set the current GPS navigator location as a variable for destinationA to find distance and time

本文关键字:变量 destinationA 查找 时间 距离 设置 GPS 位置 导航      更新时间:2023-09-26

我在这里使用两个脚本。脚本所做的是找到您的当前位置,用标记更新地图,并将当前地址显示为文本。

脚本的第二部分获取您键入的地址,并计算与第二个变量DestinationA的距离和时间。

目前目的地A设置为"美国纽约",但我希望能够将DestinationA变量设置为从GPS导航器中找到的latLng位置。我该如何实现这一目标?

这是我的脚本:

function $(id) {
    return document.getElementById(id);
}
var trackerId = 0;
var geocoder;
var theUser = {};
var map = {};
function initialize() {
    geocoder = new google.maps.Geocoder();
    if (navigator.geolocation) {
        var gps = navigator.geolocation;
        gps.getCurrentPosition(function (pos) {
            var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
            var opts = {
                zoom: 12,
                center: latLng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map($("map_canvas"), opts);
            theUser = new google.maps.Marker({
                position: latLng,
                map: map,
                title: "You!"
            });
            showLocation(pos);
        });
        var trackerId = gps.watchPosition(function (pos) {
            var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
            map.setCenter(latLng);
            theUser.setPosition(latLng);
            showLocation(pos);
        });
    }
}
function showLocation(pos) {
    var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
    if (geocoder) {
        geocoder.geocode({
            'latLng': latLng
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    $("location").innerHTML = results[0].formatted_address;
                    var addressinput = results[0].formatted_address;
                    document.getElementById('addressinput').value = addressinput;
                }
            }
        });
    }
}
//Calc Distance/Time - to fix..
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
var destinationA = "New York, NY, USA"; //How do I make this my current position?
var destinationIcon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000";
var originIcon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000";
function calculateDistances() {
    if (navigator.geolocation) {
        var gps = navigator.geolocation;
        gps.getCurrentPosition(function (pos) {
            var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
            var opts = {
                zoom: 12,
                center: latLng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map($("map_canvas"), opts);
            theUser = new google.maps.Marker({
                position: latLng,
                map: map,
                title: "You!"
            });
            showLocation(pos);
        });
        var trackerId = gps.watchPosition(function (pos) {
            var latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
            map.setCenter(latLng);
            theUser.setPosition(latLng);
            showLocation(pos);
        });
    }
    var origin1 = document.getElementById('originDestination').value;
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix({
        origins: [origin1],
        destinations: [destinationA],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.METRIC,
        avoidHighways: false,
        avoidTolls: false
    }, callback);
}
function callback(response, status) {
    if (status != google.maps.DistanceMatrixStatus.OK) {
        alert('Error was: ' + status);
    } else {
        var origins = response.originAddresses;
        var destinations = response.destinationAddresses;
        deleteOverlays();
        for (var i = 0; i < origins.length; i++) {
            var results = response.rows[i].elements;
            addMarker(origins[i], false);
            for (var j = 0; j < results.length; j++) {
                addMarker(destinations[j], true);
                document.getElementById('duration').value = results[j].duration.text;
                document.getElementById('distance').value = results[j].distance.text;
            }
        }
    }
}
function addMarker(location, isDestination) {
    var icon;
    if (isDestination) {
        icon = destinationIcon;
    } else {
        icon = originIcon;
    }
    geocoder.geocode({
        'address': location
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            bounds.extend(results[0].geometry.location);
            map.fitBounds(bounds);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                icon: icon
            });
            markersArray.push(marker);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
function deleteOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
        markersArray.length = 0;
    }
}

感谢您的帮助

在gps.getCurrentPosition()和gps.watchPosition()的回调中(取决于您想要做什么),设置destinationA:destinationA=新的google.maps.LatLng(位置坐标纬度,位置坐标经度);

但我真的不明白你想做什么……如果目的地现在是用户位置,那么起点是什么?