我有在JavaScript函数中调用的Esri API函数,但是由于Esri API是异步的,因此不会返回正确的值,我的问题是我应该将函数转换为异步吗? 还有其他解决方案吗?
buildInfoTemplateFromAssignedFields(attributes: any): string {
let content = "<table class='table table-hover'>"
//some code here
let fieldHtml1 = '<tr><td style="width:40%;font-weight:bold;">';
fieldHtml1 += "Address";
fieldHtml1 += '</td><td style="width:60%">';
let fieldContent1 = attributes.entity;
for (let location of fieldContent1["Locations"]) {
//var point = this.convertToXy(location.Longitude, location.Latitude);
let longitude = location.Longitude;
let latitude = location.Latitude;
let wkid = "102100";
var points = [];
var outSR = wkid;
var geometryService = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
var inputpoint = new Point(longitude, latitude);
var params = new ProjectParameters();
params.geometries = [inputpoint];
params.outSR = new SpatialReference({ wkid: outSR });
geometryService.project(params, (result) => {
let outputpoint = result[0];
points.push(this.formatNumber(outputpoint.x));
points.push(this.formatNumber(outputpoint.y));
if (points[0] == this.formatNumber(attributes.extent[0]) && points[1] == this.formatNumber(attributes.extent[1])) {
fieldContent1 = `<div class="mapReadMore">${location.Address}</div>`;
fieldHtml1 += fieldContent1;
fieldHtml1 += '</td></tr>';
//problem is here, the value is not in the final content that this function is returning.
content += fieldHtml1;
}
else {
points = [];
}
})
}
const url = this.getItemUrl(attributes.type, attributes.entity.id, attributes.entity.Type);
content += `<tr><td colspan="2" style="text-align: center"><a onclick="document.dispatchEvent(new CustomEvent('navigateToMapPoint', {detail: '${url}'}))"><button type="button" class="btn btn-primary">View Record</button></a></td><tr></table>`;
return content;
}
我希望函数返回的content
值具有fieldhtml1
的值,但是没有,因为它是在完成项目函数之前返回的。