Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

javascript - Google Map API v3 - 设置边界和中心(Google Map API v3 — set bounds and center)

I've recently switched to Google Maps API V3.(我最近切换到Google Maps API V3。)

I'm working of a simple example which plots markers from an array, however I do not know how to center and zoom automatically with respect to the markers.(我正在编写一个简单的例子来绘制数组中的标记,但是我不知道如何相对于标记自动居中和缩放。) I've searched the net high and low, including Google's own documentation, but have not found a clear answer.(我搜索了网络的高低,包括谷歌自己的文档,但没有找到一个明确的答案。) I know I could simply take an average of the co-ordinates, but how would I set the zoom accordingly?(我知道我可以简单地采用坐标的平均值,但是如何相应地设置缩放?) function initialize() { var myOptions = { zoom: 10, center: new google.maps.LatLng(-33.9, 151.2), mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); setMarkers(map, beaches); } var beaches = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.423036, 151.259052, 5], ['Cronulla Beach', -34.028249, 121.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.450198, 151.259302, 1] ]; function setMarkers(map, locations) { var image = new google.maps.MarkerImage('images/beachflag.png', new google.maps.Size(20, 32), new google.maps.Point(0,0), new google.maps.Point(0, 32)); var shadow = new google.maps.MarkerImage('images/beachflag_shadow.png', new google.maps.Size(37, 32), new google.maps.Point(0,0), new google.maps.Point(0, 32)); var lat = map.getCenter().lat(); var lng = map.getCenter().lng(); var shape = { coord: [1, 1, 1, 20, 18, 20, 18 , 1], type: 'poly' }; for (var i = 0; i < locations.length; i++) { var beach = locations[i]; var myLatLng = new google.maps.LatLng(beach[1], beach[2]); var marker = new google.maps.Marker({ position: myLatLng, map: map, shadow: shadow, icon: image, shape: shape, title: beach[0], zIndex: beach[3] }); } }   ask by Michael Bradley translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, simply declare your new bounds object.(是的,只需声明您的新边界对象。)

var bounds = new google.maps.LatLngBounds(); Then for each marker, extend your bounds object:(然后为每个标记扩展您的边界对象:) bounds.extend(myLatLng); map.fitBounds(bounds); API: google.maps.LatLngBounds(API: google.maps.LatLngBounds)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...