为什么我的剧本表现不好

Why is my script not behaving properly?

本文关键字:我的 为什么      更新时间:2023-11-29

我编写了一个函数,可以查找用户位置并相应地更改链接。功能如下:

/*
This function is to determine the users location and integrate that data into the navigation aspect
*/
function findLocation() {
    try { // try to find the users location and then manipulate it to get the correct google maps query strings/links
        var destinations = { // destinations to go to: to be added to the query string
            bridgeFromUs: "Ambassador+Bridge,+Detroit,+MI",
            bridgeFromCa: "Ambassador+Bridge,+Ambassador Bridge,+Windsor,+ON,+Canada",
            tunnelFromUs: "Detroit+Windsor+Tunnel,+Detroit,+MI",
            tunnelFromCa: "The+Windsor+Detroit+Tunnel,+Windsor,+ON,+Canada"
        };
        // the following variables are the correct variable for the query string
        var bridgeAddress;
        var tunnelAddress;
        // switch through the values for the country variable -- it is declared in the head of each HTML page
        switch (country) {
        case "CA": // if it is for Canada, reflect that in the query strings
            bridgeAddress = destinations.bridgeFromCa;
            tunnelAddress = destinations.tunnelFromCa;
            break;
        case "US": // if it is for the US, reflect that in the query strings
            bridgeAddress = destinations.bridgeFromUs;
            tunnelAddress = destinations.tunnelFromUs;
        }
        // now the preliminary work is done -- let's find out where they are
        navigator.geolocation.getCurrentPosition(
            function (position) {
                var latitude = position.coords.latitude;
                var longitude = position.coords.longitude;
                // store the position for the session -- VERY unnecessary, but it is cool and HTML5! ;)
                sessionStorage.setItem('latitude', latitude);
                sessionStorage.setItem('longitude', longitude);
                // link for where they are
                var addr = "https://maps.google.com/maps?f=d&source=s_d&saddr=" + latitude + ",+" + longitude + "&daddr=";
                console.log(addr);
                return addr;
            },
            function createLink(addr) {
                var baseLink = arguments[0];
                // final links
                var link = {
                    bridge: baseLink + bridgeAddress,
                    tunnel: baseLink + tunnelAddress
                };
                // set them in the DOM
                document.getElementById('bridgeLink').href = link.bridge;
                document.getElementById('tunnelLink').href = link.tunnel;
            }
        );
        // if we fail...
    } catch (e) {
        // send an alert...
        alert("Hey! We can't find out where you are! You should check back later.");
        // ...and set some links anyway.
        var lin = "http://maps.google.com";
        document.getElementById('bridgeLink').href = lin;
        document.getElementById('tunnelLink').href = lin;
    }
}

我知道这个函数正在运行,因为它是console.log,而addr变量正好。但它并没有改变链接的地址。链接仍然设置为我设置的默认"#"。我尝试过使用控制台进行调试,但没有显示任何错误。这是怎么回事?

以下是问题的链接:http://54.201.70.251/static/canada.html

createLink应该只在函数(位置)中调用,现在您将createLink作为错误回调传递。

navigator.geolocation.getCurrentPosition(success, error