2017-06-30 61 views
2

我正在使用Google Maps API,使用JavaScript/HTML。我試圖添加簡單的按鈕。如果用戶按下這些按鈕,它會在地圖上左轉,右轉等。但是,我的函數changeHeading被調用,但在調用之後不會執行。問題是什麼?Google Maps API - 添加簡單的按鈕以向左轉,向右轉等

<head> 
     <meta charset="utf-8"> 
     <title>Street View Add Third Panel</title> 
     <style> 
      html, body { 
       height: 100%; 
       margin: 0; 
       padding: 0; 
      } 
      #map { 
       float: left; 
       height: 50%; 
       width: 50%; 
      } 
      #pano { 
       width: 100%; 
       height: 50%; 
      } 
      #floating-panel { 
       float: right; 
       width: 50%; 
       height: 50%; 
      } 
     </style> 
    </head> 
    <body> 
     <div id="map"></div> 
     <div id="floating-panel"> 
     <table> 
      <tr> 
       <td><b>Position</b></td><td id="position-cell">&nbsp;</td> 
      </tr> 
      <tr> 
       <td><b>POV Heading</b></td><td id="heading-cell">270</td> 
      </tr> 
      <tr> 
       <td><b>POV Pitch</b></td><td id="pitch-cell">0.0</td> 
      </tr> 
      <tr> 
       <td><b>Pano ID</b></td><td id="pano-cell">&nbsp;</td> 
      </tr> 
      <tr> 
       <input type="button" value="Turn Left" onclick="changeHeading(-5);"> 
      </tr> 
      <tr> 
       <input type="button" value="Turn Right" onclick="changeHeading(-5);"> 
      </tr> 
      <tr> 
       <input type="button" value="Go Forward" onclick="changeHeading(-5);"> 
      </tr> 
      <tr> 
       <input type="button" value="Go Backward" onclick="changeHeading(-5);"> 
      </tr> 
      <table id="links_table"></table> 
     </table></div> 



     <div id="pano"></div> 
    <script> 

    function initialize() { 
     var fenway = {lat: 42.345573, lng: -71.098326}; 
     var map = new google.maps.Map(document.getElementById('map'), { 
      center: fenway, 
      zoom: 14 
     }); 
     var panorama = new google.maps.StreetViewPanorama(
      document.getElementById('pano'), { 
       position: fenway, 
       pov: { 
        heading: 34, 
        pitch: 10 
       } 
      }); 
     map.setStreetView(panorama); 

     panorama.addListener('pano_changed', function() { 
      var panoCell = document.getElementById('pano-cell'); 
      panoCell.innerHTML = panorama.getPano(); 
     }); 
     panorama.addListener('links_changed', function() { 
      var linksTable = document.getElementById('links_table'); 
      while (linksTable.hasChildNodes()) { 
       linksTable.removeChild(linksTable.lastChild); 
      } 
      var links = panorama.getLinks(); 
      for (var i in links) { 
       var row = document.createElement('tr'); 
       linksTable.appendChild(row); 
       var labelCell = document.createElement('td'); 
       labelCell.innerHTML = '<b>Link: ' + i + '</b>'; 
       var valueCell = document.createElement('td'); 
       valueCell.innerHTML = links[i].description; 
       linksTable.appendChild(labelCell); 
       linksTable.appendChild(valueCell); 
      } 
     }); 
     panorama.addListener('position_changed', function() { 
      var positionCell = document.getElementById('position-cell'); 
      positionCell.firstChild.nodeValue = panorama.getPosition() + ''; 
     }); 
     panorama.addListener('pov_changed', function() { 
      var headingCell = document.getElementById('heading-cell'); 
      var pitchCell = document.getElementById('pitch-cell'); 
      headingCell.firstChild.nodeValue = panorama.getPov().heading + ''; 
      pitchCell.firstChild.nodeValue = panorama.getPov().pitch + ''; 
     }); 


    } 

    function changeHeading(delta) { 
     heading = panorama.getPov().heading; 
     panorama.setPov({ heading: heading + delta }); 
    } 

回答

0

