最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how can remove the last marker google maps v3 - Stack Overflow

programmeradmin1浏览0评论

I have a html form for adding new places in my data ,

I want the user add marker and delete last marker ,

I found this code but it is added more than one marker

How can make it delete the last one?

because I need just one marker? this is the demo of form .php

<script type="text/javascript">

var map;
function mapa()
{
    var opts = {'center': new google.maps.LatLng(26.12295, -80.17122), 
    'zoom':11, 'mapTypeId': google.maps.MapTypeId.ROADMAP }

    map = new google.maps.Map(document.getElementById('mapdiv'),opts);

                google.maps.event.addListener(map,'click',function(event) {

                        document.getElementById('long').value = event.latLng.lng();
                        document.getElementById('lat').value = event.latLng.lat();

                    marker = new google.maps.Marker({
                            map: map,
                            position: new google.maps.LatLng(event.latLng.lat(),event.latLng.lng()),
                  });

                  }


);

}
</script>

please help me ,

I have a html form for adding new places in my data ,

I want the user add marker and delete last marker ,

I found this code but it is added more than one marker

How can make it delete the last one?

because I need just one marker? this is the demo of form http://saudi-hotels.info/add_hotel.php

<script type="text/javascript">

var map;
function mapa()
{
    var opts = {'center': new google.maps.LatLng(26.12295, -80.17122), 
    'zoom':11, 'mapTypeId': google.maps.MapTypeId.ROADMAP }

    map = new google.maps.Map(document.getElementById('mapdiv'),opts);

                google.maps.event.addListener(map,'click',function(event) {

                        document.getElementById('long').value = event.latLng.lng();
                        document.getElementById('lat').value = event.latLng.lat();

                    marker = new google.maps.Marker({
                            map: map,
                            position: new google.maps.LatLng(event.latLng.lat(),event.latLng.lng()),
                  });

                  }


);

}
</script>

please help me ,

Share Improve this question edited Aug 15, 2012 at 3:36 jeff 8,3582 gold badges34 silver badges43 bronze badges asked Aug 15, 2012 at 3:20 Ahmad AhmadAhmad Ahmad 131 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can create a global variable that tracks the last created marker:

var lastMarker;

And then, whenever you create a new marker, set lastMarker to the newly created marker:

lastMarker = new google.maps.Marker({
    map: map,
    position: new google.maps.LatLng(26.12295, -80.17122)
});

Then, whenever you want to remove the last created marker, you can use the lastMarker variable:

lastMarker.setMap(null);
发布评论

评论列表(0)

  1. 暂无评论