Using Google Maps' API v3, how can I geocode a city name, and have a map returned with the outline of the result? usually this is a political entity with strict boundaries, like a city, municipality, etc.
Which API method should be used?
To see it yourself, go to Google Maps and search for a city name or even a zip code. You will see a pinkish highlight around the border. Based on your zoom level, as you zoom out, Google will highlight the whole area, not just the borders, in the pink color.
A Map API (also known as Mapping API) provides location intelligence for software developers creating location-based products and services. It is the base building block for location-aware applications, feature-rich maps and the retrieval of geographic-related data.
The Google Maps API is one of those clever bits of Google technology that helps you take the power of Google Maps and put it directly on your own site. It lets you add relevant content that is useful to your visitors and customise the look and feel of the map to fit with the style of your site.
There isn't any API (at present) that provides that data.
Enhancement request
I think you will need Tiger/Line files as referenced in this answer. You can use the data to generate polygons. Use Geocoder.geocode
to search for city names. Here's a promise based function that I use:
function locateAddress(address) {
var deferred = $q.defer();
if(!address) { $q.resolve(null); return; }
var geocoder = new google.maps.Geocoder();
var a = address;
if(_.isObject(a)) {
a = (a.line1 || '') + ' ' + (a.city || '') + ' ' + (a.state || '') + ' ' + (a.zip || '');
}
geocoder.geocode({ 'address' : a}, function(results, status) {
if(status === 'ZERO_RESULTS') {
deferred.resolve(null);
return;
}
var c = results[0].geometry.location;
deferred.resolve({latitude: c.lat(), longitude: c.lng()});
}, function(err) {
deferred.reject(err);
});
return deferred.promise;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With