【问题标题】:Trying to use the place attribute for showing/hiding markers in Google Maps API尝试使用 place 属性在 Google Maps API 中显示/隐藏标记
【发布时间】:2012-11-20 05:16:02
【问题描述】:

好的,差不多解决了。我正在尝试使用来自附近搜索的地点属性(如下所示)

var request = {
  location: pyrmont,
  radius: '500',
  types: ['atm','bus_station','parking']
};
infowindow = new google.maps.InfoWindow();
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);

标记存储在一个数组中(如下)

    function createMarker(place) {
      var iconType = {};
      iconType['atm'] = "http://maps.google.com/mapfiles/ms/micons/dollar.png";
      iconType['bus_station'] = "http://maps.google.com/mapfiles/ms/micons/bus.png";
      //iconType['restaurant'] = "http://maps.google.com/mapfiles/ms/micons/restaurant.png";
      iconType['parking'] = "http://maps.google.com/mapfiles/ms/micons/parkinglot.png";
      var placeLoc = place.geometry.location;
      var marker = new google.maps.Marker({
      map : map,
      icon : iconType[place.types[0]],
      position : place.geometry.location,
      types : place.types
    });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.setContent(place.name);
      infowindow.open(map, this);
    });

    return marker;
  }

现在的问题是下面的'if'语句与数组中的位置不匹配,与我输入的字符串,我不确定语句本身是否错误,或者我是否没有检索到 place.types 属性正确地来自 Google 请求。

      function onRemoveBtn_Clicked() {
    var i;
    for (i = 0; i < markers.length; i++) {
      if (markers[i].get("types") != ATM) {
      markers[i].setMap(null);
    }
  }

到目前为止,我在这个问题上得到了很大的帮助,并感谢所有花时间帮助我的人,这是我最后一次打扰你:)

【问题讨论】:

    标签: jquery google-maps-api-3 google-maps-markers


    【解决方案1】:

    如果将类型数组设置为标记,则 marker.get("types") 返回该数组。 所以代码应该是这样的:

    function onRemoveBtn_Clicked() {
      var i, j, isHit, types;
      for (i = 0; i < markers.length; i++) {
        types = markers[i].get("types");
        if (types) {
          isHit = false;
          for (j = 0; j < types.length; j++) {
            if (types[j] == "atm") {
              isHit = true;
              break;
            }
          }
          if (isHit) {
            markers[i].setMap(null);
          }
        }
      }
    }
    

    【讨论】:

    • 你是我的英雄!像魅力一样工作
    猜你喜欢
    • 2012-11-08
    • 2019-06-04
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 2012-03-24
    • 2014-02-13
    • 1970-01-01
    相关资源
    最近更新 更多