我正在尝试将谷歌地图置于我已解码的折线上。我得到了
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]));
}
我错过了某处的初始化吗?
谢谢!
答案 0 :(得分:0)
calcBounds不返回任何应该起作用的东西:
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]));
return b;
}