gpt4 book ai didi

javascript - 从 ngMap 自动完成获取坐标

转载 作者:行者123 更新时间:2023-11-28 04:46:27 26 4
gpt4 key购买 nike

Google map 允许人们从自动完成库中获取坐标。我似乎找不到使用 ngMap 来做到这一点的方法不涉及“controller as”语法。我想获取坐标并仅使用 $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());
});
};
})

关于javascript - 从 ngMap 自动完成获取坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43330345/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com