使用GMap APi v3,我使用“指导”服务为用户生成路由,但是一旦在地图上呈现该路由,我放置的目标信息就会被裁剪,因为映射集中在路由上,而不是目标上。
我如何告诉通用汽车公司:
发布于 2011-12-17 11:11:04
我通过在.setCenter回调中使用directionsService ()(在接收到适当的响应之后)成功地完成了这一任务。但是,这只在使用超时时才有效。这可能不是最纯粹的解决办法,但这是我目前唯一可用的解决办法。
/* _x & _y are passed as coordinates */
var _latLng=new google.maps.LatLng(_x,_y);
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsRenderer.setDirections(response);
window.setTimeout(function(){
gmap.setCenter(_lagLng);
}, 500);
}
}如果调用.setCenter()太早,则gmap仍然在构建,一旦完成,就会以路由为中心--从而将最后的命令重写为.setCenter() --但是,使用超时作为解决办法,调用将在构建映射和呈现路由之后进行。
发布于 2018-01-31 09:56:42
这是一个老问题,但这里有一个没有setTimeout的解决方案。您可以使用“tilesloaded”事件:
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(gmap);
directionsService.route(request, function(result, status) {
if (status === 'OK') {
directionsDisplay.setDirections(result);
var listener = gmap.addListener('tilesloaded', function(){
// add your centering code here
// or open your infowindow here and maps will auto center like expected
google.maps.event.removeListener(listener);
});
}else{
console.warn('Error retreiving directions');
}
});https://stackoverflow.com/questions/8535167
复制相似问题