【问题标题】:Google Maps API - unexpected redirectionGoogle Maps API - 意外重定向
【发布时间】:2014-04-28 21:44:52
【问题描述】:

在执行这段代码后,我遇到了到 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);

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

【问题讨论】:

  • 您的默认位置为空。也许您应该设置一个,以防用户不允许跟踪他们的地理位置。这是一个开始。
  • location 是 javascript 中的保留字...设置它可能会导致问题。
  • 谢谢@geocodezip - 这导致了这个问题。
  • 它本身不是保留字,但它已经在window 对象中声明。并且您的 Javascript 的范围在 window 内,因此当您设置 location = null 时,您只需覆盖 window.location,因为您的 Javascript 解释器(浏览器)将该行读取为 window.location = null
  • 您可以通过将 location 声明为 var 来解决问题,以避免与 window.location 冲突

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


【解决方案1】:

Location 是窗口对象的 javascript 中的保留字 属性,因此会将浏览器重定向到 /position.coords;

感谢@peter 的澄清

【讨论】:

  • 不是保留字而是窗口对象的属性
  • 感谢您接受。我已经更正了我的答案。
猜你喜欢
  • 2018-06-21
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多