尝试通过 Angular 控制器使用 Google Geocode API 进行地理编码,值未定义

Trying to geocode with Google Geocode API through Angular controller, values are undefined

本文关键字:编码 未定义 API Geocode Angular 控制器 Google      更新时间:2023-09-26

所以我有这个使用AngularJS的Cordova应用程序。我已经为它构建了几个控制器,到目前为止我没有任何问题,但现在尝试对我们为客户构建的 API 提供的地址进行地理编码,在尝试访问键/值对时,我似乎得到的只是未定义的。

控制台的目的.log这里只是为了确保对象被正确解析并且我可以访问内容(我在调试测试时使用实时重新加载服务器),实际内容从应用程序内的 HTML 模板加载。

这个问题已经有几天了,我无法让它工作,请帮我解决这个问题!

.controller('StayDetailCtrl', function($scope, $http, $stateParams) {
stayId = $stateParams.stayId
$scope.geodata = [];
$http.get('http://api.vllapp.com/staydetail/' + stayId)
    .then(function(result) {
        $scope.staydetails = result.data;
$http.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + $scope.staydetails.address + '&key=AIzaSyBZVOSPh0Z4mv9jljJWzZNSug6upuec7Sg')
    .then(console.log($scope.staydetails.address))
        .then(function(result) {
            $scope.geodata = result.data;
    })
    .then(console.log($scope.geodata.geometry.location.lat))
});

注意:这个位加载在另一个运行良好的控制器上,模板从那里获取所有其他数据来构建自己。

$scope.staydetails = [];

提前感谢!

编辑了这段代码以使其同步,但无济于事:/

不确定我是否理解这个问题,但我认为您可能会发现下面的代码很有帮助。

http://codepen.io/aaronksaunders/pen/ogajKX?editors=101

.controller('MainCtrl', function($scope,$http) {
  var address ="1334 Emerson St, NE Washington DC";
  $scope.geodata = {};
  $scope.queryResults = {};
  $scope.queryError = {};
  $http.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + 
            address + '&key=AIzaSyBZVOSPh0Z4mv9jljJWzZNSug6upuec7Sg')
    .then(function(_results){
       console.log(_results.data);
       $scope.queryResults = _results.data.results;
       $scope.geodata = $scope.queryResults[0].geometry
     }, 
     function error(_error){
        $scope.queryError = _error;
     })
});