gpt4 book ai didi

javascript - 从函数js内部获取变量值

转载 作者:行者123 更新时间:2023-12-03 12:28:02 24 4
gpt4 key购买 nike

我正在尝试获取用户当前位置并将这些纬度/经度坐标传递给 map 。该 map 将用作网站背景图像。

我使用这段代码取得了部分成功:

  navigator.geolocation.getCurrentPosition(showPosition);

function showPosition(position) {
xss =position.coords.latitude +
"," + position.coords.longitude;


}
var position = [32.5590985,35.8415147];

现在我希望 xss 的值位于此变量内:

var position = [32.5590985,35.8415147];

基本上我试图从函数中获取值但没有成功。

我应该如何从函数内部获取值?

HTML

<div id="googlemaps"></div>

JS代码

    navigator.geolocation.getCurrentPosition(showPosition);

function showPosition(position) {
xss =position.coords.latitude +
"," + position.coords.longitude;


}
var position = [32.5590985,35.8415147];

function initialize() {

var myOptions = {
zoom: 17,
streetViewControl: false,
scaleControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(document.getElementById('googlemaps'),
myOptions);


latLng = new google.maps.LatLng(position[0], position[1]);

map.setCenter(latLng);

marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: false,
animation: google.maps.Animation.DROP
});


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

最佳答案

如何在 showPosition 函数内部设置 position 变量的值?

var position;

navigator.geolocation.getCurrentPosition(showPosition);

function showPosition(pos) {
position = [pos.coords.latitude, pos.coords.longitude];
}

关于javascript - 从函数js内部获取变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24087802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com