2013-03-09 90 views
1

尽管花了整天的搜索和尝试无数的代码方法,但我仍然遇到了尝试正确居中和缩放谷歌地图的问题。谷歌地图v3 - 设置界标到标记中心

我不知道是否有人会那么善良,以指出我的代码中我做错了什么。在v2中一切正常,但我很难更新到v3。

有关信息 地图是一个更大的PHP应用程序的一小部分,并且所需要的纬度&长地图参数已经通过它们在主PROG使用和声明其他方法如下制得: -

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的任何帮助:)

+1

它正在做什么?你有任何的JavaScript错误?既然你只有2点,它应该没有循环工作。你能提供一个链接到一个展现问题的地图(或创建一个jsfiddle)吗? – geocodezip 2013-03-09 07:49:07

+0

它似乎显示确定..只是fitBounds似乎不起作用,因此没有正确地居中两点之间。 http://jsfiddle.net/klaw/tQf9n/3/ – klaw 2013-03-09 11:36:49

回答

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

谢谢您回复我已经取代 VAR LatLngList = [ \t \t \t [新google.maps.LatLng(e_lat,e_long)], \t \t \t [new google.maps.LatLng(h_lat,h_long)] \t \t \t]; 但它仍然表现出相同的行为,并且仍然没有约束边界内的2个点,并且地图不适合正确的缩放,也不适合居家与目的地之间的中点。 :/ 谢谢您的帮助和建议,到目前为止.. – klaw 2013-03-09 11:34:57

+0

'VAR LatLngList = [新google.maps.LatLng(e_lat,e_long),新google.maps.LatLng(h_lat,h_long)]' HTTP: //jsfiddle.net/tQf9n/4/ – 2013-03-09 14:06:46

+0

YAY!非常感谢你帮助这样的n00b。 它对它进行排序,并且它现在完美地居中。 – klaw 2013-03-09 15:52:51