问题
Hey there GMaps API developers. To start - what an incredible product and service the Google Maps/JS API v3 is - thank you so much! Now, with that important bit of sincere gratitude out of the way...
The short_name
vs long_name
options for address components is a nice feature, but for each service these two attributes mostly contain the same values, except geocode has what I would expect for short_name
for both and places has what I would expect for long_name
for both! *tears*
Before Places library came out I just assumed that Google didn't actually have short/long versions for street name, localities, etc - but now it seems you do have that formatting somewhere - maybe different product teams have different databases?
So what's the deal?
Image of what I'm talking about (would embed but this is my first stackoverflow question so no reputation): http://i.imgur.com/XPVj8.png
Detailed list of how I wish this worked:
- for routes, geocode's
short_name
places'long_name
- have the same "types" value for locality/sublocality between geocode and places (either is fine, just pick one)
- consistently provide short/long for state (administrative_area_level_1) - geocode gets this right but places doesn't
- same for country - and where is geocode getting its "USA" in the formatted address?
- would be neat if
postal_code
long_name
had zip+4, but I would imagine that'd be a huge endeavor from a data standpoint
Thanks so much - looking forward to any insight on this behavior!
Code behind the image:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GMaps API V3 Geocoder vs Places Address Components | Short vs Long Name</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true&libraries=places"></script>
<style type="text/css">
#map {height: 100px; width: 600px; border: 1px solid #333; margin-top: 0.6em; }
table { border-collapse: collapse; border-spacing: 0; }
table td {border-top: 1px solid #DDD; padding:4px 8px; }
</style>
<script type="text/javascript">
var map, geocoder, places, geocode_components, geocode_formatted, places_components, places_formatted
function initialize() {
geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': '3 South Main Street, York New Salem, PA'},
function (results, status) {
geocode_components = results[0].address_components;
geocode_formatted = results[0].formatted_address;
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: results[0].geometry.location,
zoom: 10
});
places = new google.maps.places.PlacesService(map);
places.search({location: results[0].geometry.location, radius:100, keyword: "Pizza"},
function(results, status) {
//get details - this feels a little extraneous to me, but y'all know best
places.getDetails({reference: results[0].reference},
function(result, status) {
places_components = result.address_components;
places_formatted = result.formatted_address;
writeResults();
});
});
});
}
function writeResults() {
tbody = document.getElementById('results');
formatted = document.getElementById('formatted');
var rows = "";
for(var i=0; i<geocode_components.length; i++) {
var c = geocode_components[i];
rows = rows + '<tr><td>Geocoding</td><td>' + c.types[0] + '</td><td>' + c.short_name + '</td><td>' + c.long_name + '</td></tr>';
}
for(var i=0; i<places_components.length; i++) {
var c = places_components[i];
rows = rows + '<tr><td>Places</td><td>' + c.types[0] + '</td><td>' + c.short_name + '</td><td>' + c.long_name + '</td></tr>';
}
tbody.innerHTML = rows;
formatted.innerHTML = '<br />Geocode Formatted: ' + geocode_formatted + '<br />' + 'Places Formatted: ' + places_formatted;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map"></div>
<table>
<thead><tr><th>Service</th><th>Type</th><th>Short Name</th><th>Long Name</th></tr></thead>
<tbody id="results"></tbody>
</table>
<div id="formatted"></div>
</body>
</html>
回答1:
As noted in comments - this question (request) was fixed/implemented back in January, 2013, see https://issuetracker.google.com/issues/35821543
来源:https://stackoverflow.com/questions/11055452/google-maps-address-components-geocoding-vs-places-api-short-vs-long-name