Geocoding is the process of converting addresses ("1600 Amphitheatre Parkway, Mountain View, CA") to geographic coordinates (37.423021, -122.083739), which you can use to place markers or position the map. The Google Maps Platform APIs provide two approaches to geocoding:
- Client-side geocoding, which is executed in the browser, generally in response to user action. The Maps JavaScript API provides classes that make the requests for you. This approach is described in the Maps JavaScript API documentation.
- HTTP server-side geocoding, which allows your server to directly query Google's servers for geocodes. The Geocoding API is the web service that provides this functionality. Typically, you integrate this service with other code that is running server-side. Server-side geocoding is described in the Geocoding API documentation.
Examples of client-side and server-side geocoding
Here is a sample of client-side geocoding which takes an address, geocodes it, moves the center of the map to that location, and adds a map marker there:
geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'address': address }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); } });
For more examples, see the Maps JavaScript API documentation.
Here is an example using Python to do a server-side geocoding request:
import urllib2 address="1600+Amphitheatre+Parkway,+Mountain+View,+CA" key="my-key-here" url="https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s" % (address, key) response = urllib2.urlopen(url) jsongeocode = response.read()
This produces a JSON object with the following content:
{ "status": "OK", "results": [ { "types": street_address, "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "address_components": [ { "long_name": "1600", "short_name": "1600", "types": street_number }, { "long_name": "Amphitheatre Pkwy", "short_name": "Amphitheatre Pkwy", "types": route }, { "long_name": "Mountain View", "short_name": "Mountain View", "types": [ "locality", "political" ] }, { "long_name": "San Jose", "short_name": "San Jose", "types": [ "administrative_area_level_3", "political" ] }, { "long_name": "Santa Clara", "short_name": "Santa Clara", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "California", "short_name": "CA", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] }, { "long_name": "94043", "short_name": "94043", "types": postal_code } ], "geometry": { "location": { "lat": 37.4220323, "lng": -122.0845109 }, "location_type": "ROOFTOP", "viewport": { "southwest": { "lat": 37.4188847, "lng": -122.0876585 }, "northeast": { "lat": 37.4251799, "lng": -122.0813633 } } } } ] }
The server-side geocoder also provides an XML format as an alternative to JSON. For more examples, see the Geocoding API documentation and the client libraries for Python and other languages.
Quota and cost considerations
Geocoding costs, quotas, and rate limits drive the strategies outlined in this document.
Cost
Quota-per-day (QPD) limits are no longer in use for geocoding requests. Instead, each geocoding request, whether client-side through the browser or server-side through the Geocoding API web service, is billed at a per-each price. To manage your cost of use, consider capping your daily quota.
Rate limits
The geocoding service is rate limited to 3,000 QPM (queries per minute), calculated as the sum of client-side and server-side queries.
When running client-side geocoding requests at periodic intervals, such as in a mobile app, your requests may return errors if all of your users are making requests at the same time (for example, all at the same second of every minute). To avoid this, consider one of the following:
- Introduce random intervals to your requests (jitter). Ensure requests are random across your entire userbase.
- If developing for Android, use an inexact repeating alarm.
- If developing for Android, select an appropriate location strategy.
Caching
See Geocoding API Policies about caching.
When to use client-side geocoding
The short answer is "almost always." The reasons are:
- Client-side request and response provide a faster, more interactive experience for users.
- A client-side request can include information that improves geocoding quality: user language, region, and viewport.
In particular, client-side geocoding is best when geocoding addresses based on input from the user.
There are two basic architectures for client-side geocoding:
- Do the geocoding and the display entirely in the browser. For instance, the user enters an address on your page. Your application geocodes it. Then your page uses the geocode to create a marker on the map. Or your app does some simple analysis using the geocode. No data is sent to your server. This reduces load on your server.
- Do the geocoding in the browser and then send it to the server. For instance, the user enters an address on your page. Your application geocodes it in the browser. The app then sends the data to your server. The server responds with some data, such as nearby points of interest. This allows you to customize a response based on your own data.
When to use server-side geocoding
Server-side geocoding is best used for applications that require you to geocode addresses without input from a client. A common example is when you get a dataset that comes independently of user input, for instance if you have a fixed, finite, and known set of addresses that need geocoding. Server-side geocoding can also be useful as a backup for when client-side geocoding fails.
Some possible concerns are an unnecessary increase in latency for the user, and geocoding results of a lesser quality than client-side because less information is available in the request.