0

我试图将谷歌地图放在我解码的折线上。我明白了

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]));
    }

我在某处错过了初始化吗?

谢谢!

4

1 回答 1

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;
}
于 2013-06-11T15:43:25.000 回答