【发布时间】:2019-05-07 12:57:03
【问题描述】:
所以,我不得不更新之前使用 setTimeout 而不是 Callbacks 编写的一些 TypeScript 代码,问题是我无法使其正常工作。
使用 settimeouts 的功能正在运行,因此没有理由与配置相关(库=地点等)。另外,我可以在日志中看到它确实在工作,但我的地图是纯灰色的。
map_initialize(callback) {
navigator.geolocation.getCurrentPosition((position) => {
this.directionsService = new google.maps.DirectionsService();
this.localizacaoAtual = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var mapOptions = {
zoom: 8,
center: this.localizacaoAtual
}
if (this.origem == "atual") {
this.origem = this.localizacaoAtual;
}
callback({ mapOptions: mapOptions })
});
}
现在我要调用函数初始化
if (this.origem != null && this.destino != null) {
this.map_initialize((callback) => {
if (this.tipoMapa == "rota") {
this.map_calcRoute(callback);
} else if (this.tipoMapa == "radius") {
this.map_radius(callback);
}
});
}
if, 调用的第一个函数是实际工作的路线图。但是如果我将 'this.tipoMapa' 值更改为 'radius',它将无法加载。
map_radius(mapOptions: any) {
this.map = new google.maps.Map(document.getElementById('map'), mapOptions);
let service = new google.maps.places.PlacesService(this.map);
let start = this.origem;
let end = this.destino;
this.request = {
location: start,
radius: '1500',
name: ['something here']
}
service.nearbySearch(this.request, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var stepDisplay: any = new google.maps.InfoWindow();
for (var i = 0; i < results.length; i++) {
this.createMarker(results[i], stepDisplay);
}
}
this.entrada = { mensagem: status, origem: this.origem, destino: this.destino, instrucoes: this.instrucoes };
this.conversationService.sendMessage(this.entrada, this.context).subscribe(
data => {
this.conv_tratarRetorno(data);
}, error => { }
)
});
};
然后我有了我的 createMarker 函数。
createMarker(place, stepdisp) {
var marker = new google.maps.Marker({
map: this.map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function () {
stepdisp.setContent(place.name);
stepdisp.open(this.map, marker);
});
}
所以基本上当我调用一个路由函数时,它确实可以工作,但是如果我调用一个半径函数,它会返回一个灰色地图,但带有正确的日志。
提前致谢!
【问题讨论】:
标签: angular typescript google-maps callback angular-google-maps