Can Google Maps/Places 'autocomplete' API be used via AJAX?

后端 未结 8 762
广开言路
广开言路 2020-12-14 11:34

I\'m trying to use the Google Places autocomplete API to pre-fill a form on a web application with Establishment data to ease data-entry. The API is pretty straightforward,

相关标签:
8条回答
  • 2020-12-14 12:22

    You can bypass this issue by using PHP.

    In your PHP script use file_get_contents to query the URL like this:

    $suggestions = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/place/autocomplete/json?input=blabla&sensor=false&types=establishment&radius=10000')

    You can then just use an AJAX request to your PHP script which will return the results effectively bypassing the cross domain constraint.

    0 讨论(0)
  • 2020-12-14 12:33

    The Google probably allows this API only called through its own Javascript API which you load from maps.google.com served Javascript file. The lack of Access-Control-Allow-Origin header tells that you should not use the API otherwise through Javascript.

    Alternatively you can simply write a server-side proxy function which calls Google API and passes the result forward to your own $.getJSON call, but this would be probably against the Terms of Service.

    http://www.daniweb.com/web-development/php/code/216729

    (disclaimer: I have not read the API spec for this particular function call)

    0 讨论(0)
提交回复
热议问题