简体   繁体   English

使用phonegap和Google Maps API删除单个标记

[英]Remove single marker with phonegap and google maps api

I'm using phonegape, google maps api v3 and navigator.geolocation.watchPosition function in my project. 我在我的项目中使用phonegape,google maps api v3和navigator.geolocation.watchPosition函数。

I try to follow my position. 我尝试跟随我的位置。 I have problem with markers. 我的标记有问题。 Application working good, but doesn't remove old markers. 应用程序运行良好,但不会删除旧标记。 It creates new markers all time, when i will get new coords. 当我得到新坐标时,它将一直创建新的标记。

    function onSuccess(position) {

    var image = 'arrow1.png';

             var pos_table = ['Here I am', position.coords.latitude, position.coords.longitude];
             var where_i_am = new google.maps.LatLng(pos_table[1], pos_table[2]);
             var my_position = new google.maps.Marker({
                 position: where_i_am,
                 map: map,
                 title: pos_table[0],
                 icon: image
             });

             map.panTo(new google.maps.LatLng(
                    position.coords.latitude,
                    position.coords.longitude
                ));

    }

I added setmap(null), but now there aren't any markers on the map 我添加了setmap(null),但是现在地图上没有任何标记

 for (var i = 0; i < my_position.length; i++) {
    my_position[i].setMap(null);
 }

When I try : 当我尝试:

if (my_position) {
    for (var i = 0; i < my_position.length; i++) {
        my_position[i].setMap(null);
    }
}

it dosen't work at all. 它根本不起作用。 Please help me. 请帮我。 Thx. 谢谢。

Instead of trying to delete old markers, you could try to use update the position of the old marker. 与其尝试删除旧标记,不如尝试使用更新旧标记的位置。

var my_position;
function onSuccess(position) {

    var image = 'arrow1.png';
    var pos_table = ['Here I am', position.coords.latitude, position.coords.longitude];
    var where_i_am = new google.maps.LatLng(pos_table[1], pos_table[2]);
    if (my_position) {
        my_position.setPosition(where_i_am);
    } else {
        my_position = new google.maps.Marker({
            position: where_i_am,
            map: map,
            title: pos_table[0],
            icon: image
        });

    }

    map.panTo(where_i_am);

}

Change to 改成

 var my_position = new Array();
 function onSuccess(position) {
for(var i = 0; i< 10; i++){
    var image = 'arrow1.png';

             var pos_table = ['Here I am', position.coords.latitude, position.coords.longitude];
             var where_i_am = new google.maps.LatLng(pos_table[1], pos_table[2]);
             my_position[i] = new google.maps.Marker({
                 position: where_i_am,
                 map: map,
                 title: pos_table[0],
                 icon: image
             });

             map.panTo(new google.maps.LatLng(
                    position.coords.latitude,
                    position.coords.longitude
                ));

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM