中心
问题描述:
的GMAP折线位置当我创建GMAP她需要建立中央和放大中心
一旦我们通过的GMap2 构造函数创建一个地图,我们需要对它进行初始化。 使用地图的setCenter() 方法完成此初始化 。必须 发送 需要的GLatLng坐标setCenter()方法和一个 缩放级别和该方法的任何其它操作是在地图上进行 之前,包括 设置 地图本身的任何其他属性。
因为这条路线的不位于中心 - 这是一个例子http://grab.by/4OD6
我应该坐标的基础上得到的中心?
并获得显示所有对象地图的缩放?
我的代码:
var TrainingGMap = Class.create ((
initialize: function (div_id, points, options) (
this.options = Object.extend ((), options)
this.points = points;
this.map = new GMap2 (document.getElementById (div_id));
this.map.setCenter (new GLatLng (this.points [0]. lan, this.points [0]. lon), 12);
this.map.setUIToDefault();
this.set_route();
)
set_route: function() (
var line = new Array();
for (var i = 0; i <this.points.length; i + +) (
line [i] = new GLatLng (this.points [i]. lat, this.points [i]. lon);
)
var polyline = new GPolyline (line, "# aa0000", 5);
this.map.addOverlay (polyline);
)
));
答
我解决我的问题
this.map.setCenter(polyline.getBounds().getCenter());
this.map.setZoom(this.map.getBoundsZoomLevel(polyline.getBounds()));
新代码
var TrainingGMap = Class.create({
initialize: function(div_id, points, options) {
this.options = Object.extend({}, options)
this.points = points;
this.map = new GMap2(document.getElementById(div_id));
this.map.setCenter(new GLatLng(this.points[0].lan, this.points[0].lon), 12);
this.map.setUIToDefault();
var line = new Array();
for (var i = 0; i < this.points.length; i++) {
line[i] = new GLatLng(this.points[i].lan,this.points[i].lon);
}
var polyline = new GPolyline(line, "#aa0000", 5);
this.map.setCenter(polyline.getBounds().getCenter());
this.map.setZoom(this.map.getBoundsZoomLevel(polyline.getBounds()));
this.map.addOverlay(polyline);
}
});
答
VAR TrainingGMap = Class.create({ 初始化:功能(div_id ,积分,选项){ this.options = Object.extend({,options) this.points = points; this.map = new GMap2(document.getElementById(div_id)); (新的GLatLng(this.points [0] .lan,this.points [0] .lon),12);
this.map.setUIToDefault();
var line = new Array();
for (var i = 0; i < this.points.length; i++) {
line[i] = new GLatLng(this.points[i].lan,this.points[i].lon);
}
var polyline = new GPolyline(line, "#aa0000", 5);
var tengah=Math.round(polyline.getVertexCount()/2)-1;
this.map.setCenter(polyline.getVertex(tengah));
this.map.setZoom(this.map.getBoundsZoomLevel(polyline.getBounds()));
this.map.addOverlay(polyline);
} });