删除谷歌地图标记 [英] Delete google maps marker

查看:135
本文介绍了删除谷歌地图标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个脚本

$('.check_orders_dl').click(function(){

    var get_maps = function(){
        $.ajax({
            type: "POST",
            url:"/tracking.php",
            success:function(data, textStatus, jqXHR) {

                var objs = jQuery.parseJSON( data );
                var obj = objs[0];

                $('.lat').val(obj.latitude);
                $('.long').val(obj.longitude);
            },
            error: function(jqXHR, textStatus, errorThrown){

            }
        });
    };
    var latitude = $('.lat').val();
    var longitude = $('.long').val();
    var markers = [];
    var map,

            mapCenter = new google.maps.LatLng(40.700683, -73.925972),
            map;

        function initializeMap()
        {
            map = new google.maps.Map(document.getElementById('map_canvas'), {
               zoom: 16,
               center: mapCenter,
               mapTypeId: google.maps.MapTypeId.ROADMAP
             });
        }


        function setCurrentPosition() {
            var latitude = $('.lat').val();
            var longitude = $('.long').val();
            var markers = new google.maps.Marker({
                map: map,
                position: new google.maps.LatLng(
                    latitude,
                    longitude
                ),
                title: "Current Position"
            });
            map.panTo(new google.maps.LatLng(
                    latitude,
                    longitude
                ));


        }



        function initLocationProcedure() {
            initializeMap();
        }

        initLocationProcedure();


    setInterval(setCurrentPosition,1000);

    setInterval(get_maps, 1000);
});

它让我每隔1秒就从gps发件人的坐标数据库中保存的信息,I将它们加载到谷歌地图中,​​但它创建了一个新标记,而不是替换上次创建的标记。

And it's giving me back each 1second the information saved from database with the coordates from the gps sender, I loaded them into google maps, but it's creating a new mark instead of replacing the last mark created.

我需要删除最后生成的标记或替换生成的新标记1秒后。任何想法?

I need to delete the last generated mark or replace with the new one generated after 1 second. Any ideas?

推荐答案

不要每次都创建一个新标记,如果它已经存在或者将其移动到新的位置或

Don't create a new marker every time, if it already exists either move it to the new location or hide it before creating the new one.

// in the global scope
marker;
function setCurrentPosition() {
    var latitude = $('.lat').val();
    var longitude = $('.long').val();
    if (marker && marker.setPosition) {
      // not the first time, move the existing marker
      marker.setPosition(new google.maps.LatLng(
        latitude,
        longitude
      ));
    } else { // first time, create a marker
      marker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(
            latitude,
            longitude
        ),
        title: "Current Position"
    });
    map.panTo(new google.maps.LatLng(
            latitude,
            longitude
        ));
}

这篇关于删除谷歌地图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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