我在网上发现了一个很棒的代码,我试图用一个按钮将标记移动到另一个位置。我想我需要触发变量" targetLocation"点击事件,但我无法找到方法。或者我应该创建像targetlocation2和marker2这样的变量吗?
location1:41.118555",28.2743889
location2:38.9152733,-111.6676686
HTML:
<a href="#location1">Address 1</a>
<a href="#location2">Address 2</a>
代码:
if ($("#map .google-maps").length) {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var targetLocation = new google.maps.LatLng("41.118555", "28.2743889");
var myOptions = {
center: targetLocation,
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 13,
scrollwheel: false,
streetViewControl: false,
mapTypeControl: false,
disableDoubleClickZoom: true,
styles: [
{
featureType: "all",
stylers: [
{saturation: -100}
]
}
]
}
var map = new google.maps.Map($("#map .google-maps")[0], myOptions);
directionsDisplay.setMap(map);
var marker = new google.maps.Marker({
position: targetLocation,
map: map,
icon: "marker.png",
visible: true
});
}
答案 0 :(得分:3)
在事件处理程序中使用setPosition
。
$(selector).on('click', function(){
newLocation = new google.maps.LatLng(0,0);
marker.setPosition( newLocation );
});