Google maps api 3 如何找到google地图路线的边界?
我正在Google maps中使用存储的数据对象创建路线:Google maps api 3 如何找到google地图路线的边界?,google-maps-api-3,bounds,Google Maps Api 3,Bounds,我正在Google maps中使用存储的数据对象创建路线: routeData = { origin: 'address A', destination: 'address B', travelMode: google.maps.DirectionsTravelMode.DRIVING, waypoints: [obj, obj, obj], optimizeW
routeData = {
origin: 'address A',
destination: 'address B',
travelMode: google.maps.DirectionsTravelMode.DRIVING,
waypoints: [obj, obj, obj],
optimizeWaypoints: false
};
因为这张地图上还有其他对象,所以我设置了optimizeWaypoints:false
然后我使用以下代码绘制路线:
// Instantiate a directions service.
var directionsService = new google.maps.DirectionsService;
// Create a renderer for directions and bind it to the map.
directionsDisplay = new google.maps.DirectionsRenderer({
preserveViewport: true,
suppressMarkers: true,
map: map
});
directionsService.route(routeData, function(result, status) {
directionsDisplay.setDirections(result);
});
方向显示
是一个全局变量
在地图上绘制完所有对象后,我将遍历所有对象以获得边界
fitMapObjectBounds: function(mapData){
var bounds = new google.maps.LatLngBounds();
var marker, circle, route = null;
var fitTheBounds = false;
// Marker
$(mapData.markers).each(function(key, value){
marker = new google.maps.Marker(value);
bounds.extend(marker.getPosition());
fitTheBounds = true;
});
// Circles
$(mapData.circles).each(function(key, value){
circle = new google.maps.Circle(value);
bounds.union(circle.getBounds());
fitTheBounds = true;
});
if(fitTheBounds == true) {
map.fitBounds(bounds);
}
}
我的问题是,我如何获得路线的边界?我真的找不到这方面的好例子
更新因此,我在我的
fitMapObjectBounds
函数中添加了此代码,但它不起作用:
directionsService = new google.maps.DirectionsService;
directionsService.route(mapData.routeStreet, function(result, status) {
directionsDisplay.setDirections(result);
bounds.union(directionsDisplay.getDirections().routes[0].bounds);
});
默认情况下显示的路线边界(返回的第一个)为 代码片段:
函数初始化(){
var map=new google.maps.map(
document.getElementById(“地图画布”){
中心:新google.maps.LatLng(37.4419,-122.1419),
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var routeData={
来源:纽约州纽约市,
目的地:“宾夕法尼亚州费城”,
travelMode:google.maps.Directions travelMode.DRIVING,
航路点:[],
优化航路点:错误
};
//实例化一个方向服务。
var directionsService=新的google.maps.directionsService;
//为方向创建渲染器并将其绑定到地图。
directionsDisplay=新建google.maps.DirectionsRenderer({
对,,
对,,
地图:地图
});
路由(路由数据、功能(结果、状态){
方向显示。设置方向(结果);
map.fitBounds(directionsDisplay.getDirections().routes[0].bounds);
});
//fitObjectBounds()
}
google.maps.event.addDomListener(窗口“加载”,初始化)代码>
html,
身体,
#地图画布{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}
最好的方法是,您可以创建一个JSFIDLE,其中包含我们可以测试的完整/工作代码。但是为什么你要在你的循环中创建新的google.maps.XXXX
?是否每个标记/圆圈/其他对象都尚未创建并存储在任何位置?我们有3个不同的地图(3个不同的视图)。一个用于读者(前端),一个用于编辑处理文本,另一个用于编辑处理地图。我们只存储JSON数据,而不存储整个对象。为什么不存储指向全局变量的方向路由,您可以在fitMapObjectBounds
函数中重用该全局变量?为什么这个Q会被否决?这是一个合法的问题!你愿意花多少钱买一张选票?只是开玩笑。。。但你永远也不会知道,除非投反对票的人告诉你为什么……;)太好了。但是,如何在我的fitMapObjectBounds()
函数中使用directionsDisplay.getDirections().routes[0].bounds
而不必重新绘制路由?保留对每个directionsDisplay
对象的引用(每个路由一个)或保留来自`directionsService(其中有边界)的每个结果的结果。如果您在这样做时遇到问题,请在问题中添加一个。
directionsDisplay.getDirections().routes[0].bounds