我正在使用Geocoder API获取地理位置的地址组件。在以下示例中,经/纬度39.35703,-81.30830从地理编码器API返回地址“ WV-2,圣玛丽,WV 26170,美国”。但是,该地址不正确。当我在Google地图上搜索39.35703,-81.30830时,我得到了正确的地址1 Heilman Ave,Belmont,WV 26134。
我在Google问题跟踪器中创建了一个问题,他们在这里推荐了我。
JSFiddle:https://jsfiddle.net/hoas9602/
该示例演示了获取Heilman Ave,Belmont,WV 26134的经度/纬度,然后返回以获取地址成分。请让我知道是否可以提供更多信息或澄清问题。
var address = "1 Heilman Ave, Belmont, WV 26134";
$(document).ready(function() {
getPosition();
});
function getPosition() {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
$(".output-lat-long").html(location.lat() + " " + location.lng())
getGeocodedAddress(location, false);
} else {
$(".output-lat-long").html("Geocode was not successful for the following reason: " + status);
}
});
}
function getGeocodedAddress(position) {
var address = {
streetNumber: null,
streetName: null,
city: null,
state: null,
stateAbbreviation: null,
zip: null,
county: null,
country: null
}
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'location': position }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$(".output-geocoded-formatted-address").html(results[0].formatted_address);
for (var i = 0; i < results[0].address_components.length; i++) {
for (var k = 0; k < results[0].address_components[i].types.length; k++) {
if (results[0].address_components[i].types[k] == "street_number") {
address.streetNumber = results[0].address_components[i].long_name;
}
else if (results[0].address_components[i].types[k] == "route") {
address.streetName = results[0].address_components[i].short_name;
}
else if (results[0].address_components[i].types[k] == "locality") {
address.city = results[0].address_components[i].long_name;
}
else if (results[0].address_components[i].types[k] == "administrative_area_level_1") {
address.state = results[0].address_components[i].long_name;
address.stateAbbreviation = results[0].address_components[i].short_name;
}
else if (results[0].address_components[i].types[k] == "postal_code") {
address.zip = results[0].address_components[i].long_name;
}
else if (results[0].address_components[i].types[k] == "administrative_area_level_2") {
address.county = results[0].address_components[i].long_name;
}
else if (results[0].address_components[i].types[k] == "country") {
address.country = results[0].address_components[i].short_name;
}
}
}
}
if (address.country == 'US') {
address.country = 'USA';
}
displayAddress(address);
});
}
function displayAddress(address) {
$(".output-street").html(address.streetNumber + " " + address.streetName);
$(".output-city").html(address.city);
$(".output-state").html(address.state);
$(".output-zip").html(address.zip);
$(".output-county").html(address.county);
$(".output-country").html(address.country);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=[KEY]"></script>
<div class="output-lat-long"></div>
<div class="output-geocoded-formatted-address"></div>
<div class="output-street"></div>
<div class="output-city"></div>
<div class="output-state"></div>
<div class="output-zip"></div>
<div class="output-county"></div>
<div class="output-country"></div>