0

Google offers a service where you can enter the address, and it will return the latitude and longitude (as well as other information). The way to use this google service is to call a url that returns a JSON. The google URL to call is this: http://maps.googleapis.com/maps/api/geocode/output?parameters I am trying to make the output of that go into a JSON that I can navigate through and select my desired variables.

Here is my code:

address_name = "1600 Amphitheatre Parkway, Mountain View, CA";
address_name = address_name.replace(/ /gi,'+');

var geocode = "http://maps.googleapis.com/maps/api/geocode/json?address="+address_name+"&sensor=true";
//alert(geocode)
var json = get_content_from(geocode)

I have tried to use .load() but either I'm doing something completely wrong or it just does not work. Is there a way to load this into a variable called json?

Sorry if this is trivial, I have tried searching but I'm not really sure what the keywords would be in this case so I've come up with nothing.

Thanks, Sam

2
  • 1
    You could try using jQuery (stackoverflow.com/questions/4132685/…). Also, you should accept a few more answers :) Commented Aug 16, 2012 at 19:44
  • 1
    PLEASE use encodeURIComponent, not .replace to put the address in the URL...! Commented Aug 16, 2012 at 19:59

1 Answer 1

1

Try this (and make sure you include jQuery).

address_name = "1600 Amphitheatre Parkway, Mountain View, CA";
address_name = address_name.replace(/ /gi,'+');

var geocode = "http://maps.googleapis.com/maps/api/geocode/json?address="+address_name+"&sensor=true";

$.ajax({
    url:geocode,
    success: function(json){
        console.log(json);
        var data = eval('(' + json + ')');
        console.log(data);
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

I have already included jquery at the top of the file, will try, thanks. EDIT: and where would I be able to locate the variables after I run this?
Be sure to look at your JavaScript console to see the output!
Unfortunately there is no response in the console... I can see that it sent the request, though
change console.log(json) to alert(json). Any better? If so, you can assume the JSON was parsed into a JavaScript object and stored in data. Do whatever further processing you need to do right there in the body of that success function.
thanks, I'll play around with this. appreciate the help edit: no alert when using alert(json), ubt I can still see that there is a request being made, with parameters, no response though

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.