2

The following does not work:

$.get('http://maps.googleapis.com/maps/api/geocode/json?&sensor=false&region=nz&address=queen', function(response){ 
console.debug(response); 
});

It seems like it is not possible due to Ajax's same origin policy. However, I am trying to do something interactive and I need responses from Google Maps API within javascript in order to do so.

Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
Aman
  • 1,624
  • 3
  • 15
  • 25

2 Answers2

4

The same origin policy may well make it difficult to access google maps URIs directly. But if you use google's provided Javascript API, you'll have no problems.

Lee
  • 13,462
  • 1
  • 32
  • 45
  • humm... looks like i got no other choice but to use google's own api function, but why it's api call would work and not ajax call. What's so different about it's implementation. thanx any way, appreciate the answer. – Aman Nov 28 '10 at 08:53
  • Their API probably uses something like [JSONP](http://stackoverflow.com/questions/2067472/please-explain-jsonp/2067584#2067584) to make the cross-domain calls work properly. In any case, their service is designed to be used through one of their APIs -- they don't want you to use it via any other mechanism. And why not? You benefit from using their API too -- by doing so, you insulate yourself from any future changes they may make to the underlying implementation. Just out of curiosity - why are you trying to avoid their API code? – Lee Nov 28 '10 at 18:41
  • using google api means they have usage limit right? only certain query request can be made to their server per day, and since i indent to use this location searching at frontend where user can type a location and available users of that location will display. I thought using direct url might save some request usage, right ? – Aman Nov 30 '10 at 05:22
  • @Aman: As far as I know, the limit is imposed per-user, not per-application (I don't know exactly what mechanism is used to track this; ip address? cookies? other google "magic"?). So, the limit is ([2500 requests per day](http://code.google.com/apis/maps/documentation/geocoding/#Limits), per-end user. Do you expect a single user of your app to place more than 2500 geolocation requests per day? (if so, it's probably fair that google would expect you to to buy a subscription). – Lee Nov 30 '10 at 06:33
  • @Aman: in any case, accessing their web service directly would: (1) be *very* difficult, (2) violate their terms of service, and (3) probably fail to bypass their limiting, which is likely implemented server-side. I've used the geocoding api on some very high-volume user-facing applications, and never had any trouble with the limits. *Use the api* - it's easy, full-featured, and... fun. :-) – Lee Nov 30 '10 at 06:38
0

An alternative to Lee's solution is to make an Ajax Request to your own server script which in turn makes a call to the Google geocoder url. Here's a simple example in PHP. A working example can be found here.

<?php

$address = $_GET['address'];
$address=str_replace(" ","+",$address);
if ($address) {
    $json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$address.
    '&sensor=true');
    echo $json;
}

?>

The jQuery Code

$.getJSON("getjson.php?address="+address,
        function(response){ 
                //rest of your code
             });
Philar
  • 3,887
  • 1
  • 24
  • 19