2017-04-18 146 views
-1

下面的代碼在jsp頁面中不起作用。什麼可能是錯誤?代碼適用於HTML,但不適用於JSP

<html> 
 
    <head> 
 
     <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
 
     <meta charset="utf-8"> 
 
     <title>Geocoding service</title> 
 
     <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
 
     <script>   
 
     function codeAddress() {   
 
      var address = document.getElementById("address").value;      
 
      var geocoder = new google.maps.Geocoder(); 
 
      geocoder.geocode({ 'address': address}, function(results,status) { 
 
      var location = results[0].geometry.location; 
 
      alert('LAT: ' + location.lat() + ' LANG: ' + location.lng());}); 
 
     } 
 
     google.maps.event.addDomListener(window, 'load', codeAddress);  
 
     </script> 
 
    </head> 
 
    <body> 
 
     <div id="panel">  <input id="address" type="textbox" value="Tembhurkheda, Maharashtra, INDIA">  <input type="button" value="Geocode" onclick="codeAddress()">  </div> 
 
    </body> 
 
</html>

+1

JSP使用Java的,爲什麼標籤PHP? – frz3993

回答

0

檢查瀏覽器console.It拋出以下錯誤

js:35 Google Maps API error: MissingKeyMapError https://developers.google.com/maps/documentation/javascript/error-messages#missing-key-map-error 

你需要得到密鑰,才能使用這個API。而應作爲

<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" 
    type="text/javascript"></script> 

Refer this

相關問題