javascript - Google map API - 意外重定向

标签 javascript asp.net-mvc google-maps geolocation

执行这段代码后,我遇到了意外重定向到 localhost:xxxx/Home/null 的情况。原因是什么?

这是我的 HTML:

<div id="userConsentSection">
Can we use your geolocation? <br />
    <input type="button" id="yes" value="Yes" />
    <input type="button" id="no" value="No" /><br /><br />
</div>

<div id="setCustomLocationSection">
    Enter your address manually.<br />
    <input type="text" id="customLocation" />
    <input type="button" id="setCustomLocationButton" value="Show" /><br /><br />
</div>

<div id="map" style="height: 450px; width: 720px" />

JS:

var map1 = null;
var location = null;

alert("alert4");

function initialize() {
    alert("alert5");
    $("#setCustomLocationSection").hide();

    var options =
        {
            center: new google.maps.LatLng(0, 0),
            zoom: 2
        }

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(0, 0),
        map: map,
        title: "Marker 0,0"
    });

    map1 = new google.maps.Map(document.getElementById("map"), options)

    marker.setMap(map);
}

alert("alert3");

$("#yes").click(function () {
    $("#userConsentSection").hide();
    getPosition();
    reloadMap();
});

$("#no").click(function () {
    $("#userConsentSection").hide();
    showSetCustomLocationSection();
});

function showSetCustomLocationSection() {
    $("#setCustomLocationSection").show();
}

function getPosition() {

    var options = {
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 2000
    }

    navigator.geolocation.getCurrentPosition(showPosition, errorPosition, options);
}

function showPosition(position) {
    if (position) {
        location = position.coords;
    }
}

function errorPosition(position) {
    switch (position.code)
    {
        case 1:
            showSetCustomLocationSection();
            break;
        case 2:
            showSetCustomLocationSection();
            break;
        case 3:
            showSetCustomLocationSection();
            break;
        default:
            break;
    }
}

function reloadMap()
{
    map1 = null;

    var options =
        {
            center: new google.maps.LatLng(location.latitude, location.longitude),
            zoom: 8
        }

    map1 = new google.maps.Map(document.getElementById("map"), options)
}

alert("alert2");

google.maps.event.addDomListener(window, 'load', initialize);

我插入了警报来显示哪些代码段已执行,哪些代码段未执行。看来最后一个事件方法有问题,但我不知道是什么,没有注意到任何拼写错误。有什么想法吗?

最佳答案

位置是 window 对象的 javascript 属性中的保留字,因此将浏览器重定向到/position.coords;

感谢@peter 的澄清

关于javascript - Google map API - 意外重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23351715/

相关文章:

javascript - 如何异步运行函数?

ios - 谷歌地图GMS标记警告

javascript - 将表单的所有输入重置为默认值而不重新加载

javascript - 返回一个新数组,其中包含除第一个值之外的所有值,每个值加 7

javascript - 使用 XML 解析器从用户获取最近的 YouTube 视频

c# - 使用 EF5 代码优先迁移执行 IDENTITY_INSERT

c# - 如何在 Web Api 的主体中传递派生的 JObject 类

jquery - 如何在 jQuery 中获取 TempData 值

javascript - 将所见即所得编辑器集成到最佳文本区域

android - 不可能完成的任务?谷歌地图在 iphone 或 android 中的蒙版效果