Google地图允许人们从自动完成库中获取坐标。我似乎找不到不使用“ controller as”语法的ngMap方法。我想获取坐标,然后仅使用$ scope将其输出到控制台。



var app = angular.module("App", ["ngMap"]);

app.controller("MyCtrl", function ($scope, NgMap) {

$scope.placeChanged = function placeChanged() {
    var place = this.getPlace();
    console.log(place.place.geometry.location)
  };

});

<html ng-app="App">

<script src="https://maps.google.com/maps/api/js?libraries=places"></script>
<script src="https://code.angularjs.org/1.3.15/angular.js"></script>
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>

<div ng-controller="MyCtrl">
<input type="text" name="address" places-auto-complete ng-model="address" on-place-change="placeChanged()">
</div>

</html>

最佳答案

指令places-auto-complete在NgMap中具有on-place-changed

的HTML:

<div ng-controller="MyCtrl">
  <input places-auto-complete ng-model='address' on-place-changed="getCoords()" />
</div>


脚本:当地点更改时ng-model会更新,并且

var app = angular.module("App", ["ngMap"]);
app.controller("MyCtrl", function($scope, NgMap) {
  $scope.address = '';
  $scope.getCoords = function() {
    NgMap.getGeoLocation($scope.address).then(function(latlng) {
      console.log(latlng.lat());
      console.log(latlng.lng());
    });
  };
})

07-24 09:49