簡體   English   中英

文本字段谷歌地圖中的自動定位

[英]Automatic location in textfield Google map

當我寫一些位置時,我試圖自動獲取文本字段內的位置! 但它不起作用,當我寫東西時沒有位置顯示。

這是代碼:

 <!DOCTYPE html>
      <html>
      <head>
       <title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
      <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places">
      type="text/javascript">
      function initialize() {
      var input = document.getElementById('searchTextField');
      var options = {componentRestrictions: {country: 'us'}};            
    new google.maps.places.Autocomplete(input, options); }         
       google.maps.event.addDomListener(window, 'load', initialize);
       </script>
     </head>
      <body>
      <label for="searchTextField">Please insert an address:</label>
      <input id="searchTextField" type="text" size="50">
      </body>
     </html>

嘗試這個:

JS

var input = document.getElementById('searchTextField');
var options = {componentRestrictions: {country: 'us'}};

new google.maps.places.Autocomplete(input, options);

HTML

<label for="searchTextField">Please Insert an address:</label>
<br>
<input id="searchTextField" type="text" size="50">

這是有效的小提琴示例: http : //jsfiddle.net/moinsam/SDPHm/light/我的建議是將 JS 與 HTML 分開。

我試過你的代碼,你的一個結束</script>標簽有問題:

<!DOCTYPE html>
<html>
   <head>
       <title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
       <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places" type="text/javascript"></script><!-- You forgot closing this -->
       <!-- You forgot starting the below script tag -->
       <script>
       function initialize() {
         var input = document.getElementById('searchTextField');
         var options = {componentRestrictions: {country: 'us'}};            
         new google.maps.places.Autocomplete(input, options); 
       }         
       google.maps.event.addDomListener(window, 'load', initialize);
       </script>
  </head>

  <body>
      <label for="searchTextField">Please insert an address:</label>
      <input id="searchTextField" type="text" size="50">
  </body>
</html>

編輯:

如果您想獲取用戶的當前位置(國家和城市),有多種方法可以做到這一點。 您可以使用訪問者的 IP 地址來獲取它:

$ip = getenv('REMOTE_ADDR');
$addr = file_get_contents('http://api.hostip.info/get_json.php?ip='.$ip);
$addr = json_decode($addr, TRUE);
$countryAndCity = $addr["country_name"].", ".$addr["city"];
echo $countryAndCity;

請注意,這可能不准確並且可能被偽造。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM