GMap折线中心位置

时间:2010-06-08 12:51:10

标签: google-maps map polyline

当我创建Gmap时,她需要建立中心并缩放

  

我们通过GMap2创建地图后   构造函数,我们需要初始化它。   完成初始化   使用地图的setCenter()   方法。 setCenter()方法   需要一个GLatLng坐标和一个   缩放级别和此方法必须   在任何其他操作之前发送   在地图上执行,包括   设置的任何其他属性   地图本身。

由于此路线未位于中心 - 这是一个示例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); 
  ) 
));

2 个答案:

答案 0 :(得分:2)

我解决了我的问题

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);
  }
});

答案 1 :(得分:1)

  

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);
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);

} });