getJSON Google Distance Matrix API - Syntax Error
Asked Answered
G

2

6

I am using CoffeeScript to do 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 is:

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 that in your browser it will return the JSON, but the ajax request just tells me:

Uncaught SyntaxError: Unexpected token: 

Any ideas?

Glassman answered 20/9, 2012 at 8:1 Comment(2)
try to insert a comme before the last string 'json'Paradies
Its coffeescript, no comma needed.Glassman
M
6

That endpoint does not support (JSONP) callbacks.
You should do it Google's 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 documentation here.

Missi answered 25/5, 2014 at 23:38 Comment(3)
Ok but with this way we can't use trafficModel because It isn't returns in JSON result... So what is suppose to do ?Cassandra
Do you mean TravelMode? There is no TrafficModel. If you're looking for TrafficLayer, you won't get it in a JSON response. You should again use Google's API and add the traffic layer to the map. See Google docs.Trichina
No I speak about DrivingOptions. For display the delay/distance between two points but WITH TRAFFIC. It appear that this feature is reserved to user premium... This is very strange because the field can be displayed in JSON RESPONSE with the method GET (without premium account).Cassandra
O
2

You should never call Google Maps webservices directly from client-side code*. Simply JSONP (ie json with '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

  • The webservices are for accessing the APIs where the Javascript API cant run - ie in servers.
Ormsby answered 20/9, 2012 at 11:51 Comment(5)
Ok but with this way we can't use trafficModel because It isn't returns in JSON result... So what is suppose to do ?Cassandra
It listed as a valid option developers.google.com/maps/documentation/javascript/…Ormsby
Sure but this feature is reserved to premium user. However with the GET method the field trafficModel is returned in JSON response...Cassandra
I fail to understand why we're forced to use an API when any language (JS/jQuery) should be able to make the request. It is as if Google made sure it wouldn't be possible to homebrew the API's. Oh well. Guess I'll download an entire swiss army knife for the single request...Aluminothermy
I think part of the reasons, is you only permitted to use the API in conjunction with a Google Map. If in the browser you should be already loading the Maps API anyway. (ok, yes technically could be using the API on one page, and only load the Maps API in another page - they still in conjunction, but that is a relatively rare occurrence)Ormsby

© 2022 - 2024 — McMap. All rights reserved.