I have a simple page with GMap and Streetview div's. While an external link sets a new location OK in both div's, trying to do so in the map click handler fails. (I suspect there may be some closure rule I'm missing.) Script is as follows:
var map, panorama;
function do_show ( a, b ) {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: a, lng: b}, zoom: 14
});
map.addListener ( 'click', function(e) {
do_show ( e.latLng.lat().toFixed(6), e.latLng.lng().toFixed(6) );
} ); // end addListener()
panorama = new google.maps.StreetViewPanorama(
document.getElementById('pano'), {
position: {lat: a, lng: b},
pov: { heading: 34, pitch: 10 }
});
map.setStreetView(panorama);
} // end function do_show()
function pre_init () {
panorama = map = null;
do_show( 42.345573, -71.098326 ); // Boston
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&callback=pre_init">
</script>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…