以下行将Google geocoderService实例,Google地图实例和回调函数传递给texture
:
calculateOriginCoordinates
其中定义为:
// Convert starting location addresses to coordinates, then calculate midpoint and set map's center
calculateOriginCoordinates(geocoderService, map, findMidpoint);
由于某种原因(大概是因为// Calculate origin coordinates
function calculateOriginCoordinates(geocoderService, map, callback) {
let start1Coords = convertAddressToCoordinates(geocoderService, document.getElementById("start1").value);
let start2Coords = convertAddressToCoordinates(geocoderService, document.getElementById("start2").value);
let midpoint = callback(map, start1Coords, start2Coords);
return midpoint;
}
// Find the midpoint of two coordinates
function findMidpoint(map, coord1, coord2) {
let midpoint = google.maps.geometry.spherical.interpolate(coord1, coord2, 0.5);
map.setCenter(midpoint);
map.setZoom(7);
return midpoint;
}
的执行时间大于convertAddressToCoordinates
的执行时间),findMidpoint
接收到的coord1
和coord2
未定义。
我尝试使用jquery promise和回调函数来解决这个问题,但是都没有用。如何使这些函数按顺序执行并防止未定义的变量?