【发布时间】: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 并将其调整到我的应用程序:
- https://github.com/allenhwkim/angularjs-google-maps/issues/762
- https://github.com/SebastianM/angular-google-maps/issues/696
- Angular 5 google maps : show only markers in radius of # kilometers
- How can I check the marker is or isn't in the bounds using google maps v3
我希望有人得到一些提示,甚至是解决方案。从那以后,我一直在为此苦苦挣扎。也许我只是对解决这个问题视而不见,而我忽略了一些重要的东西......
【问题讨论】:
标签: angular typescript google-maps angular-google-maps