google map v3的命令和v2有些差别。下面把我的代码贴出来。
这个功能主要是如果一个地图上有多个坐标,可以根据坐标点自动计算zoom值,防止有点坐标点跑到地图外面,导致显示不全。
<script type="text/javascript" src="http://ditu.google.cn/maps/api/js?v=3.exp&key=xxxxxxxx&language=zh-cn&sensor=false"></script>
planService.initMap = function (id, flightPlanCoordinates, areaList) { // by sjw
var lat = 34.0204989; //默认坐标点
var lng = -118.4117325; //默认坐标点
var map = new google.maps.Map(document.getElementById('map' + id), {
zoom: 6, //默认zoom=6
center: {lat: lat, lng: lng},
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var bounds = new google.maps.LatLngBounds(); //声明一个bounds
$.each(areaList, function (i, area) { //遍历坐标列表
var image = '../../resources/images/icon-' + area.imgName + '.png'; //声明图标
bounds.extend(new google.maps.LatLng(area.lat, area.lng));
var beachMarker = new google.maps.Marker({
position: {lat: area.lat, lng: area.lng},
map: map,
icon: image
});
})
if (areaList.length > 1) { //如果坐标点多余1个,则使用 google map 的fitBounds,自动计算zoom值
map.fitBounds(bounds);
}
var lineSymbol = { //把各点用虚线连起来
path: 'M 0,-1 0,1',
strokeOpacity: 1,
strokeWeight: 2, //线粗细
scale: 2 //虚线段长度
};
var line = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeOpacity: 0,
strokeColor: "#ed5565", //颜色
strokeWeight: 1,
icons: [{
icon: lineSymbol,
offset: '0',
repeat: '15px' //间距
}],
map: map
});
};