angularjs - 使用 angularjs 的地理编码服务

标签 angularjs geocoding

我是 angular js 的新手。如何实现地理编码服务?具体来说,当我填写完整地址时,如何自动填充其他字段( postal code,city,country,lat and lng )?

我想实现类似 this page 的东西在 Angular 。
请帮帮我。

我有代码来填充完整地址:

app.directive('googlePlaces', function(){
    return {
        restrict:'E',
        replace:true,
        // transclude:true,
        scope: {location:'='},
        template: '<input type="text" id="fulladdress" name="fulladdress" ng-model="enterprise.fulladdress" class="form-control text-field" placeholder="">',
        link: function($scope, elm, attrs){
            var autocomplete = new google.maps.places.Autocomplete($("#country")[0], {});
            google.maps.event.addListener(autocomplete, 'place_changed', function() {
                var place = autocomplete.getPlace();
                $scope.location = place.geometry.location.lat() + ',' + place.geometry.location.lng();
                $scope.$apply();
            });
        }
    }
});

和 HTML:
<div class="form-group enterprise-form">
     <label>Full Address</label>
     <google-places location=location></google-places>
</div>

我想再填充两个或三个字段 lat,lng, postal code .我可以扩展我的指令来实现这一点,如果可以,如何实现?

最佳答案

您只需关注 example .

自动完成监听器 'place_change'发生在 angular 的 $digest 循环之外,所以你应该调用 fillInAddress使用 $evalAsync否则在下一个摘要之前您不会看到您的表单更改。

纬度、经度存储在 place 上对象在 geometry.location .

var app = angular.module('app', []);

app.controller('myController', function($scope) {
  var components = {
    street_number: 'short_name',
    route: 'long_name',
    locality: 'long_name',
    administrative_area_level_1: 'short_name',
    country: 'long_name',
    postal_code: 'short_name'
  };
  var autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'), {
    types: ['geocode']
  });
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    $scope.$evalAsync(fillInAddress);
  });
  
  $scope.formData = {};
  $scope.ll = {};

  function fillInAddress() {
    var place = autocomplete.getPlace();
    Object.keys(components).forEach(function(component) {
      $scope.formData[component] = '';
      document.getElementById(component).disabled = false;
    });
    
    place.address_components.forEach(function(component) {
      var addressType = component.types[0];
      if (components[addressType]) {
        $scope.formData[addressType] = component[components[addressType]];
      }
    });
    $scope.ll = {
      lat: place.geometry.location.G,
      lon: place.geometry.location.K
    };
  }
});
#locationField,
#controls {
  position: relative;
  width: 480px;
}
#autocomplete {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 99%;
}
.label {
  text-align: right;
  font-weight: bold;
  width: 100px;
  color: #303030;
}
#address {
  border: 1px solid #000090;
  background-color: #f0f0ff;
  width: 480px;
  padding-right: 2px;
}
#address td {
  font-size: 10pt;
}
.field {
  width: 99%;
}
.slimField {
  width: 80px;
}
.wideField {
  width: 200px;
}
#locationField {
  height: 20px;
  margin-bottom: 2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>

<div ng-app='app' ng-controller='myController'>
  <div id="locationField">
    <input id="autocomplete" placeholder="Enter your address" type="text">
  </div>
  <table id="address">
    <tr>
      <td class="label">Street address</td>
      <td class="slimField">
        <input ng-model="formData.street_number" class="field" id="street_number" disabled="true" type="text">
      </td>
      <td class="wideField" colspan="2">
        <input ng-model="formData.route" class="field" id="route" disabled="true">
      </td>
    </tr>
    <tr>
      <td class="label">City</td>
      <td class="wideField" colspan="3">
        <input ng-model="formData.locality" class="field" id="locality" disabled="true">
      </td>
    </tr>
    <tr>
      <td class="label">State</td>
      <td class="slimField">
        <input ng-model="formData.administrative_area_level_1" class="field" id="administrative_area_level_1" disabled="true">
      </td>
      <td class="label">Zip code</td>
      <td class="wideField">
        <input ng-model="formData.postal_code" class="field" id="postal_code" disabled="true">
      </td>
    </tr>
    <tr>
      <td class="label">Country</td>
      <td class="wideField" colspan="3">
        <input ng-model="formData.country" class="field" id="country" disabled="true">
      </td>
    </tr>
  </table>
  <pre>{{ formData | json }}</pre>
  <pre>{{ ll | json }}</pre>
</div>

关于angularjs - 使用 angularjs 的地理编码服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31783217/

相关文章:

javascript - 如何全局设置 Angular 矩时区?

ip-address - 对 IP 地址进行地理编码?

iphone - 是否可以通过他在 ios 中的电话号码跟踪用户位置?

javascript - 谷歌地理编码,异步Javascript,不起作用

ruby-on-rails - 解析谷歌地理编码

javascript - MEAN js 需要授权才能列出项目

javascript - AngularJS Http 发布方法不起作用

javascript - 如何在 angularjs 中的每个特定计数后注入(inject)数据

javascript - url 哈希更改成功 - 在 Angular 指令中

ruby - Geokit LatLng 两点之间的中点