0

I have a simple task but cannot seem to pull it off. I will have a page that allows the user to enter their address and have Google return directions to a destination. So, step number one was to go to the google maps API and see how this is done. So, from their example, I got the following url:

http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false

If you put this url in your browser, you will get a json result. So, my thought was, why can't I call this in a jQuery AJAX request? Is this possible? I have tried the request below but get an 'Access Denied' error in the jQuery script:

        $.ajax({
            type: "POST",
            url: "http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false",
            success: function (msg) {
                alert(msg);
            }
        });

Does anyone see a problem with the syntax or know why this doesn't work? And FYI, this is just the first question. I would like for this question to become a great resource for others to create a google map with directions via jquery and ajax without using php (which it seems as though everyone else is using).

1 Answer 1

2

In order to do cross-domain $.ajax, the URL given must support JSON-P. The URL you are using only supports JSON.

Edit: I was wrong. You are looking for directions, not Geocoding. I am not sure if there is anything for you in the API library, but you may pock it around.

https://code.google.com/apis/maps/documentation/javascript/reference.html

https://code.google.com/apis/maps/documentation/geocoding/

(Disregard information about Geocoding below)

Google disabled JSON-P callback for Geocoding to prevent abuse. You must use Google Geocoding API library to do the proper request, and the purpose of reverse Geocoding must be to show an address on Google Maps.

Sign up to request clarification or add additional context in comments.

2 Comments

I am pretty sure you must use Google Maps API Javascript instead of Directional API since you are working on web app on client-side.
That's what I figured (I didn't want it to be the case but I did have that gut feeling that it was the cross-domaining that would bite me in the rear). I wanted to have total control over the requests and the responses so that I could handle every event and failure with custom messages but it looks like I will have to use the API. Thanks for the info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.