gpt4 book ai didi

angularjs - 如何通过在 angularjs 中使用地理定位 api 获取邮政编码或邮政编码

转载 作者:行者123 更新时间:2023-12-04 22:32:35 26 4
gpt4 key购买 nike

我在 angularjs 中使用地理定位 api。像这个例子:http://jsfiddle.net/LyD4Q/4/

HTML

 <div ng-app>
<div ng-controller="GeoTestCtrl">
<div ng-hide="supportsGeo">
Your browser doesn't support geolocation
</div>
<div ng-show="supportsGeo">
Manual digest when position received: <input type="checkbox" ng-model="manualDigest"/>
<br/>
<button ng-click="doTest1()">
Geo Location Test 1: window.navigator.geolocation
</button>

Need to type something to trigger digest:
<input type="text" ng-model="something"/>
<hr/>
Position: <button ng-click="position=null">clear</button>
<pre ng-bind="position|json"/>
<pre ng-bind="zipcode|json"/>
</div>
</div>
</div>
<hr/>

Javascript
function GeoTestCtrl($scope, $window) {
$scope.supportsGeo = $window.navigator;
$scope.position = null;
$scope.doTest1 = function() {
window.navigator.geolocation.getCurrentPosition(function(position) {
$scope.$apply(function() {
$scope.position = position;
});
}, function(error) {
alert(error);
});
};

}

但我试图通过使用它来获取邮政编码。我尝试使用“position.address.postalCode”,但它对我不起作用。请帮我解决这个问题。

最佳答案

谢谢你的帮助....:)
我解决了。这是代码:

            var lat = position.coords.latitude;
var long = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, long)

geocoder.geocode({'latLng': latlng}, function(results, status) {
$scope.$apply( function () {

if (status == google.maps.GeocoderStatus.OK) {


if (results[0]) {
for(var i=0; i<results[0].address_components.length; i++)
{
var postalCode = results[0].address_components[i].types;
}
}

关于angularjs - 如何通过在 angularjs 中使用地理定位 api 获取邮政编码或邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16727031/

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