views:

296

answers:

3

When I create Gmap she needed to establish the center and zoom

Once we've created a map via the GMap2 constructor, we need to initialize it. This initialization is accomplished with use of the map's setCenter () method. The setCenter () method requires a GLatLng coordinate and a zoom level and this method must be sent before any other operations are performed on the map, including setting any other attributes of the map itself.

Because of this route is not positioned at the center - this is an example http://grab.by/4OD6

I should on the basis of the coordinates to get the center?

And get a zoom which displays all objects map?

My code:

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); 
  ) 
));
+1  A: 

I resolve my problem

this.map.setCenter(polyline.getBounds().getCenter());
this.map.setZoom(this.map.getBoundsZoomLevel(polyline.getBounds()));

New code

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);
  }
});
Shaliko
+1  A: 

i have the same problem! i'm getting data from a php page formatted in json... i can create markers but lines aren't showing!

mike
+1  A: 

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

} });

teguh