Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Zip code or postal code by using geolocation api in angularjs

I am using geolocation api in angularjs .like this example: 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);
    });
};

}

But I am trying to get zip code by using this. I tried with 'position.address.postalCode' but it is not working for me. Please help me about this.

like image 947
Mou Avatar asked May 24 '13 02:05

Mou


2 Answers

Thanks for you help....:) I solved it. here is the code:

            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;
                             }
                         }
like image 58
Mou Avatar answered Dec 19 '22 14:12

Mou


According to the MDN documentation on window.navigator.geolocation, getCurrentPosition() (which is not an AngularJS specific API) doesn't return a zip code. Additionally, the JSFiddle example you linked shows the data available via this API:

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

If you want to change the latitude and longitude coordinates into a zip code, you'll need to use some other API or service. A quick Google search turned up a few promising looking results.

like image 35
Michelle Tilley Avatar answered Dec 19 '22 15:12

Michelle Tilley