title: service is returning undefined in the array in angularjs
body: <p>I am facing trouble with my angularjs script.</p>
  
  <p>Background: I am trying to determine the location of the user, I have written my business logic in my service from where I am returning the location of the user.</p>
  
  <p>Problem : The result I am getting in my console is as below :</p>
  
  <blockquote>
    <p>[undefined] landingPage.js:11 undefined landingPage.js:11 undefined</p>
  </blockquote>
  
  <pre><code>var app = angular.module("PublicEvents", ["geolocation"]);
  
  app.controller("iterator", ["$scope", "$http", "locationService1", function($scope, $http, locationService1){
      $scope.targetCity = [];
      $scope.targetCity.push(locationService1.location());
  
      console.log($scope.targetCity);
      $scope.$watch(function () { return locationService1.cityNameArray; },
              function (value) {
          $scope.targetCity = value;
          console.log($scope.targerCity);
      }
      );
  
  }]);
  
  
  
  app.service("locationService1",['$http','$window', function( $http, $window){
      var access = this;
      this.location = function(){
          $window.navigator.geolocation.getCurrentPosition(function(position) {
              access.lat = position.coords.latitude;
              access.long = position.coords.longitude;
  
              access.locationData = [];
              access.cityNameArray = [];
  
              /*var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=18.9750,72.8258&amp;sensor=true";*/
              var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+access.lat+","+access.long+"&amp;sensor=true";
  
              //AJAX CALL TO GET THE LOCATION
              $http.get(url).then(function(response) {
                  access.locationData = response.data; 
                  if(access.locationData.status == "OK" || access.locationData.status==200 ) {
                      angular.forEach(access.locationData.results, function(value, key){
                          var len = value.address_components.length;
                          for(var i = 0; i&lt; len; i++){
                              if(value.address_components[i].types[0] =="locality" || value.address_components[i].types[0] =="sublocality_level_1"){
                                  access.cityNameArray.push(value.address_components[i].long_name);
                              }
                          }
                      }); 
                  };
              }); 
              return access.cityNameArray;
          });
      };
  }]);
  </code></pre>