I'd like to have google maps loop through a bunch of cities while doing a nearby search. However, the asynchronous strategy from maps is not allowing the code to operate as intended. I want it to be procedural and it seems like removing the callback would fix it.
Does anyone have advise?
Generally the answer has to be NO
The Maps-API internally uses a lot of async-requests, for example the loading of the tiles is the result of the callback of an asynchronous request.
But the issue here are not asynchronous requests at all, it's asynchronous requests to services(geocoding, places).
When I redefine the question to :
Is it possible to use responses of google maps services without using a callback?
The answer has to be: YES
These webservices may be accessed from serverside too, so you may get the results on serverside and pass them to the scripts directly.
Another option would be a synchronous request to the webservices, but in practice this would block the browser until you get the response, that's not a desirable option.
Related
I have a general query regarding creating a simple REST API using Google Cloud Functions in Python. Is it best practice to create a HTTP function for each endpoint or create one function which handles all the endpoints?
Also is it possible to use a custom URL for the function e.g. /api/v1/pets/
I wrote 2 articles on that topic, one in Python and the other in Golang.
But I can't recommend you to do that for real world workload, it's fun when you hack but not more. You can have a look to my first article that explain why I prefer Cloud Run to Cloud Function, and the local testing is the main argument that I can offer. Then portability.
Google Cloud only supports one event definition per
function. However, you could handle multiple definition by using same sources or use one trigger which has a response to call another function that combines different kind of events. You can refer to this GCP docs.
If you want to use a custom URL, you can check out Cloud Endpoints since it acts as an API Gateway for Cloud Functions.
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!
So I understand how to use the Google Directions service to add waypoints and to change the transit mode but is it possible to do both with the same API call?
I'm trying to get directions that will involve walking, then biking, and then some more walking but I'm not sure how to do it using one API call. I know I can split it up into a few but it will then force me to process Google's response vs using the setDirections method.
These are the docs I'm going from: https://developers.google.com/maps/documentation/javascript/directions
but is it possible to do both with the same API call?
Not at present.
it will then force me to process Google's response vs using the setDirections method.
Why do you say that? Unless you want to do something special, you should be able to use the DirectionsRenderer to display the results from multiple calls to the DirectionsService either by combining them or using different DirectionRenderer objects for the different pieces.
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
Is it possible to use the Google Maps API router just using HTTP? Something like http://maps.google.com?from=blah?to=blah
And have it return some sort of XML or JSON representing the directions?
If you want Google to allow legal access to driving directions via HTTP you might consider voting for the enhancement request: Issue 235. Theoretically, the more people that vote for an enhancement request, the more attention Google give to it.
However, I strongly suspect that there might be contractual issues with the organisations that supply the data. Organisations like TeleAtlas impose restrictions about how Google can use their data. TeleAtlas probably don't want Google to make it legal for people to use their data to create apps for free that directly compete with their own commercial apps and devices.
There is no documented and approved method to access driving directions via an HTTP API request.
This makes it difficult to get driving directions on the server-side, which I assume is your intention.
Not only it is undocumented and difficult to achieve, but it would also violate the restrictions 10.1 and 10.5 of the Google Maps API Terms and Conditions.
However, if you don't mind the challenge, and you believe that rules are there to be broken, you may want to check these articles:
Calculate driving directions using PHP?
Retrieve driving directions from google maps with server-side HTTP calls and show results with static maps for WAP
Actually yes, and they do it themselves
The REST format is like:
http://maps.google.com/maps/nav?output=js&q=from:%20Montreal%20to:%20Toronto&key=apikey
Where q should have the form: "from: x to: y" (url encoded).
Some of the parameters are similar to the HTTP Geocoding service: http://code.google.com/apis/maps/documentation/geocoding/index.html
Mike
As of May, 2010, directions are available via web services:
http://code.google.com/apis/maps/documentation/directions/
Directions are returned in XML or JSON format:
http://maps.google.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false
http://maps.google.com/maps/api/directions/xml?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false
It's pretty danged easy to use.
Here you can find a list of parameters you can pass to the maps.google.com URL. Maybe you'll be able to get the information you need. I don't know what the returned output contains. At least you are able to define different output types.
I have already used this library with python and it works well, although it's against Google Maps API Terms.
API v3 (made the official version in the last six months), does:
http://code.google.com/apis/maps/documentation/directions/
Also note that most of the competing services (Bing Maps, Yahoo, MapQuest, CloudMade, etc) also support RESTful web services along these lines.
As always with these services, check the Terms & Conditions.
I've never used the API but it is pretty easy to get the structured directions data from a google maps page using JS to access the gmap page's DOM.