javascript - Google map v3 - 将边界设置为标记中心

标签 javascript google-maps google-maps-api-3

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

我想知道是否有人会好心地指出我在代码中做错的地方。在 v2 中一切正常,但更新到 v3 时遇到困难。

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

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...]

关于javascript - Google map v3 - 将边界设置为标记中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15306640/

相关文章:

javascript - 如何使用 Commander.js 运行 bash?

javascript - 是否有可以显示正在执行的 JS 的 Google Chrome 扩展程序?

javascript - NodeJs : fs. appendFile 函数似乎错过了给出的最后一个字符串

java - 如何在Android上缩放标记,并正确设置shadow和onTap

javascript - 谷歌地图缩放事件: Ignored attempt to cancel a touchend event with cancelable=false

javascript - Cakephp Ajax,使用 Js Helper 获取文本字段的值

ios - 谷歌地图 - reverseGeocodeCoordinate 不会打印新加坡的地址

jquery - 谷歌地图只显示部分

google-maps - Google Maps API - 获取街道坐标

javascript - 在 Google map 的 JS API 中,根据事件, fromPointToLatLng 似乎不准确