-1

I am trying to use google's places api to get near by places.... so I tried adding the header just as its written in the code.... It didnt work... then i tired dataType:'jsonp' ... this shows Some error... when i saw the error in chrome... the data returned from google was there but I couldn't access it... but firefox didnt display the returned data from google completely but only the second line of the data "html_attributions":[],"

$.ajax({                        
       url : "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=13.089500428429146,77.48688038438559&radius=1000&type=bus_station&key=AXXXXXXXXXXXXXXXXXXXXXXX",
        type:"GET",
        //headers:{"Access-Control-Allow-Origin":"https://maps.googleapis.com/"},
        dataType: 'jsonp',
        success: function (return_data_json){
                console.log(return_data_json);
        }   
});
Subendra Sharma
  • 111
  • 1
  • 5
  • jsonp isn't the be all end all solution to CORS problems. It only works if the api you are requesting from also supports JSONP. – Kevin B Apr 15 '16 at 20:37
  • So can u give me some solution.... If u can..... – Subendra Sharma Apr 15 '16 at 20:38
  • If an API endpoint requires an API key, there's a pretty good chance that you won't be able to make the request using your browser (and probably aren't supposed to be anyway due to the TOS of said api.) – Kevin B Apr 15 '16 at 20:38
  • 1
    At the very top of the documenation: **"The Google Places API Web Service is for use in server applications. If you're building a client-side application, take a look at the Google Places API for Android and the Places Library in the Google Maps JavaScript API."** – Kevin B Apr 15 '16 at 20:39
  • I did it in android it works fine... why doesnt it work in browser – Subendra Sharma Apr 15 '16 at 20:39
  • As I said it works perfectly fine in android Phone.... I need it to work in browser... did u even try it.... – Subendra Sharma Apr 15 '16 at 20:40
  • No i didn't, i simply read the api. Because I know how CORS and JSONP works, i can tell you for a fact that what you are trying in your question cannot work. there is no workaround that doesn't involve instead making the request with your server, or using an entirely different api. – Kevin B Apr 15 '16 at 20:41
  • Can u see this post.... http://stackoverflow.com/questions/28359730/google-place-api-no-access-control-allow-origin-header-is-present-on-the-req Im having the same problem – Subendra Sharma Apr 15 '16 at 20:45
  • Yes, the answer is incorrect. – Kevin B Apr 15 '16 at 20:46
  • So why did the person who asked the question accepted the answer???... If i use node.js or php will it work for server side??? – Subendra Sharma Apr 15 '16 at 20:47
  • Yes, if you make the request server-side, where CORS doesn't apply, it will work. Note however that there is a limit to how many requests you can make per day to this api. the javascript api option at the top of the docs would be a better alternative because it is meant for this usage. – Kevin B Apr 15 '16 at 20:48
  • As far as accepting a wrong answer, who knows, maybe they just wanted two rep. – Kevin B Apr 15 '16 at 20:50
  • @Kevin B ... Thnxxxxxx I solved the problem ...but I was wondering why this api worked in Android phone(I was making HTTP request) even though I was using a Android Places WEB API??????.... and I am using this for my final year project in android phone since it reduces load in server... even though it worked using php... is i appropriate to use this WEB API in android Phones instead of a server???? – Subendra Sharma Apr 16 '16 at 12:19
  • nope just java ... normal stuff...and... I always run my programs in a real device .... never used emulator... – Subendra Sharma Apr 16 '16 at 14:38
  • Well that's why. You weren't using a browser. – Kevin B Apr 16 '16 at 14:50
  • no no... when i just copy paste the url on the browser ill get the result.... – Subendra Sharma Apr 16 '16 at 14:52
  • Well of course, that's not Ajax. The CORS policy only affects Ajax. – Kevin B Apr 16 '16 at 14:52
  • So how is it different...??? – Subendra Sharma Apr 16 '16 at 14:53
  • I just told you how it is different. I'm sorry if you don't understand it. – Kevin B Apr 16 '16 at 14:53
  • ajax sends mainly json data where as browser sends get and post??? – Subendra Sharma Apr 16 '16 at 14:55
  • No, Ajax transfers text via http.. The browser is a client that can connect to web servers via http by using Ajax or connecting to them directly. Ajax is subject to CORS, connecting directly is not. – Kevin B Apr 16 '16 at 14:56
  • kk thnxxxx very much... – Subendra Sharma Apr 16 '16 at 14:57

1 Answers1

0

ok so this is how we do it in javascript... google have their own functions for this....

link: https://developers.google.com/maps/documentation/javascript/places#place_search_requests

var map;
var service;
var infowindow;

function initialize() {
  var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);

  map = new google.maps.Map(document.getElementById('map'), {
      center: pyrmont,
      zoom: 15
    });

  var request = {
    location: pyrmont,
    radius: '500',
    types: ['store']
  };

  service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      var place = results[i];
      createMarker(results[i]);
    }
  }
}
  • anyone wants to gain access to the place Id and stuff this is how we do it.... In the call back function we have the JSONArray of places returned by google... In the call back function inside the for loop after the line var place = results[i]; u can get wat u want like

    console.log(place.name); console.log(place.place_id); var types = String(place.types); types=types.split(","); console.log(types[0]);

Subendra Sharma
  • 111
  • 1
  • 5