我正在使用CoffeeScript执行getJSON
请求:
$.getJSON(
"http://maps.googleapis.com/maps/api/distancematrix/json?callback=?"
origins: origin
destinations: destinations
sensor: false
success: (data) ->
console.log data
error: (data) ->
console.log data
'json'
)
网址是:
http://maps.googleapis.com/maps/api/distancematrix/json?callback=?&origins=-25.8350643,28.1636066&destinations=-25.551836,%2028.423075|-25.218503,%2027.923075|&sensor=false
如果你把它放在你的浏览器中它会返回JSON,但是ajax请求告诉我:
Uncaught SyntaxError: Unexpected token:
有什么想法吗?
答案 0 :(得分:6)
该端点不支持(JSONP)回调 你应该这样做:
var distanceService = new google.maps.DistanceMatrixService();
distanceService.getDistanceMatrix({
origins: ['Istanbul, Turkey'],
destinations: ['Ankara, Turkey'],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
durationInTraffic: true,
avoidHighways: false,
avoidTolls: false
},
function (response, status) {
if (status !== google.maps.DistanceMatrixStatus.OK) {
console.log('Error:', status);
} else {
console.log(response);
}
});
答案 1 :(得分:2)
您绝不应直接从客户端代码*调用Google Maps网络服务。简单地说JSONP(即带有'回调'的json)不受支持。
您需要将距离矩阵服务用作Google Maps Javascript API的一部分 https://developers.google.com/maps/documentation/javascript/services#distance_matrix