klaw
klaw

Reputation: 13

Google Maps v3 - Setting bounds to center of markers

Despite spending all day searching and trying numerous code methods I am still having problems with trying to center and zoom a google map correctly.

I wonder if someone would be so kind as to point out the obvious as to what Im doing wrong in my code. Everything worked fine in v2 but Im having difficulty updating to v3.

For information The map is a small part of a much larger php application, and the needed lat & long map parameters have already been obtained by other methods which are used in the main prog and declared as follows:-

var myDestination=new google.maps.LatLng(e_lat,e_long);
var myHome=new google.maps.LatLng(h_lat,h_long);

Everything seems to be working ok aside from the zoom and center. Problematic Code is as follows:-

function initialize()
{

// set the map properties
var mapProp = {
  center:myHome,
  zoom:12,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

// set the home marker
var marker_home=new google.maps.Marker({
  position:myHome,
  icon:'house.png'
   });

  marker_home.setMap(map);

//set the destination marker  
var marker_destination=new google.maps.Marker({
  position:myDestination,
  icon:'flag_red.png'
  });

  marker_destination.setMap(map);



//google polyline between the 2 points 
 var myPolyline = [myDestination,myHome];
 var directPath = new google.maps.Polyline({
  path:myPolyline,
  strokeColor:"#0000FF",
  strokeOpacity:0.8,
  strokeWeight:2  
});

directPath.setMap(map);


//  Make an array of the LatLng's of the markers you want to show
var LatLngList = array (new google.maps.LatLng (e_lat,e_long), new google.maps.LatLng  (h_lat,h_long));
//  Create a new viewpoint bound
var bounds = new google.maps.LatLngBounds ();
//  Go through each...
for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) {
//  And increase the bounds to take this point
bounds.extend (LatLngList[i]);
}
//  Fit these bounds to the map
map.fitBounds (bounds);

}

google.maps.event.addDomListener(window, 'load', initialize);

TIA for any help :)

Upvotes: 1

Views: 2832

Answers (1)

Mr.Pohoda
Mr.Pohoda

Reputation: 494

In your code "array" is not defined. See jsFiddle.

var LatLngList = array (new google.maps.LatLng (e_lat,e_long), new google.maps.LatLng  (h_lat,h_long));

You should use

var LatLngList = new Array(new google.maps.LatLng...)

or

var LatLngList = [new google.maps.LatLng...]

Upvotes: 1

Related Questions