1

我想创建一个工厂,使用 $resource 从谷歌的距离 api ( https://developers.google.com/maps/documentation/distancematrix/ ) 检索距离信息。

我已经剥离了参数以尝试让简单的服务正常工作..

这是我当前的代码:

VenuesAppServices.factory('VenueDistance', ['$resource',
function($resource){
    return $resource('http://maps.googleapis.com/maps/api/distancematrix/json', {}, {
        Distance: {method:'GET'}
    });
}]);

并称之为:

VenueDistance.Distance(function(results){
    alert(JSON.stringify(results));
},function(error){
    alert(JSON.stringify(error));
});

我希望返回以下内容:

{
"destination_addresses" : [],
"error_message" : "The 'sensor' parameter specified in the request must be set to either 'true' or 'false'.",
"origin_addresses" : [],
"rows" : [],
"status" : "REQUEST_DENIED"
}

但是我得到了 404 响应。

{"data":"","status":404,"config":{"transformRequest":[null],"transformResponse":[null],"method":"GET","url":"http://maps.googleapis.com/maps/api/distancematrix/json","headers":{"Accept":"application/json, text/plain, */*"}}}

如果您将 google api 换成类似 ( http://api.geonames.org/postalCodeLookupJSON )。然后,您会从该站点获得预期的响应。

谁能告诉我为什么 google url 没有返回您从浏览器获得的结果?

4

2 回答 2

1

我认为您不会发送诸如起点和终点之类的参数..

您需要通过 $resource 或 $http 服务发送 URL:

VenuesAppServices.factory('VenueDistance', ['$resource',function($resource){
return $resource('http://google.com/maps/api/distancematrix/json?origins=Bobcaygeon+ON|41.43206,-81.38992&destinations=Darling+Harbour+NSW+Australia|24+Sussex+Drive+Ottawa+ON|Capitola+CA', {}, { Distance: {method:'GET'}});}]);

或者

VenuesAppServices.factory('VenueDistance', ['$resource',function($resource){
return $resource('http://google.com/maps/api/distancematrix/json?origins='+ origin +'&destinations='+ destination, {}, { Distance: {method:'GET'}});}]);

谷歌地图 API 文档

于 2014-09-17T17:01:42.763 回答
0

Could be that you are getting cross domain javscript http request error and stringifying it

于 2014-12-30T15:08:33.140 回答