如何更改Google Map API上的标记位置

时间:2016-07-11 11:41:52

标签: javascript google-maps google-maps-api-3

我在我的网站上使用谷歌地图,这里是谷歌地图代码:

<script src='https://maps.googleapis.com/maps/api/js?v=3.exp'></script>
<div style='overflow:hidden;height:440px;width:700px;'>
    <div id='gmap_canvas' style='height:440px;width:700px;'></div>
    <style>
        #gmap_canvas img {
            max-width: none!important;
            background: none!important
        }
    </style>
</div>
<script type='text/javascript'>
    function init_map() {
        var myOptions = {
            zoom: 10,
            center: new google.maps.LatLng(51.5073509, -0.12775829999998223),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('gmap_canvas'), myOptions);
        marker = new google.maps.Marker({
            map: map,
            position: new google.maps.LatLng(51.5073509, -0.12775829999998223)
        });
        infowindow = new google.maps.InfoWindow({
            content: '<strong>Title</strong><br>London, United Kingdom<br>'
        });
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map, marker);
        });
        infowindow.open(map, marker);
    }
    google.maps.event.addDomListener(window, 'load', init_map);
</script>

获取此地图输出:

enter image description here

目前,我有一个以红色突出显示的div,标记显示在红色区域旁边,所以我想移到左侧。

现在我想在左侧移动标记而不想使用中心位置,因为我在下面的示例中显示红色区域的内容:

enter image description here

知道如何在左上方移动标记吗?

感谢。

2 个答案:

答案 0 :(得分:0)

您可以轻松地移动地图,指定一个新的中心..(新的坐标为样本)

var newCenter = new google.maps.LatLng(50.5073509, 0.1200)

 map.setCenter(newCenter);

答案 1 :(得分:0)

您可以通过javascript函数更改标记

function changeMarkerPosition(marker) {
var latlng = new google.maps.LatLng(-24.397, 140.644);
marker.setPosition(latlng);

}

了解更多信息See this link

相关问题