1

尽管花了一整天的时间搜索并尝试了许多代码方法,但我仍然无法正确地居中和缩放谷歌地图。

我想知道是否有人会这么好心地指出我在我的代码中做错了什么。在 v2 中一切正常,但我很难更新到 v3。

参考 该地图是一个更大的 php 应用程序的一小部分,所需的纬度和经度地图参数已经通过主程序中使用的其他方法获得,并声明如下:-

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

除了缩放和中心之外,一切似乎都正常。有问题的代码如下: -

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 寻求帮助:)

4

1 回答 1

1

在您的代码中未定义“数组”。请参阅jsFiddle

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

你应该使用

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

或者

var LatLngList = [new google.maps.LatLng...]
于 2013-03-09T08:30:34.147 回答