Google点击移动标记 [英] Google maps moving marker on click

查看:81
本文介绍了Google点击移动标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是,在我的地图上,当我点击地图上的某处时,它显示了一个标记点​​,然后点击不同的点在地图上显示另一个标记。但我希望它将第一个标记移动到第二个点。



(为了将代码放在这里,我把它放在html标签后面。)



这是我的代码:

 < html> 
<样式类型=text / css>
#map_canvas {
height:760px;
width:1100px;
position:static;
top:100px;
left:200px;
}
< / style>

< script type =text / javascript
src =http:// maps。 google.com/maps/api/js?sensor=false\">
< / script>

< script type =text / javascript>

函数initialize(){
var latlng = new google.maps.LatLng(42.55308,9.140625);

var myOptions = {
zoom:2,
center:latlng,
mapTypeId:google.maps.MapTypeId.ROADMAP,
streetViewControl:false,
mapTypeControl:false,
};

var map = new google.maps.Map(document.getElementById(map_canvas),
myOptions);


google.maps.event.addListener(map,'click',function(event){
placeMarker(event.latLng);
});

函数placeMarker(location){

var marker = new google.maps.Marker({
position:location,
map:map
动画:google.maps.Animation.DROP,

});
map.setCenter(location);

}


}

< / script>
< / head>


< body onload =initialize()>
< div id =map_canvasstyle =1500px; 1000px>< / div>
< / body>
< / html>


解决方案

每次 placemarker 已运行,它会创建一个新标记。



您需要在placemarker函数之外创建标记,然后在placemarker内部使用 marker.setPosition()


What I want to do is,

on my map, when I click somewhere on the map, it shows a marker on the point, then I click different point on the map then it shows a another marker. But I want it to move the first marker to the second point.

( I put " behind the html tags for putting the code here.)

This is my code:

<html>
<style type="text/css">
    #map_canvas {
        height: 760px;
        width: 1100px;
        position: static;
        top: 100px;
        left: 200px;
    }
</style>

<script type="text/javascript"
        src="http://maps.google.com/maps/api/js?sensor=false">
</script>

<script type="text/javascript">

    function initialize() {
        var latlng = new google.maps.LatLng(42.55308, 9.140625);

        var myOptions = {
            zoom: 2,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            streetViewControl: false,
            mapTypeControl: false,
        };

        var map = new google.maps.Map(document.getElementById("map_canvas"),
                myOptions);


        google.maps.event.addListener(map, 'click', function(event) {
            placeMarker(event.latLng);
        });

        function placeMarker(location) {

            var marker = new google.maps.Marker({
                position: location,
                map: map
                animation: google.maps.Animation.DROP,

            });
            map.setCenter(location);

        }


    }

</script>
</head>


<body onload="initialize()">
<div id="map_canvas" style="1500px; 1000px"></div>
</body>
</html>

解决方案

Every time placemarker() is ran, it creates a new marker.

You need to create the marker once outside of the placemarker function and after that, inside placemarker, use marker.setPosition().

这篇关于Google点击移动标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