简体   繁体   中英

Passing coordinates to HTML page to open Google Street View in new window

I have a separate mapping application that reads the current center of the map and grabs the lat and long and then passes those variable to an HTML page. Here is the code for the HTML page and its javascript. This opens a new window but does not show the street view and does not give an error.

  var lat = getParameterByName('lat');
  var lon = getParameterByName('lon');

  var panorama;
  function initialize() {
    panorama = new google.maps.StreetViewPanorama(
        document.getElementById('street-view'),
        {
          position: {lat: ""+lat+"", lng: ""+lon+""}, //bad code???
          pov: {heading: 165, pitch: 0},
          zoom: 1
        });
  }
</script>
<script async defer
     src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBaSDR10ovsZ0rxygIpheEgrR2YjpbJHbg&callback=initialize">
</script>

Remove the "" from the google.maps.LatLngLiteral and probably be prudent to use parseFloat on those strings).

code snippet:

 var lat = 40.713613; // getParameterByName('lat'); var lon = -74.006757; // getParameterByName('lon'); var panorama; function initialize() { panorama = new google.maps.StreetViewPanorama( document.getElementById('street-view'), { position: { lat: parseFloat(lat), lng: parseFloat(lon) }, pov: { heading: 165, pitch: 0 }, zoom: 1 }); } google.maps.event.addDomListener(window, "load", initialize); 
 html, body, #street-view { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="street-view"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM