谷歌地图点击移动标记

时间:2010-12-21 22:41:44

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

我想做的是,

在我的地图上,当我点击地图上的某个地方时,它会在点上显示一个标记,然后我点击地图上的不同点,然后它会显示另一个标记。但我希望它将第一个标记移动到第二个点。

(我把“代码放在html标签后面。”

这是我的代码:

<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>

3 个答案:

答案 0 :(得分:19)

每次运行placemarker()时,都会创建一个新标记。

您需要在地标标记功能之外创建标记,然后在地标标记内部使用marker.setPosition()

答案 1 :(得分:9)

另一个解决方案是移动一个标记,因为你只需要用户marker.setPosition()。 (感谢kjy112的警告:)

<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">
    var marker;

    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) {



            if (marker == undefined){
                marker = new google.maps.Marker({
                    position: location,
                    map: map, 
                    animation: google.maps.Animation.DROP,
                });
            }
            else{
                marker.setPosition(location);
            }
            map.setCenter(location);

        }


    }

</script>
</head>


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

答案 2 :(得分:1)

要删除标记,只需设置setMap(null)即可。

<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">
    var oldMarker;

    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) {

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

            });

            if (oldMarker != undefined){
                oldMarker.setMap(null);
            }
            oldMarker = marker;
            map.setCenter(location);

        }


    }

</script>
</head>


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