2016-01-06 76 views
0

我正在尝试在下面的脚本中获取区域字段的内容,以便我可以在输入地址时自动显示区域。 我会在js脚本中创建一个名为getRegion()的新函数吗?使用jQuery地址选择器获取区域名称

我该怎么做?

<!DOCTYPE HTML> 
 
<html> 
 
    <head> 
 
    <meta charset="utf-8"/> 
 

 
    <title>placepicker</title> 
 

 
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
 

 
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
 
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> 
 
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"></link> 
 
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css" rel="stylesheet"></link> 
 

 
    <script type="text/javascript" 
 
     src="https://maps.googleapis.com/maps/api/js?sensor=true&libraries=places"> 
 
    </script> 
 

 
    <script src="http://www.jqueryscript.net/demo/jQuery-Location-Autocomplete-with-Google-Maps-Places-Library-Placepicker/src/js/jquery.placepicker.js"></script> 
 

 
    <script> 
 

 
     $(document).ready(function() { 
 

 
     // Basic usage 
 
     $(".placepicker").placepicker(); 
 

 
     // Advanced usage 
 
     $("#advanced-placepicker").each(function() { 
 
      var target = this; 
 
      var $collapse = $(this).parents('.form-group').next('.collapse'); 
 
      var $map = $collapse.find('.another-map-class'); 
 

 
      var placepicker = $(this).placepicker({ 
 
      map: $map.get(0), 
 
      placeChanged: function(place) { 
 
       console.log("place changed: ", place.formatted_address, this.getLocation()); 
 
      } 
 
      }).data('placepicker'); 
 
     }); 
 

 
     }); // END document.ready 
 

 
    </script> 
 

 
    <style> 
 

 
     .placepicker-map { 
 
     width: 100%; 
 
     height: 300px; 
 
     } 
 

 
     .another-map-class { 
 
     width: 100%; 
 
     height: 300px; 
 
     } 
 

 
     .pac-container { 
 
     border-radius: 5px; 
 
     } 
 

 
    </style> 
 

 
    </head> 
 

 
    <body> 
 

 

 
    <div class="container"> 
 

 
     <h1>Basic usage</h1> 
 
     <div class="row" data-example> 
 
     <div class="col-md-6"> 
 
      <div class="form-group"> 
 
      <input class="placepicker form-control" placeholder="Enter a location"/> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 

 

 
    </body> 
 

 
</html>

回答

0

确定。在函数initAutoComplete() 我可以显示place.address_components [2] .long_name

这是一个区域的名称:

我已经得到了良好的尖端,第一。

+0

问题解决@glochen G Uyirr –