繁体   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