【问题标题】:Change google maps location onclick点击更改谷歌地图位置
【发布时间】:2015-03-23 01:15:27
【问题描述】:

我在网上找到了一个很棒的代码,我正在尝试使用按钮将标记移动到另一个位置。我想我需要用点击事件触发变量“targetLocation”,但我不知道怎么做。还是应该创建像 targetlocation2 和 marker2 这样的变量?

位置 1:41.118555",28.2743889

位置 2: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
});

}

【问题讨论】:

    标签: jquery google-maps-api-3 google-maps-markers


    【解决方案1】:

    在您的事件处理程序中使用setPosition

    $(selector).on('click', function(){
         newLocation = new google.maps.LatLng(0,0);
         marker.setPosition( newLocation );
    });
    

    【讨论】:

    • 非常感谢!现在效果很好:) 有没有办法移动地图?
    • map.setCenter(newLocation)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 2015-02-16
    • 2020-02-02
    相关资源
    最近更新 更多