谷歌地图api中的航点显示错误

时间:2016-04-14 19:38:50

标签: jquery api maps

我正在使用谷歌地图捕捉到道路api而我正在调用航点时显示错误 InvalidValueError:在属性航点中:在索引0处:未知属性lat
TypeError:a为空

如果没有使用航点,我会得到路径,但不包括航路点。

任何帮助都会有所帮助。

function getPoly() {
        for (var i = 0; i < polyValuesChecked.length; i++) {
            var setLat = parseFloat(polyValuesChecked[i].lat);
            var setLng = parseFloat(polyValuesChecked[i].lng);
        }
        var polys = new google.maps.Polyline({
            map: map,
            strokeColor: "red",
            zoom: 4
        });

        // BEGIN: Snap to road
        var service = new google.maps.DirectionsService(), polys, snap_path = [];

        myCoord = [
            new google.maps.LatLng(12.961608, 77.562039),
            new google.maps.LatLng(12.961574, 77.560976),
            new google.maps.LatLng(12.961404, 77.559956),
            new google.maps.LatLng(12.966734, 77.546926),
            new google.maps.LatLng(12.972937, 77.538833),
            new google.maps.LatLng(12.985802, 77.525771),
            new google.maps.LatLng(12.985565, 77.525554),
            new google.maps.LatLng(12.985375, 77.525033),
            new google.maps.LatLng(12.985301, 77.524740),
            new google.maps.LatLng(12.982437, 77.530903),
            new google.maps.LatLng(12.985181, 77.524365),
            new google.maps.LatLng(12.985057, 77.524059)
        ];

        var start = myCoord[0];
        var end = myCoord[myCoord.length - 1];
        myCoord.shift();
        myCoord.pop();

        service.route({
            origin: start,
            destination: end,
            waypoints: myCoord,
            optimizeWaypoints: false,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        }, function (result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                console.log("success");
                snap_path = snap_path.concat(result.routes[0].overview_path);
                polys.setPath(snap_path);
            }
        });

        for (var i = 0; i < myCoord.length; i++) {
            var marker = new google.maps.Marker({
                position: myCoord[i],
                map: map
            });
        }
    }

function getPoly() { for (var i = 0; i < polyValuesChecked.length; i++) { var setLat = parseFloat(polyValuesChecked[i].lat); var setLng = parseFloat(polyValuesChecked[i].lng); } var polys = new google.maps.Polyline({ map: map, strokeColor: "red", zoom: 4 }); // BEGIN: Snap to road var service = new google.maps.DirectionsService(), polys, snap_path = []; myCoord = [ new google.maps.LatLng(12.961608, 77.562039), new google.maps.LatLng(12.961574, 77.560976), new google.maps.LatLng(12.961404, 77.559956), new google.maps.LatLng(12.966734, 77.546926), new google.maps.LatLng(12.972937, 77.538833), new google.maps.LatLng(12.985802, 77.525771), new google.maps.LatLng(12.985565, 77.525554), new google.maps.LatLng(12.985375, 77.525033), new google.maps.LatLng(12.985301, 77.524740), new google.maps.LatLng(12.982437, 77.530903), new google.maps.LatLng(12.985181, 77.524365), new google.maps.LatLng(12.985057, 77.524059) ]; var start = myCoord[0]; var end = myCoord[myCoord.length - 1]; myCoord.shift(); myCoord.pop(); service.route({ origin: start, destination: end, waypoints: myCoord, optimizeWaypoints: false, travelMode: google.maps.DirectionsTravelMode.DRIVING }, function (result, status) { if (status == google.maps.DirectionsStatus.OK) { console.log("success"); snap_path = snap_path.concat(result.routes[0].overview_path); polys.setPath(snap_path); } }); for (var i = 0; i < myCoord.length; i++) { var marker = new google.maps.Marker({ position: myCoord[i], map: map }); } }

1 个答案:

答案 0 :(得分:0)

查看文档: https://developers.google.com/maps/documentation/javascript/directions#Waypoints

你的waypoints数组应该是一个对象数组,如下所示:

myCoords = [
    { location : new google.maps.LatLng(12.961608, 77.562039) },
    etc...
]