尝试通过 Angular 控制器使用 Google Geocode API 进行地理编码,值未定义
Trying to geocode with Google Geocode API through Angular controller, values are undefined
所以我有这个使用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;
})
});
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- 尝试通过 Angular 控制器使用 Google Geocode API 进行地理编码,值未定义
- 未定义编码 URI 组件
- 在PHP编码一些未定义的变量时出错
- jquery显示php-json编码数组的值时出现未定义错误
- 如何在Angular中发送未定义的对象关键帧而不进行硬编码
- 未捕获的类型错误:无法读取属性'地理编码'的未定义
- 从JSON编码中获得控制台中未定义的对象
- JSON数组从PHP编码在主JS文件中未定义
- JSON 编码的 PHP 数组在 JavaScript 中返回未定义
- 未定义地理编码中的变量,如何正确调用它们