Panorama是一個局部變量,不可用於changeHeading()函數。 做兩件事:

  1. 定義var panorama = null;作爲全局變量的函數之外的
  2. 從刪除VARVAR全景=新google.maps.StreetViewPanorama(...」,所以它不會創建一個新的局部變量,並使用全球性的,而不是
0

兩個問題:

  1. 全景是一個局部變量,所以是不是在按鈕的onclick功能(運行在全球範圍內)進行訪問。您可以修復,通過在,它是在限定的範圍使用google.maps.event.addDomListener功能。

  2. 當你設置你需要用POV對象(否則你將得到的JavaScript錯誤InvalidValueError: setPov: in property pitch: not a number

(內initMap)設置的POV:

google.maps.event.addDomListener(document.getElementById("left"),'click', function() {changeHeading(-5)}); 
google.maps.event.addDomListener(document.getElementById("right"),'click', function() {changeHeading(5)}); 

function changeHeading(delta) { 
    var heading = panorama.getPov().heading; 
    var POV = panorama.getPov(); 
    POV.heading = heading + delta; 
    panorama.setPov(POV); 
} 

proof of concept fiddle

code code snippet:

function initialize() { 
 
    var fenway = { 
 
    lat: 42.345573, 
 
    lng: -71.098326 
 
    }; 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    center: fenway, 
 
    zoom: 14 
 
    }); 
 
    var panorama = new google.maps.StreetViewPanorama(
 
    document.getElementById('pano'), { 
 
     position: fenway, 
 
     pov: { 
 
     heading: 34, 
 
     pitch: 10 
 
     } 
 
    }); 
 
    map.setStreetView(panorama); 
 

 
    panorama.addListener('pano_changed', function() { 
 
    var panoCell = document.getElementById('pano-cell'); 
 
    panoCell.innerHTML = panorama.getPano(); 
 
    }); 
 
    panorama.addListener('links_changed', function() { 
 
    var linksTable = document.getElementById('links_table'); 
 
    while (linksTable.hasChildNodes()) { 
 
     linksTable.removeChild(linksTable.lastChild); 
 
    } 
 
    var links = panorama.getLinks(); 
 
    for (var i in links) { 
 
     var row = document.createElement('tr'); 
 
     linksTable.appendChild(row); 
 
     var labelCell = document.createElement('td'); 
 
     labelCell.innerHTML = '<b>Link: ' + i + '</b>'; 
 
     var valueCell = document.createElement('td'); 
 
     valueCell.innerHTML = links[i].description; 
 
     linksTable.appendChild(labelCell); 
 
     linksTable.appendChild(valueCell); 
 
    } 
 
    }); 
 
    panorama.addListener('position_changed', function() { 
 
    var positionCell = document.getElementById('position-cell'); 
 
    positionCell.firstChild.nodeValue = panorama.getPosition() + ''; 
 
    }); 
 
    panorama.addListener('pov_changed', function() { 
 
    var headingCell = document.getElementById('heading-cell'); 
 
    var pitchCell = document.getElementById('pitch-cell'); 
 
    headingCell.firstChild.nodeValue = panorama.getPov().heading + ''; 
 
    pitchCell.firstChild.nodeValue = panorama.getPov().pitch + ''; 
 
    }); 
 
    google.maps.event.addDomListener(document.getElementById("left"), 'click', function() { 
 
    changeHeading(-5) 
 
    }); 
 
    google.maps.event.addDomListener(document.getElementById("right"), 'click', function() { 
 
    changeHeading(5) 
 
    }); 
 

 
    function changeHeading(delta) { 
 
    var heading = panorama.getPov().heading; 
 
    var POV = panorama.getPov(); 
 
    POV.heading = heading + delta; 
 
    panorama.setPov(POV); 
 
    } 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize);
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
#map { 
 
    float: left; 
 
    height: 50%; 
 
    width: 50%; 
 
} 
 

 
#pano { 
 
    width: 100%; 
 
    height: 50%; 
 
} 
 

 
#floating-panel { 
 
    float: right; 
 
    width: 50%; 
 
    height: 50%; 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script> 
 
<div id="map"></div> 
 
<div id="floating-panel"> 
 
    <table> 
 
    <tr> 
 
     <td><b>Position</b></td> 
 
     <td id="position-cell">&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td><b>POV Heading</b></td> 
 
     <td id="heading-cell">270</td> 
 
    </tr> 
 
    <tr> 
 
     <td><b>POV Pitch</b></td> 
 
     <td id="pitch-cell">0.0</td> 
 
    </tr> 
 
    <tr> 
 
     <td><b>Pano ID</b></td> 
 
     <td id="pano-cell">&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <input type="button" id="left" value="Turn Left" onclick="changeHeading(-5);"> 
 
    </tr> 
 
    <tr> 
 
     <input type="button" id="right" value="Turn Right" onclick="changeHeading(5);"> 
 
    </tr> 
 
    <table id="links_table"></table> 
 
    </table> 
 
</div> 
 

 

 

 
<div id="pano"></div>

相關問題