gpt4 book ai didi

javascript - 谷歌地图 PanTo OnClick

转载 作者:可可西里 更新时间:2023-11-01 01:57:00 25 4
gpt4 key购买 nike

我试图通过点击平移 map 上的某个区域。该脚本不工作和页面重新加载。也许有人能看出问题所在。

我的函数

function clickroute(lati,long) {

map = google.maps.Map(document.getElementById("map_canvas"));
map.panTo(lati,long)
}

其他的

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
geocoder = new google.maps.Geocoder();
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var myOptions = {
zoom:10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: chicago
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

var address = 'virginia water';
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
});
directionsDisplay.setMap(map);
}

function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}

还有我的事件

<a href="" onclick="clickroute(51.433373, -0.712251)">test</a>

最佳答案

问题是您使用的是 map.panTo(latitude,longitude) 但 google maps API 使用的是:panTo(latLng myLatLng) 其中 latLng 是一个谷歌地图类。

尝试这样的事情(未经测试)

function clickroute(lati,long) {
var latLng = new google.maps.LatLng(lati, long); //Makes a latlng
map.panTo(latLng); //Make map global
}

here了解更多信息。

编辑正如其他人所说,您不想重新制作新 map 。也许让它全局化更容易?

关于javascript - 谷歌地图 PanTo OnClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6191714/

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