根据位置删除标记

时间:2012-11-05 02:40:06

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

我正在尝试根据地址移除地图上的标记,但由于某种原因它无法正常工作。它一定是显而易见的,我没有看到。即使我确保m和locationsall [i] [0]相同,它也不会进入deleteMarker方法中的if(m == locationsall [i] [0])。

    //alert(tmp);
    //alert(locationsall[0][0]);

添加到地图代码:

$('#map').show();
    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById("address").value +",  " + document.getElementById("city").value;

    geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
            var lat = results[0].geometry.location.lat();
            var lng = results[0].geometry.location.lng();
            locationsall[counter] = new Array();
            locationsall[counter][0] = address;
            locationsall[counter][1] = lat;
            locationsall[counter][2] = lng;
            var mapOptions = {
                    zoom: 13,
                    center: new google.maps.LatLng(lat, lng),
                    mapTypeId: google.maps.MapTypeId.ROADMAP
            }

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    var i;
    for (i = 0; i < locationsall.length; i++) {  
            locationsall[i][3] = marker = new google.maps.Marker({
            position: new google.maps.LatLng(locationsall[i][1], locationsall[i][2]),
            map: map
            });
            //markersArray.push(marker);
    }
    counter++;
    } else {
        alert("Geocode was not successful for the following reason: " + status);
    }
  });
}

从文本框中检索地址并将其格式化为“1 main street,city”

的代码
    var tmp = locations.splice(locations.indexOf(location), 1);
    deleteMarker(tmp);

删除标记代码:

function deleteMarker(m){
for (i = 0; i < locationsall.length; i++) { 
     if(m==locationsall[i][0]){
     alert(locationsall[i][0]); 
 alert(locationsall[i][3]);
      locationsall[i][3].setMap(null);
     } 
  }
}

1 个答案:

答案 0 :(得分:0)

当我比较m和locationsall [i] [0];

时,我有一个额外的空间

现在代码完美无缺。