开发者问题收集

“未捕获的类型错误:无法读取未定义的属性‘lat’”

2017-11-21
5960

我试图在“信息窗口”中显示“全景视图”。 但是当我尝试使用“google.maps.geometry.spherical.computeHeading”时,我得到了“未捕获类型错误:无法读取未定义的属性‘lat’”。

你能检查出哪里出了问题吗?

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);
}

我已经不知道是什么原因导致了这个问题,所以我非常感谢你的帮助。

1个回答

在查看(其他评论)后,发现 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 headline = google.maps.geometry.spherical.computeHeading(nearStreetViewLocation, clickedMarker.position);

应该是

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

否则 clickedMarker.position 将未定义。

Phillip Thomas
2017-11-21