【问题标题】:Angular Google Maps get all locations inside the maps boundariesAngular Google Maps 获取地图边界内的所有位置
【发布时间】:2018-09-24 13:05:21
【问题描述】:

我在一个 Angular 6 项目中使用这个框架:

这是我第一个使用 Angular 的项目,所以我仍在弄清楚它在某些时候是如何工作的。 我的项目现在的状态如下: 我能够以特定位置为中心加载地图。它还会在缩小时加载标记并将它们聚集在一起。在侧面菜单中列出了所有标记项并显示有关位置的一些信息。我还实现了一些用户交互:将鼠标悬停在侧面菜单中的标记或列表项上会突出显示列表/地图中的相应项。单击标记或列表项时,侧面菜单中还会显示一些详细信息。到目前为止一切顺利。

现在我想提取位于地图当前边界内的所有标记,以缩短侧面菜单中的列表。 在组件的模板中,我使用的地图如下(我已经提取了使用地图的部分):

[...]

<!-- Map Section -->
<div class="main-data-container content-box">
  <div class="split-view-container" #parentData>
    <app-spinner *ngIf="!allItems"></app-spinner>
    <agm-map [style.height.px]="parentData.offsetHeight"
             [latitude]="mapCenter.lat"
             [longitude]="mapCenter.lon"
             [zoom]="mapZoom"
             #agmMap>
      <agm-marker-cluster [styles]="[companyStyle]">
        <agm-marker
          *ngFor="let companyLocation of companyLocations"
          (markerClick)="clickedMarker(companyLocation)"
          (mouseOver)="hoverOverMarker(companyLocation)"
          (mouseOut)="hoverOutMarker(companyLocation)"
          [latitude]="companyLocation.latitude"
          [longitude]="companyLocation.longitude"
          [iconUrl]="{url: setIcon(companyLocation), scaledSize: {width: 55, height: 82.5}}">
        </agm-marker>
      </agm-marker-cluster>
    </agm-map>
  </div>

  [...]

companyLocations: typescript 文档中的数组,其中包含有关公司多个位置的信息。

#agmMap:通过这个我将 html 元素绑定到 typescript 文件中的一个对象。

现在到我正在努力的 Typescript 部分:

[...]

@ViewChild('agmMap') agmMap: any;

companyLocations: Array<CompanyLocation>;

filteredCompanyLocations: Array<CompanyLocation>;

[...]

checkMarkersInBounds() {

    // Initialize Filtered Arrays as empty Arrays
    this.filteredCompanyLocations = [];

    // Run through all CompanyLocations
    for(let company of this.companyLocations){

      //write coordinates from companyLocation into LatLng Object 
      let loc: LatLngLiteral = {lat: company.latitude, lng: company.longitude};

      //Check if the Location is in Bounds of the Map
      if (this.agmMap.getBounds().contains(loc)){

        //If it's in Bounds add it to the filtered Array
        this.filteredCompanyLocations.push(company);

      }
    }
  }

[...]

.getBounds() 出现错误:

TypeError: this.agmMap.getBounds 不是函数。

当尝试用this.agmMap._mapsWrapper.getBounds().contains(loc) 切换出this.agmMap.getBounds().contains(loc) 时,它会抛出:

TypeError: this.agmMap._mapsWrapper.getBounds(...).contains 不是函数。

我已经在这里和其他地方针对不同但相似的意图尝试了几种解决方案 postet 并将其调整到我的应用程序:

我希望有人得到一些提示,甚至是解决方案。从那以后,我一直在为此苦苦挣扎。也许我只是对解决这个问题视而不见,而我忽略了一些重要的东西......

【问题讨论】:

    标签: angular typescript google-maps angular-google-maps


    【解决方案1】:

    我找到了解决这个问题的方法。 在模板中,将事件输出添加到地图的(boundsChange) 输出。这会返回地图的边界,并且还会在边界发生变化时触发(显然):

    [...]
    <agm-map
            [style.height.px]="parentData.offsetHeight"
            [latitude]="mapCenter.lat"
            [longitude]="mapCenter.lon"
            [zoom]="mapZoom"
            (boundsChange)="checkMarkersInBounds($event)">
    [...]
    

    在 TS 文件中我实现了这样的checkMarkersInBounds(bounds) 方法:

    checkMarkersInBounds(bounds) {
    
        this.filteredItems = [];
    
        for(let company of this.companyLocations){
    
          let companyPosition = {lat: company.latitude, lng: company.longitude};
    
          if (bounds.contains(companyPosition)){
    
            //Put the item in an Array which can be shown in the List
    
          }
        }
      }
    

    【讨论】:

      【解决方案2】:

      如果未过滤的列表很长,那么您可能需要添加一个去抖动计时器。

        private debounceTimer = null;
      
        public checkMarkersInBounds(bounds: any): void {
          // debounce timer prevents processing of marker filters until map stops moving for 200ms
          if (this.debounceTimer !== null) {
            clearTimeout(this.debounceTimer);
            this.debounceTimer = null;
          }
          this.debounceTimer = setTimeout(() => {
            this.companyFilter = [];
            for (const companyof this.companyLocations) {
              if (bounds.contains({ lat: company.lat, lng: company.long })) {
      
                //Put the item in an Array which can be shown in the List
      
              }
            }
      
          }, 200);
        }
      

      【讨论】:

        猜你喜欢
        • 2015-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-10
        相关资源
        最近更新 更多