【问题标题】:How to get Zip code or postal code by using geolocation api in angularjs如何在angularjs中使用地理定位api获取邮政编码或邮政编码
【发布时间】:2013-05-24 02:56:46
【问题描述】:

我在 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”,但它对我不起作用。请帮我解决这个问题。

【问题讨论】:

    标签: angularjs geolocation zipcode street-address


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      根据MDN documentation on window.navigator.geolocation,getCurrentPosition()(不是 AngularJS 特定的 API)不返回邮政编码。此外,您链接的 JSFiddle 示例显示可通过此 API 获得的数据:

      {
        "timestamp": 1369367289899,
        "coords": {
          "speed": null,
          "heading": null,
          "altitudeAccuracy": null,
          "accuracy": 22000,
          "altitude": null,
          "longitude": -125.419416,
          "latitude": 35.774929
        }
      }
      

      如果要将经纬度坐标更改为邮政编码,则需要使用其他 API 或服务。 quick Google search 出现了一些看起来很有希望的结果。

      【讨论】:

        猜你喜欢
        • 2013-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多