gpt4 book ai didi

javascript - Google Maps API v3,无法调用未定义的方法 "panTo"

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

我只想移动 map 上的标记,并将变量 mapmarker 声明为全局变量,但我无法在函数moveMarker()。我是 JavaScript 新手,所以这个问题可能很简单,但我仍然无法解决它 - 有人可以帮我解决它吗?

var map;
var marker;

function initialize() {
var myLatLng = new google.maps.LatLng(50, 50),
myOptions = {
zoom: 4,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
marker = new google.maps.Marker({
position: myLatLng,
map: map,
draggable: true
});
marker.setMap(map);
setTimeout("moveMarker(1)", 2000);
}

function moveMarker(i) {
//delayed so you can see it move
if (i > 10) {
return;
}
var myLatLng = new google.maps.LatLng(50 + 0.1 * i, 50 + 0.1 * i);
marker.setPosition(myLatLng);
map.panTo(myLatLng);
var latlng = marker.getPosition();
newlatlng = latlng.toString();
marker.setTitle(newlatlng);
setTimeout("moveMarker(" + (i + 1) + ")", 1500);
}

window.onload = function () {
// Setup the dnd listeners.
initialize();
};

最佳答案

您需要将“,”替换为“;”,否则它认为您正在重新定义变量“map”:

            var map;
var marker;

function initialize() {
var myLatLng = new google.maps.LatLng(50, 50);
myOptions = {
zoom: 4,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
marker = new google.maps.Marker({
position: myLatLng,
map: map,
draggable: true
});
marker.setMap(map);
setTimeout("moveMarker(1)", 2000);
}

function moveMarker(i) {
//delayed so you can see it move
if (i > 10) {
return;
}
var myLatLng = new google.maps.LatLng(50 + 0.1 * i, 50 + 0.1 * i);
marker.setPosition(myLatLng);
map.panTo(myLatLng);
var latlng = marker.getPosition();
newlatlng = latlng.toString();
marker.setTitle(newlatlng);
setTimeout("moveMarker(" + (i + 1) + ")", 1500);
}

window.onload = function () {
// Setup the dnd listeners.
initialize();
};

关于javascript - Google Maps API v3,无法调用未定义的方法 "panTo",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18635966/

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