Google getJSON API for determining distance - syntax error

I use CoffeeScript to execute a getJSON request:

 $.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' ) 

URL:

 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 

If you put this in your browser, it will return JSON, but the ajax request will just tell me:

 Uncaught SyntaxError: Unexpected token: 

Any ideas?

+6
source share
2 answers

This endpoint does not support callbacks (JSONP).
You have to do it in a Google way:

  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); } }); 

See here .

+6
source

You should never call Google Maps web services directly from the client code *. Just JSONP (i.e. json with a "callback") is NOT supported.

You need to use the Distance Matrix service as part of the Google Maps Javascript API https://developers.google.com/maps/documentation/javascript/services#distance_matrix

  • Web services are designed to access APIs in which the Javascript API does not work, that is, on servers.
+2
source

Source: https://habr.com/ru/post/925822/


All Articles