我对角度很新,并且我认为这是一个基本问题。
我创建了我的地图
<agm-map [latitude]="lat" [longitude]="lng">
<agm-marker [latitude]="location.lat" [longitude]="location.lng" *ngFor="let location of locations; let i=index">
<agm-snazzy-info-window [maxWidth]="200" [closeWhenOthersOpen]="true">
<ng-template>
<strong>{{location.title}}</strong>
<p>adresse</p>
</ng-template>
</agm-snazzy-info-window>
</agm-marker>
</agm-map>
当我手动设置标记时,一切正常,但当我使用*ngFor
遍历我的列表时,会创建标记的html,但显然在地图脚本之后查找标记,因此没有标记渲染(地图本身)。
来自我的控制器:
export class LocationMapComponent implements OnInit {
lat: number = 51.678418;
lng: number = 7.809007;
locations: Location[];
async ngOnInit() {
}
public async getLocations() {
this.locations = await this._locationService.getLocations();
}
constructor(private _locationService:LocationService, private _coreCOMService: CoreCOMService, private _coreLanguageService: CoreLanguageService, private _coreLogService: CoreLogService, private _componentFactoryResolver: ComponentFactoryResolver, private _coreSystemService: CoreSystemService) {
this.getLocations();
}
}
从一个从远程数据库中获取它们的服务加载位置。我确实认为问题是在执行ngFor循环之前渲染了地图,我不确定如何确保首先执行循环或者如何告诉地图仅在循环之后(重新)渲染标记完了。
如上所述,这可能是非常基本的,但我现在感到茫然,谢谢。
答案 0 :(得分:9)
纬度/经度必须是Number类型。如果它们作为字符串从API或某种服务返回,则需要转换它们。根据我的经验,似乎要求将其严格键入数字。
答案 1 :(得分:0)
在显示标记时,我需要解决与ngFor有关的问题。
我从API(纬度,经度)收集数据,但我无法进行绑定。
我怀疑这可能是因为它没有将类型检测为“数字”。
map.interface.ts:
export interface Marker {
lat: number[];
lng: number[];
}
地图location.ts:
import { CapacitorBanksService } from '../../services/capacitorBanks.service';
import { Component, OnInit} from '@angular/core';
import { AuthService } from '../../services/auth/auth.service';
import { Marker } from './map.interface';
@Component({
selector: 'mdb-map-location',
templateUrl: './map-location.component.html',
styleUrls: ['./map-location.component.scss'],
providers: [AuthService, CapacitorBanksService]
})
export class MapLocationComponent implements OnInit {
localCustomer = localStorage.getItem('customer_id');
subscriptions: any;
latitude: number = 40;
longitude: number = 0;
lat: number[] = [];
lng: number[] = [];
markers: Marker[] = [{
lat: this.lat,
lng: this.lng,
}];
constructor(public authService: AuthService, public cbank: CapacitorBanksService) { }
ngOnInit(): void {
this.cbank.getCapacitorBanks(this.localCustomer).subscribe(ires => {
let data: any;
data = ires.data;
data = data.map(index => {
return index.id.id;
});
let indexx = 0;
const interval = setInterval(() => {
if ( data[indexx] ) {
this.subscriptions = this.cbank.getAssetAttributesServer(this.localCustomer, data[indexx]).subscribe(vres => {
const myObj = vres;
Object.keys(myObj).forEach(key => {
if (myObj[key].key === 'latitude') {
this.lat.push(myObj[key].value);
}
});
Object.keys(myObj).forEach(key => {
if (myObj[key].key === 'longitude') {
this.lng.push(myObj[key].value);
}
});
indexx ++;
});
} else {
this.subscriptions.unsubscribe();
}
if ( indexx >= data.length - 1 ) {
clearInterval(interval);
}
}, 400);
console.log('COORD: ', this.markers);
});
}
}
这是map-location.component.html:
<div id="content">
<agm-map [latitude]="latitude" [longitude]="longitude" [zoom]="6">
<agm-marker-cluster>
<!-- <agm-marker *ngFor="let marker of markers; let i=index"
<agm-marker *ngFor="let marker of markers; let i=index" [latitude]="marker.lat[i]" [longitude]="marker.lng[i]"></agm-marker>
</agm-marker-cluster>
</agm-map>
</div>