使用angularjs进行地理编码服务

时间:2015-08-03 08:55:34

标签: angularjs geocoding

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

我想在角度上实现类似this page的东西 请帮帮我。

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

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。我是否可以扩展我的指令来实现这一目标,如果是这样的话?

1 个答案:

答案 0 :(得分:3)

您可以关注example

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

纬度,经度存储在place的{​​{1}}对象中。

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;
}