我试图将谷歌地图放在我解码的折线上。我明白了
Cannot call method 'getSouthWest' of undefined
当我调用下面的 calcBounds 函数时。这是完整的代码:
var map;
$(document).ready(function () {
var gmCenter = new google.maps.LatLng(33.7489, -84.3881);
var gmMapType = google.maps.MapTypeId.ROADMAP;
var mapOptions = { center: gmCenter, zoom: 8, mapTypeId: gmMapType };
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
displayOnMap();
});
function displayOnMap() {
var decodedPath = google.maps.geometry.encoding.decodePath('@Model.Polyline');
var pathOptions = { path: decodedPath, strokeColor: "#FF0000", strokeOpacity: 0.5, strokeWeight: 5 };
var trackPath = new google.maps.Polyline(pathOptions);
trackPath.setMap(map);
map.fitBounds(calcBounds(trackPath));
}
function calcBounds(trackPath) {
var b = new google.maps.LatLngBounds();
var gmPath = trackPath.getPath();
var pathLength = gmPath.getLength();
var i = [0, (pathLength / 3).toFixed(0), (pathLength / 3).toFixed(0) * 2];
b.extend(gmPath.getAt(i[0]));
b.extend(gmPath.getAt(i[1]));
b.extend(gmPath.getAt(i[2]));
}
我在某处错过了初始化吗?
谢谢!