“未捕获的TypeError:无法读取未定义的属性'lat'”

时间:2017-11-21 14:46:37

标签: javascript google-maps

我试图在'infowindow'中显示'panoramaview'。 但是当我尝试使用'google.maps.geometry.spherical.computeHeading'时,我得到'Uncaught TypeError:无法读取'undefined'属性'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 个答案:

答案 0 :(得分:0)

经过审核(其他评论),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将是未定义的。