角度中的函数不使用此参数



我使用的是Google的API Geocode,在我需要使用this.singleGeocode和this.resultsMarker之前,该函数运行良好。这是我的组件代码:

interface Location {
lat: number;
lng: number;
}
singleGeocode: Location;
resultsMarkers: Location[];

addressToCoordinates(addressToSearch: string) {
this.geoCoder.geocode({ address: addressToSearch }, function ( results, status) {
debugger;
if (status == google.maps.GeocoderStatus.OK) {
let p = results[0].geometry.location;
debugger;
this.singleGeocode.lat = p.lat();
this.singleGeocode.lng = p.lng();
this.resultsMarkers.push(this.singleGeocode);
}
});
}

这是我的HTML代码:

<agm-map [latitude]="startingLat" [longitude]="startingLng" [zoom]="startingZoom" (zoomChange)="onZoomChange($event)">
<div *ngIf="selectedZoom > 4">
<agm-marker-cluster imagePath="https://raw.githubusercontent.com/googlemaps/v3-utility-library/master/markerclustererplus/images/m" [minimumClusterSize]="2">
<agm-marker *ngFor="let m of resultsMarkers; let i = index"
(markerClick)="clickedMarker(m.label, i)"
[latitude]="m.lat"
[longitude]="m.lng"
[label]="test"
[markerDraggable]="false">
</agm-marker>
</agm-marker-cluster>
</agm-map>

如何让函数读取我的这些参数,这样我的HTML就会使用它?或者我如何更改代码以使其工作?

编辑:我试图设置绑定,但这些参数仍然没有定义。

this.geoCoder.geocode({ address: addressToSearch }, function (results, status) {
debugger;
if (status == google.maps.GeocoderStatus.OK) {
let p = results[0].geometry.location;
debugger;
this.singleGeocode.lat = p.lat();
this.singleGeocode.lng = p.lng();
this.resultsMarkers.push(this.singleGeocode);
}
}.bind(this));
}

此外,我尝试使用=>但仍未确定:

this.geoCoder.geocode({ address: addressToSearch }, (results, status) => {
debugger;
if (status == google.maps.GeocoderStatus.OK) {
let p = results[0].geometry.location;
debugger;
this.singleGeocode.lat = p.lat();
this.singleGeocode.lng = p.lng();
this.resultsMarkers.push(this.singleGeocode);
}
});
}

我错过了什么?

在为singleGeocoderesultsMarkers赋值之前是否初始化它们?如果没有,那么这很可能就是问题所在。看看它是否有助于修改你的代码如下:

interface Location {
lat: number;
lng: number;
}
// class starts here
singleGeocode: Location;
resultsMarkers: Location[] = []; // new
addressToCoordinates(addressToSearch: string) {
this.geoCoder.geocode({ address: addressToSearch }, function(
results,
status
) {
if (status == google.maps.GeocoderStatus.OK) {
let p = results[0].geometry.location;
// new
this.singleGeocode = {
lat: p.lat(),
lng: p.lng()
};
this.resultsMarkers.push(this.singleGeocode);
}
});
}

相关内容

  • 没有找到相关文章