谷歌地图放置Address_components不返回街道

时间:2014-07-14 12:15:03

标签: javascript google-maps-api-3

有人可以告诉我为什么我的fillInAddress()函数不起作用? 在console.log中它表示'未捕获的TypeError:无法读取未定义'的属性'0' 我认为我做得对,但因为我的生活无法弄清楚为什么它没有回归'路线'

<script>
            var map;
            var iw = new google.maps.InfoWindow();
            var places;


            function initialize() {

                var markers = [];
                var myOptions = {
                zoom: 1,
                mapTypeId: google.maps.MapTypeId.HYBRID,
                streetViewControl: true
            }
            map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);

            var southWest = new google.maps.LatLng(35.834515,-116.586914);
            var northEast = new google.maps.LatLng(42.947879,-76.311035);
            var bounds = new google.maps.LatLngBounds(southWest,northEast);
            map.fitBounds(bounds);
            var input = (document.getElementById('pac-input'));
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
            var searchBox = new google.maps.places.SearchBox((input));
            google.maps.event.addListener(searchBox, 'places_changed', function() {
                places = searchBox.getPlaces();
                for (var i = 0, marker; marker = markers[i]; i++) {
                    marker.setMap(null);
                }
                markers = [];
                var bounds = new google.maps.LatLngBounds();
                for (var i = 0, place; place = places[i]; i++) {
                    var image = {
                        url: place.icon,
                        size: new google.maps.Size(71, 71),
                        origin: new google.maps.Point(0, 0),
                        anchor: new google.maps.Point(17, 34),
                        scaledSize: new google.maps.Size(25, 25)
                    };

                    var marker = new google.maps.Marker({
                        map: map,
                        icon: image,
                        title: place.name,
                        position: place.geometry.location
                    });
                    markers.push(marker);
                    bounds.extend(place.geometry.location);

                }

                map.fitBounds(bounds);

                fillInAddress();

            });
            google.maps.event.addListener(map, 'bounds_changed', function() {
                var bounds = map.getBounds();
                searchBox.setBounds(bounds);



            });
        }

        function fillInAddress() {

          var components = places.address_components;
          var street = null;
          for (var i = 0, component; component = components[i]; i++) {
            console.log(component);
            if (component.types[0] == 'route') {
              street = component['long_name'];
            }
          }
          alert('Street: ' + street);
        }

1 个答案:

答案 0 :(得分:0)

这个for循环看起来很奇怪....第一次,组件为null,因为你刚刚创建它作为一个没有任何赋值的局部变量。因此,此声明(component = components[i])的while部分将为false。除非那是components[i] = null,否则进一步向下的代码将会出错。

for (var i = 0, component; component = components[i]; i++) {

难道你不想这样做:

for (var i = 0, component; i < components.length; i++) {
    component = components[i]        
    console.log(component);
    if (component.types[0] == 'route') {
       street = component['long_name'];
    }
}