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

Sam Creamer
  • 5,187
  • 13
  • 34
  • 49
  • 1
    You could try using jQuery (http://stackoverflow.com/questions/4132685/google-maps-geocode-api-v3-not-returning-result-in-javascript-function). Also, you should accept a few more answers :) – dana Aug 16 '12 at 19:44
  • 1
    PLEASE use encodeURIComponent, not .replace to put the address in the URL...! – Matt Aug 16 '12 at 19:59

1 Answers1

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);
    }
});
Jonathan Wilson
  • 4,138
  • 1
  • 24
  • 36
  • 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? – Sam Creamer Aug 16 '12 at 19:57
  • Be sure to look at your JavaScript console to see the output! – Jonathan Wilson Aug 16 '12 at 19:58
  • Unfortunately there is no response in the console... I can see that it sent the request, though – Sam Creamer Aug 16 '12 at 20:04
  • 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. – Jonathan Wilson Aug 16 '12 at 20:15
  • 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 – Sam Creamer Aug 16 '12 at 20:16