【发布时间】:2019-10-14 14:26:39
【问题描述】:
已经尝试了 1.5 小时。有人可以帮我吗? “您的帖子似乎包含未正确格式化为代码的代码”问题。 请问有人可以帮我格式化这段代码吗? 它现在在这篇文章中有效,但在制作新帖子时无效...... 现在我一天都不能发新帖子了...什么...
已修复:浏览器输出似乎必须采用代码布局。
这是我最初的问题:
TITLE:使用地理定位 API - 范围问题
大家好
我正在使用 Mapbox 并尝试更新 vars lngreal 和 latreal(从默认的 Greenwhich 坐标)。在设置过程中调用函数 initializeMap(),其想法是如果可能,它会使用用户的 longlat 坐标更新它。当我运行下面的代码时...
function initializeMap() {
mapboxgl.accessToken = 'HIDDEN FOR THIS POST';
var lngreal = 0.0098;
var latreal = 51.4934;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayLocationInfo);
}
function displayLocationInfo(position) {
//Why does this not work to update the map zoom?
lngreal = position.coords.longitude;
latreal = position.coords.latitude;
console.log(lngreal);
console.log(latreal);
}
console.log(lngreal);
console.log(latreal);
// This adds the map to your page
var map = new mapboxgl.Map({
// container id specified in the HTML
container: 'map',
// style URL
style: 'mapbox://styles/mapbox/light-v10',
// initial position in [lon, lat] format
center: [lngreal, latreal],
// initial zoom
zoom: 14
});
// More code after this but not related
}
它在 html 检查器控制台中显示以下内容:
play-mapboxscripts.js:21 0.0098
play-mapboxscripts.js:22 51.4934
play-mapboxscripts.js:17 4.9212796
play-mapboxscripts.js:18 52.333704999999995
为什么它首先显示最后的 console.log 行,而不是 displayLocationInfo 函数中的 console.log 行?那个函数不是在navigator.geolocation函数中调用的吗,在它之前呢?
我无法正确更新 lngreal 和 latreal 变量以匹配用户的位置。它们将默认返回 0.0098 和 51.4934,加载的地图(var map)将显示默认的 Greenwhich 位置。这是否与 displayLocationInfo 函数的变量范围有关?
【问题讨论】:
-
我投票结束这个问题作为离题,因为这似乎是 SO 本身的问题,所以应该去 meta.stackoverflow.com
-
我添加了一些示例代码,希望对您有所帮助。
-
将代码包装在```
-
不,不要。改用缩进。标记它并按 Ctrl+K
-
不要提出新问题。编辑这个。包括代码是如何失败的、您遇到了什么错误以及确切的问题是什么。
标签: javascript function scope mapbox