推送到折线时,Array.push未设置索引

时间:2018-12-15 00:09:19

标签: javascript arrays google-maps

您好第一次提问者,长期读者;

我要疯了atm。我正在开发包裹跟踪应用程序。单包交易过程中多次交易。因此,我试图设置一个名为packagePath的数组,它只是包将停止的位置的经纬度。

但是,当我push()未设置到数组时,似乎正在设置索引,而Polyline则什么也不做。我尝试在packagePath中手动设置一些示例值,并且数组可以正确构建。还奇怪的是,我通过控制台记录了阵列,一切看起来都很好。手动设置阵列和推入之间的唯一区别是,它向我显示了手动设置时控制台中的计数。

这是我的代码:

// We're using geocoder and infowindows and polylines
        var geocoder = new google.maps.Geocoder();
        var infowindow = new google.maps.InfoWindow;
        var packagePath = [];

        // Construct markers through geocode
        var markers = locations.map(function(location, i){

            // all location data gets passed through this so i can geocode on the fly.
            geocoder.geocode( { 'address': location.addr}, function(results, status) {
                if (status === 'OK') {
                    if (results[0]) {

                        var pos = results[0].geometry.location;

                        // Construct the markers
                        var marker = new google.maps.Marker({
                            map: map,
                            position: pos,
                            icon: location.icon
                        });

                        // Construct the infowindows
                        let content = '<strong>'+location.locTitle+'</strong>';

                        // Open them on click
                        google.maps.event.addListener(marker,'click',
                            (function(marker,content,infowindow){ 
                                return function() {
                                    infowindow.setContent(content);
                                    infowindow.open(map,marker);
                                };
                            })(marker,content,infowindow)
                        );

                        // push to packagePath for polyline
                        var latlng = {
                                lat: pos.lat(),
                                lng: pos.lng()
                            };

                        packagePath.push(latlng);


                        // Center the map on the current location
                        if(location.locTitle === 'Current Location') {
                            map.setCenter(pos);
                        }


                    } else {
                        window.alert('No results found');
                    }
                } else {
                    window.alert('Geocoder failed due to: ' + status);
                }

            })
        });

        console.log(packagePath);

        var polyline = new google.maps.Polyline({
            path: packagePath,
            geodesic: true,
            strokeColor: '#444444',
            strokeOpacity: 1.0,
            strokeWeight: 2
        });

        polyline.setMap(map);

如上所述,控制台显示手动设置数组时的计数:

var packagePath = [
            {lat: -38.416097, lng: -63.616671999999994},
            {lat: 25.7616798, lng: -80.19179020000001},
            {lat: 29.7604267, lng: -95.3698028},
            {lat: 33.5624527, lng: -101.8490617},
            {lat: 33.5773742, lng: -101.88202109999997},
            {lat: 30.1265635, lng: -95.42688989999999}
        ];

Console output for manual array Console output for push array

0 个答案:

没有答案