Implementing Google Places Autocomplete dropdown without the javascript library - google-maps

I'm implementing a Google Places Autocomplete dropdown, but need to do the api communication on the server as to not expose the API key (project requirement). The querying and returning of suggested addresses from my server to the client is done and working. Now I just need to build the dropdown with the data.
Implementing the autocomplete dropdown widget is fairly simple using the places javascript library (new google.maps.places.Autocomplete(elem)), but is there a low effort way of doing it when you're not using that library? Or is there a way of using that library without exposing your api key?

If you need to implement Place Autocomplete in client-side JavaScript then you should use the client-side library. Otherwise you'll run into a bunch of issues such as CORS errors.
If what you're worried about is exposing the API key, then note that there's no need to because the key can (and should) be restricted with HTTP referrers. As long as it's restricted, it cannot be used from third-party domains. To learn more about API key restrictions check out Google's documentation.
Hope this helps!

Related

Is it possible to use multiple Google Map Account under same Application?

I have a Web Application, which is hosted under a domain (say www.xx.com) and I have activated Google Map Account (say, MyAccount) for that Application as well. The Google Map Key under MyAccount; I have enabled for JavaScript Map API and Geocoding API only. There is another option in that Application, which uses Places API, which I haven't enabled due to the heavy costing for that what google have. But, one of my client want to use the Places API option and they have suggested me that to use Client Google Map Account (say, ClientAccount) for that particular Option; where I'll use their Map Key (under ClientAccount) while they sign-in into the Application. So, the following questions became relevant;
-Is it possible to use Dynamic Map Key under one Project?
Is there any violation of Terms? if yes,
- What is the alternative I can suggest to the client to achieve this requirement?
NB: I cannot make another instance of the application to host for the client as my services are under SaaS model.
Thanks,
Shenulal
Yes this should be possible, just send the customer's key with your Places API requests, and make sure they whitelist your domain in their key restrictions.

How come one doesn't need a Google Maps API Key to do this?

These 2 sites have some sample code on them. I copied and pasted the code into a php page in localhost, and they worked fine. However, the code doesn't seem to contain any API Keys. How come Google allows the code to use the Maps API without Keys? I'm obviously missing something here. What is it? Are there use cases where API Keys are not necessary? Where can I see the documentation regarding such uses cases?
http://www.codeofaninja.com/2014/06/google-maps-geocoding-example-php.html
https://gist.github.com/lazarofl/3901081#file-geocoder_example-html
The Google Maps JavaScript API v3 does not require an API key to function correctly. However, we strongly encourage you to load the Maps API using an APIs Console key which allows you to monitor your application's Maps API usage
From: https://developers.google.com/maps/signup

Migrating to Drive SDK from DocumentsList API V3

I just came to know that DocumentsList API is depricated. So i thought of migrating my app to Drive and saw this page. I mostly use urls like https://docs.google.com/feeds/default/private/full/. So is it enough to replace them with new URL's like this https://www.googleapis.com/drive/v2/files/ ?
Just wanted to confirm is that all i need to takecare or am I missing anything. My App uses OAuth2 for authorization. Do I need to change anything in there too?
If you are constructing the requests yourself, you have to replace the URLs but also take into account that the responses from the Drive API will be JSON-formatted and not XML-formatted as in the Documents List API.
If instead you are using one of the Google-provided client libraries, you have to start using a newer client library, that you can download from https://developers.google.com/drive/downloads.
The migration guide is mostly a guide that maps methods from the Documents List API into methods of the Drive API and should be used as reference to know what is available and where.
Oauth 2.0 is fine. You can even use the same tokens you already have for Documents List v3.

When using the Google Places API, what is the difference between "using the JavaScript library" and "calling the API directly"?

I have seen the two forms of reference to the Google Places Library/Service, using JavaScript vs calling the API directly, a number of times, but I don't understand the difference. The Google Docs don't describe anything about two methods of accessing the API.
For example, this question talks about 2 ways of accessing the API: OVER_QUERY_LIMIT in a loop
And it appears that there is some type of direct web access taking place in this question: Querying Google Places API using jQuery
Is this something where there was an old way that involved formatting URL parameters and the new way is by utilizing the JavaScript library calls?
FINAL SUMMARY EDIT: There are two distinct ways of requesting data from Google, as described in #Dan Nissenbaum's answer below. And since my original question, the QUERY_LIMIT question referenced above has been edited to also include more information about the two options.
Perhaps you are referring to the distinction between the Google Places API that is intended for use on the SERVER (i.e., utilizing PHP to call the Google Places API directly), and using the completely different approach of the Google Places Javascript Library in which the BROWSER executes Javascript using the Javascript library provided by Google (that internally wraps calls to the Google Places API, so that you, as a Javascript programmer, only need to understand the Javascript library provided by Google, and use that)?
Here are the two scenarios.
Scenario #1: Use the API directly. For this method, you must refer to Google's API documentation for the Google Places API: https://developers.google.com/maps/documentation/places/.
Using this API works as follows (giving a simple example only). Say you want to retrieve places within 1000 meters of latitude=-27.2531166, longitude=138.8655664. You need to hit a URL as described by the API documentation: https://developers.google.com/maps/documentation/places/#PlaceSearchRequests.
In this example, the URL looks like this (it's long):
https://maps.googleapis.com/maps/api/place/search/json?location=-27.2531166,138.8655664&radius=1000&sensor=false&key=AddYourOwnKeyHere
You need a key for your personal use, which I assume you have. There are other options you can specify, such as limiting the results to restaurants, etc.
When you hit this URL, the data will be returned in either JSON, or XML format, as specified by the text json in the URL above (use the text xml for xml). This data is returned exactly like data is returned from any URL call when you hit a URL in your browser.
You can test this by simply typing the URL directly in your browser, and see the results.
To use the API directly from code, you will need to use code that hits the external URL above within code and retrieves the results within code (for example, using the PHP CURL library, or using AJAX in Javascript).
Scenario #2: You use the Javascript library that Google provides that wraps the API, so you don't need to deal with it. I'll update the answer with more details about this, if you don't know what this is.
The docs do discuss the two different approaches. The Places Library utilizes the Google Places services from within the JavaScript Google Maps API. If you are using the Google Maps API in a browser, this is probably the approach for you:
https://developers.google.com/maps/documentation/javascript/places
There is also a web service, which allows you to query directly from your application. You query it using direct http calls to Google services. If you need access to the data on your server or a mobile device, this is the approach you want to take:
https://developers.google.com/maps/documentation/places

Using google maps API with varied domains

Ok, so I have a site that uses the google maps API, and I want to serve it from more domains. The fact that the API key is tied to a domain greatly hinders my efforts on this front.
Is there any solution more elegant than getting a key for every domain?
There is one way you can avoid using the API key completely - use the version 3 API.
The v3 API is still in 'beta' but it has the key (no pun intended) functionality that most people use for their maps and there are no keys required. Nor will there be in the future according to Google. If you can get by without features like draggable markers, then v3 will work well. I use it for my primary map page and have no problems with it.
You can set up a proxy. But obviously that will complicate your code, and it's your responsibility to make sure only your domains can access the proxy.