4

I am building an application in which i am trying to fetch the list of places near to a given lat and long.

i got a api key and entering this on my browser url works and gives me a json file with the places data.

https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I

but when i try to fetch and parse it though j query its not giving me anything.

$(document).ready(function(){
$.getJSON('https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I',
 function(data) {
  alert(data);
});

i found the following questions on stack overflow but they dint reach me to an answer.

parse google place json with javascript

How do I use Google Places to get an array of Place names?

EDIT:

i tried to get contents of the file using php by

echo file_get_contents("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I");

and got the file contents using php.

But is there a way to parse this with jquery or javascript ?

Thank you for your help.

Community
  • 1
  • 1
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
  • Check for an update of your WAMP distribution. Common ones have HTTPS available. Which one are you using? – hakre Aug 13 '11 at 11:48
  • Related: [How to enable HTTPS stream wrappers](http://stackoverflow.com/questions/2305954/how-to-enable-https-stream-wrappers) (WAMP as well) – hakre Aug 13 '11 at 11:53
  • On my windows box I don't have a problem with the default PHP available from php.net using the HTTPS Stream wrapper (PHP 5.3.6). The [wrapper](http://www.php.net/manual/en/wrappers.php) needs the openssl extension to be enabled at least. Check if that extension is available and successfully loading. – hakre Aug 13 '11 at 11:56
  • i have already changed the line extension=php_openssl.dll in my php.ini .... also tried running but this gives no 'https' in the list.. – Mithun Satheesh Aug 13 '11 at 12:05
  • Not only uncomment that line but please check as well if the openssl extension is actually loading, e.g. cmd: `php -m` or by executing `phpinfo()`. – hakre Aug 13 '11 at 12:07
  • its not loading i guess.. i tried executing the function openssl_pkey_new() and i gave an error **Call to undefined function openssl_pkey_new()** – Mithun Satheesh Aug 13 '11 at 12:23
  • If the openssl functions are not available, than the extension does not get loaded. Maybe you're editing the wrong php.ini? I'm not fluent with WampServer. – hakre Aug 13 '11 at 12:32
  • i ran echo phpinfo(); in my system and got **Configuration File (php.ini) Path C:\WINDOWS
    Loaded Configuration File C:\wamp\bin\apache\Apache2.2.17\bin\php.ini** i edited the second one. Couldnt locate the first.
    – Mithun Satheesh Aug 13 '11 at 12:37
  • Second one is fine. Enable PHP error logging as well and check if the error log give's you an error/reason why the library is not loading. Maybe that's inside the apache error log as well. – hakre Aug 13 '11 at 12:39

4 Answers4

3

It's not possible using ajax (directly) cross domain request are not allowed. I've tried:

echo file_get_contents("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I");

it works for me (wamp 2.0 openssl enabled).

if you succeed in this, you can get the content using your ajax script on the page in the same domain.

EDIT:

The idea is to do an ajax call to your php page, the php page gets data from de google api and returns it. jquery automatically parses json :)

in code:

$.ajax({
  url: 'url_to_your_php',
  data: {
    location:'-33.8670522,151.1957362',
    radius:500,
    sensor:false,
    key:'AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I'},
  dataType: "json"
  type: "GET", //or POST if you want => update your php in that case
  success: function( data ) {
    for(var i in data.results){
       doSomthingWithYourLocation(data.results[i]);
    }
  },
  error: function (request, status, error) {
    //handle errors
  }
});

Your php shoud do somthing like this:

<?php 
    header('Content-type: application/json');
    echo file_get_contents("https://maps.googleapis.com/maps/api/place/search/json?" .
      "location=-" . $_GET['location'] .
      "&radius=" . $_GET['radius'] .
      "&sensor=" . $_GET['sensor'] .
      "&key=" .$_GET['key']);
?>

It is probably best to check the input in the php files but this should work.

VDP
  • 6,340
  • 4
  • 31
  • 53
  • yup.. but open ssl also is not getting loaded.. i uncommented the php.ini line. Any thing else tobe done. Or shall i reinstall the wamp. How can i install with open ssl enabled? – Mithun Satheesh Aug 13 '11 at 13:08
2

A script loaded from one domain can't make an ajax request to another domain. Use the Google Maps JavaScript api to load your data.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • i tried the codes in the below link http://code.google.com/apis/maps/documentation/javascript/places.html ..in **Place Search Requests** where to get the places near by we need to have a map initiated first. 'service = new google.maps.places.PlacesService(map); service.search(request, callback);' .. can this be converted to a request which without initiating maps. – Mithun Satheesh Aug 13 '11 at 12:12
  • I'm pretty sure it's not possible. The idea of the Maps API is to use Maps, not to get free information from Google. BTW, the terms of service (http://code.google.com/intl/fr-FR/apis/maps/terms.html) say: "You must not use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation". Accessing the API through PHP is a violation of the TOS. – JB Nizet Aug 13 '11 at 12:21
  • But im getting the data thought the file url i gave in the question. My problem is to parse it by either php or javascript. – Mithun Satheesh Aug 13 '11 at 12:25
2

The places javascript API can be accessed here: http://code.google.com/apis/maps/documentation/javascript/places.html

bperreault
  • 994
  • 9
  • 20
1

You can parse your object data this way:

$(document).ready(function(){
    $.getJSON('https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyAImBQiqvaXOQtqeK8VC-9I96kMmB6Mz7I', function(data) {
        $.each(data, function(index,place) {
            alert(place.name);
        });
    });
});

Hope it helps.

Littm
  • 4,923
  • 4
  • 30
  • 38
Julie
  • 91
  • 1
  • 6