繁体   English   中英

检查选项是否已经存在

[英]Check if option exists already

我正在动态构建gMap插件的选项,需要了解如何检查其中一个标记是否已存在。

这是用javascript构建的方式:

success: function(data) {
  var markers = { controls: true, scrollwheel: true, markers: [], zoom: 8 };
  $.each(data["events"], function(id, event) {
    // .. do other stuff with the data
    if(showmap) {
      // add location to maps list prevent multiples
      //
      // How do I check to see if it already exists?
      //
      // event[] is a JSON Object, BTW
      //
      //if((markers.indexOf(event['LocLatitude']) == -1) && (markers.indexOf(event['LocLongitude']) == -1)) {
      marker1 = { latitude: event['LocLatitude'],
                  longitude:event['LocLongitude'],
                  html: '"'+event['LocName']+'<br />'+event['LocAddress']+'<br />'+event['LocCity']+', '+event['LocState']+'"',
                  icon:{image: "/images/gmap_pin_orange.png",
                        iconsize: [26, 46],
                        iconanchor: [12,46],
                        infowindowanchor: [12, 0]
                       }
                };
      markers.markers.push(marker1);
      //} // if((markers.indexOf(event['LocLatitude']) == -1)
    } // if(showmap)
  } // $.each(data["events"]
}, // success:

上面的注释代码显示了将其构建为字符串时我是如何做的,该技术不再起作用。 我需要知道如何在推送之前检查在markers.markers中是否已存在要添加的标记。

可以将数字标识分配给表,还是可以根据上下文创建标识,而不是将标记推入表中? 请参阅我对以下问题的回答: 如何通过ID选择和操作Google地图(V3 JS API)标记?

在这里,我正在遍历标记并检查我想要的标记,return false使我脱离$ .each,返回true继续。

marker_exists = false;
$.each( markers.markers, function(i, n){
  if(markers.markers[i].latitude == event['LocLatitude'] && markers.markers[i].longitude == event['LocLongitude']) {
    if(markers.latitude == "") {
      markers.latitude = event['LocLatitude'];
      markers.longitude = event['LocLongitude'];
    }
    marker_exists = true;
    return false;
  } else {
    return true;
  }
});

暂无
暂无

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

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