我试图使 StreetViewPanorama 具有某个位置,然后检查:如果它有全景返回 true 否则返回 false (如果需要,它可能不是 StreetViewPanorama,我只需要保存检查位置的坐标)
问题是使用
console.log()
检查 .getStatus() 返回什么,我得到 undefined
没有任何异常等等。我看到很多程序员在使用其他类时遇到这个问题,但这没有帮助
我的代码从 Google 代码示例中被盗:
index.html
<html>
<head>
<title>Street View split-map-panes</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<link rel="stylesheet" type="text/css" href="C:/Users/pasha/Desktop/geog/style.css"/>
<script src="C:/Users/pasha/Desktop/geog/index.js"></script>
</head>
<body>
<div id="map"></div>
<div id="pano"></div>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initialize&v=weekly"
defer
></script>
</body>
</html>
index.js
function initialize() {
const fenway = { lat: 42.345573, lng: -71.098326 };
const map = new google.maps.Map(document.getElementById("map"), {
center: fenway,
zoom: 14,
});
const panorama = new google.maps.StreetViewPanorama(
document.getElementById("pano"),
{
position: fenway,
pov: {
heading: 34,
pitch: 10,
},
},
);
console.log(panorama.getStatus()); // undefined
map.setStreetView(panorama);
}
window.initialize = initialize;
.getStatus() 实际上必须返回
StreetViewStatus.OK | StreetViewStatus.UNKNOWN_ERROR | StreetViewStatus.ZERO_RESULTS
。我还尝试了 .getStatus().code
甚至 .getStatus().getCode()
但出现了错误。有谁知道怎么了?
在调用
status_changed
之前,您需要监听 getStatus
事件。
google.maps.event.addListener(panorama, 'status_changed', function() {
console.log(panorama.getStatus());
})
代码片段:
function initialize() {
const fenway = { lat: 42.345573, lng: -71.098326 };
const map = new google.maps.Map(document.getElementById("map"), {
center: fenway,
zoom: 14,
});
const panorama = new google.maps.StreetViewPanorama(
document.getElementById("pano"),
{
position: fenway,
pov: {
heading: 34,
pitch: 10,
},
},
);
google.maps.event.addListener(panorama, 'status_changed', function() {
console.log(panorama.getStatus());
document.getElementById("info").innerHTML = "panorama.getStatus() returns:"+ panorama.getStatus();
})
map.setStreetView(panorama);
}
window.initialize = initialize;
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map,
#pano {
float: left;
height: 90%;
width: 50%;
}
<!doctype html>
<html>
<head>
<title>Street View split-map-panes</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="info"></div>
<div id="map"></div>
<div id="pano"></div>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize&v=weekly"
defer
></script>
</body>
</html>