简体   繁体   English

如何在 JS 中获取用户的位置,然后将响应作为坐标传递给 Google 地图?

[英]How can I get the user's location in JS and then pass the response to the Google map as coordinates?

I am trying to get the users location in JS using geolocation successfully and the 2 log statements print out the co-ordinates.我正在尝试使用地理定位成功获取 JS 中的用户位置,并且 2 个日志语句打印出坐标。 How do I pass in the startPos into the Google map function?如何将 startPos 传入 Google 地图功能?

Error:错误:

Uncaught (in promise) TypeError: Cannot read property 'coords' of undefined未捕获(承诺)类型错误:无法读取未定义的属性“坐标”

at initMap (dashboard.js:20)在 initMap (dashboard.js:20)

Thanks!谢谢!

Code:代码:

var startPos;

window.onload = function() {
    var geoSuccess = function(position) {
        startPos = position;
        //document.getElementById('startLat').innerHTML = startPos.coords.latitude;
        console.log(startPos.coords.latitude);
        //document.getElementById('startLon').innerHTML = startPos.coords.longitude;
        console.log(startPos.coords.longitude);
    };
    navigator.geolocation.getCurrentPosition(geoSuccess);
    return startPos;

    // Initialize and add the map

};
function initMap() {
    // The location of user

    var lat = startPos.coords.latitude;
    var long = startPos.coords.longitude;

    const userLocation = { lat: lat, lng: long };
    // The map, centered at Uluru
    const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 4,
        center: userLocation,
    });
    // The marker, positioned at Uluru
    const marker = new google.maps.Marker({
        position: userLocation,
        map: map,
    });
}

initMap(startPos); initMap(startPos);

The reason that initMap is accessing an undefined startPos, is that it is being executed before the onload event has started, and before the geoSuccess callback has been called by the arrival of the location data. initMap 访问未定义的 startPos 的原因是它在 onload 事件开始之前执行,并且位置数据到达调用 geoSuccess 回调之前

So it should look more like:所以它应该看起来更像:

var startPos;

window.onload = function() {
    var geoSuccess = function(position) {
        startPos = position;
        //document.getElementById('startLat').innerHTML = startPos.coords.latitude;
        console.log(startPos.coords.latitude);
        //document.getElementById('startLon').innerHTML = startPos.coords.longitude;
        console.log(startPos.coords.longitude);

        initMap(); // Call it here!
    };
    navigator.geolocation.getCurrentPosition(geoSuccess);
};
function initMap() {
    // The location of user

    var lat = startPos.coords.latitude;
    var long = startPos.coords.longitude;

    const userLocation = { lat: lat, lng: long };
    // The map, centered at Uluru
    const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 4,
        center: userLocation,
    });
    // The marker, positioned at Uluru
    const marker = new google.maps.Marker({
        position: userLocation,
        map: map,
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM