开发者问题收集

谷歌地图:无法读取未定义的属性“几何”

2016-04-05
13539

当我尝试从 JSON 源在地图上显示标记时,我收到错误。我获取了很多地址,然后尝试获取地址的经度和纬度,然后将其放置在地图上,但是,我收到错误:无法在控制台中读取未定义的属性“几何”。

代码:

$.getJSON(apiUrl, function( data ) {
            var delay = 0,
                p,
                contentString,
                iconBase;

            _.each(data, function (value, key) { 

                var link = value.LinkUrl,
                    address = value.Address,
                    content = "<div class='info-address'>" +  address + "</div>
                               <div class='info-message'>" + value.OpeningHoursMessage + "</div>
                               <div class='info-link'><a href='"+ value.LinkUrl +"'>Læs mere</a></div>";
                console.log("Adresse: " + address);
                $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+address+', Danmark', null, function (data) {


                        p = data.results[0].geometry.location,
                        contentString = content,
                        iconBase = '/static/dist/img/';

                        //console.log("Værdi" + p);

                        var latlng = new google.maps.LatLng(p.lat, p.lng);
                        var marker = new google.maps.Marker({
                            position: latlng,
                            map: map,
                            animation: google.maps.Animation.DROP,
                            icon: iconBase + 'maps-marker.png',
                            url: link
                        });

                        //If single lookup, then center to position
                        if(DOM.dataAddress.length) {
                            var pos = {lat: lat, lng: lng};
                            map.setCenter(pos);
                        }

                        bindInfoWindow(marker, map, infowindow, contentString);

                        DOM.markersArray.push(marker);


                });
            });
        });
1个回答

根据文档,响应似乎应该是一个数组 。也许您请求的某个地址没有任何结果?您应该在向地图 API 发出 JSON 请求后进行检查。

$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+address+', Danmark', null, function (data) {
    if (data.results.length > 0) {
        // set `p` and other code
    }
});
Shaun
2016-04-05