"未捕获的类型错误:无法读取未定义的属性'lat'"



我试图在" Infowdindow"中显示'全景''。但是我会在尝试使用'google.maps.maps.spherical.computeheading'的"未定义的typeerror:无法读取属性"。

您可以检查出什么问题吗?

var myMap;
var myMarkerList=[];
var myBound;
var myStreetView;
var radius;
var clickedMarker;
var m_mark;
var initMap = function(){
    var myStyleType = new google.maps.StyledMapType(myStyles,{name: 'Styled Map'});
    myMap = new google.maps.Map(document.getElementById('googleMap'), {
        center:{lat:40.7413549, lng:-73.9980244},
        zoom:13,
        mapTypeControlOptions:{
            mapTypeIds:['roadmap', 'satellite', 'hybrid', 'terrain',
            'styled_map']
        }
    });
    myMap.mapTypes.set('styled_map', myStyleType);
    myMap.setMapTypeId('styled_map');
    google.maps.event.addListener(myMap, 'click', function(event){
        setMarkerList(event.latLng);
        openInfoWindow(event.latLng);
    });
    myBound = new google.maps.LatLngBounds();
            m_mark = new google.maps.Marker({
            position:{lat: 40.7713024, lng: -73.9632393},
            map:myMap,
            title:'Park Ave Penthouse',
            animation: google.maps.Animation.DROP,
            });
            m_mark.addListener('click', function(){
            openStreetView(this);
            clickedMarker = this;
            });

    myStreetView = new google.maps.StreetViewService();
    radius = 50;
}
function setMarkerList(pos){
    var myMarker = new google.maps.Marker({
            position:pos,
            map:myMap,
            animation: google.maps.Animation.DROP,
    });
    myMarker.addListener('click', function(){
        openStreetView(this);
        clickedMarker = this;
    });
    myMarkerList.push(myMarker);
    setBound(myMarker);
}
function openStreetView(targetMarker){
    myStreetView.getPanoramaByLocation(targetMarker.position, radius, getParanomaView);
}
function getParanomaView(data, status){
    var myInfo = new google.maps.InfoWindow();
    if (status = google.maps.StreetViewStatus.OK) {
        var nearStreetViewLocation = data.location.latlng;
        var heading = google.maps.geometry.spherical.computeHeading(nearStreetViewLocation, clickedMarker.position);
        myInfo.setContent('<div>'+clickedMarker.title+'</div><div id="panorama"></div>');
        var panoramaOptions = {
                    position:nearStreetViewLocation,
                    pov: {
                        heading: heading,
                        pitch: 30
                    }
                };
            var panorama = new google.maps.StreetViewPanorama( document.getElementById('panorama'),panoramaOptions);
    }
    else{
        myInfo.setContent('<div>'+clickedMarker.title+'</div><div>No Street View Found</div>');
    }
    myInfo.open(myMap, targetMarker);
}

我对可能导致的是什么,所以我非常感谢您的帮助。

审查(对其他注释)后,getParanomaView()函数有一些问题。应该是:

function getParanomaView(data, status){
    var myInfo = new google.maps.InfoWindow();
    if (status == google.maps.StreetViewStatus.OK) {
        var nearStreetViewLocation = data.location.latLng;
        var heading = google.maps.geometry.spherical.computeHeading(nearStreetViewLocation, clickedMarker.getPosition());
        myInfo.setContent('<div>'+clickedMarker.title+'</div><div id="panorama"></div>');
        var panoramaOptions = {
            position:nearStreetViewLocation,
            pov: {
                heading: heading,
                pitch: 30
            }
        };
        var panorama = new google.maps.StreetViewPanorama(document.getElementById('panorama'),panoramaOptions);
    } else {
        myInfo.setContent('<div>'+clickedMarker.title+'</div><div>No Street View Found</div>');
    }
    myInfo.open(myMap, targetMarker);
}

上一个答案

线var heading = google.maps.geometry.spherical.computeHeading(nearStreetViewLocation, clickedMarker.position);

应该是

var heading = google.maps.geometry.spherical.computeHeading(nearStreetViewLocation, clickedMarker.getPosition());

否则clickedMarker.position将不确定。

最新更新