簡體   English   中英

Google Maps v3-設置標記中心

[英]Google Maps v3 - Setting bounds to center of markers

盡管花了一整天的時間搜索並嘗試了許多編碼方法,但我仍然無法正確地對中和縮放Google地圖。

我想知道是否有人會指出我在我的代碼中做錯了什么。 在v2中一切正常,但是Im無法更新到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尋求任何幫助:)

在您的代碼中未定義“數組”。 參見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...]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM