v3n*_*3nt 43 google-maps geocoding city street-address
从地理编码器结果中获取不同的数组内容时遇到问题.
item.formatted_address有效,但不是item.address_components.locality?
geocoder.geocode( {'address': request.term }, function(results, status) {
response($.map(results, function(item) {
alert(item.formatted_address+" "+item.address_components.locality)
}
});
Run Code Online (Sandbox Code Playgroud)
//返回的数组是;
"results" : [
{
"address_components" : [
{
"long_name" : "London",
"short_name" : "London",
"types" : [ "locality", "political" ]
} ],
"formatted_address" : "Westminster, London, UK" // rest of array...
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏!
DC
v3n*_*3nt 57
最终使用以下工作:
var arrAddress = item.address_components;
var itemRoute='';
var itemLocality='';
var itemCountry='';
var itemPc='';
var itemSnumber='';
// iterate through address_component array
$.each(arrAddress, function (i, address_component) {
console.log('address_component:'+i);
if (address_component.types[0] == "route"){
console.log(i+": route:"+address_component.long_name);
itemRoute = address_component.long_name;
}
if (address_component.types[0] == "locality"){
console.log("town:"+address_component.long_name);
itemLocality = address_component.long_name;
}
if (address_component.types[0] == "country"){
console.log("country:"+address_component.long_name);
itemCountry = address_component.long_name;
}
if (address_component.types[0] == "postal_code_prefix"){
console.log("pc:"+address_component.long_name);
itemPc = address_component.long_name;
}
if (address_component.types[0] == "street_number"){
console.log("street_number:"+address_component.long_name);
itemSnumber = address_component.long_name;
}
//return false; // break the loop
});
Run Code Online (Sandbox Code Playgroud)
Гро*_*ный 15
尝试了几个不同的请求:
就像你说的,返回的数组大小不一致,但两个结果的Town似乎都在address_component项中,类型为["locality","political"].也许你可以用它作为指标?
编辑:使用jQuery获取locality对象,将其添加到您的响应函数:
var arrAddress = item.results[0].address_components;
// iterate through address_component array
$.each(arrAddress, function (i, address_component) {
if (address_component.types[0] == "locality") // locality type
console.log(address_component.long_name); // here's your town name
return false; // break the loop
});
Run Code Online (Sandbox Code Playgroud)
小智 10
我必须创建一个程序,当用户点击地图上的某个位置时,该程序将填写用户表单中的纬度,经度,城市,县和州字段.该页面可以在http://krcproject.groups.et.byu.net找到,是一个允许公众为数据库贡献的用户表单.我并不认为自己是专家,但效果很好.
<script type="text/javascript">
function initialize()
{
//set initial settings for the map here
var mapOptions =
{
//set center of map as center for the contiguous US
center: new google.maps.LatLng(39.828, -98.5795),
zoom: 4,
mapTypeId: google.maps.MapTypeId.HYBRID
};
//load the map
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
//This runs when the user clicks on the map
google.maps.event.addListener(map, 'click', function(event)
{
//initialize geocoder
var geocoder = new google.maps.Geocoder()
//load coordinates into the user form
main_form.latitude.value = event.latLng.lat();
main_form.longitude.value = event.latLng.lng();
//prepare latitude and longitude
var latlng = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
//get address info such as city and state from lat and long
geocoder.geocode({'latLng': latlng}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
//break down the three dimensional array into simpler arrays
for (i = 0 ; i < results.length ; ++i)
{
var super_var1 = results[i].address_components;
for (j = 0 ; j < super_var1.length ; ++j)
{
var super_var2 = super_var1[j].types;
for (k = 0 ; k < super_var2.length ; ++k)
{
//find city
if (super_var2[k] == "locality")
{
//put the city name in the form
main_form.city.value = super_var1[j].long_name;
}
//find county
if (super_var2[k] == "administrative_area_level_2")
{
//put the county name in the form
main_form.county.value = super_var1[j].long_name;
}
//find State
if (super_var2[k] == "administrative_area_level_1")
{
//put the state abbreviation in the form
main_form.state.value = super_var1[j].short_name;
}
}
}
}
}
});
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
我假设你想要城市和州/省:
var map_center = map.getCenter();
reverseGeocode(map_center);
function reverseGeocode(latlng){
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var level_1;
var level_2;
for (var x = 0, length_1 = results.length; x < length_1; x++){
for (var y = 0, length_2 = results[x].address_components.length; y < length_2; y++){
var type = results[x].address_components[y].types[0];
if ( type === "administrative_area_level_1") {
level_1 = results[x].address_components[y].long_name;
if (level_2) break;
} else if (type === "locality"){
level_2 = results[x].address_components[y].long_name;
if (level_1) break;
}
}
}
updateAddress(level_2, level_1);
}
});
}
function updateAddress(city, prov){
// do what you want with the address here
}
Run Code Online (Sandbox Code Playgroud)
不要尝试返回结果,因为您会发现它们是未定义的 - 这是异步服务的结果.您必须调用一个函数,例如updateAddress();
我创建了这个函数来获取地理编码器结果的主要信息:
const getDataFromGeoCoderResult = (geoCoderResponse) => {
const geoCoderResponseHead = geoCoderResponse[0];
const geoCoderData = geoCoderResponseHead.address_components;
const isEmptyData = !geoCoderResponseHead || !geoCoderData;
if (isEmptyData) return {};
return geoCoderData.reduce((acc, { types, long_name: value }) => {
const type = types[0];
switch (type) {
case 'route':
return { ...acc, route: value };
case 'locality':
return { ...acc, locality: value };
case 'country':
return { ...acc, country: value };
case 'postal_code_prefix':
return { ...acc, postalCodePrefix: value };
case 'street_number':
return { ...acc, streetNumber: value };
default:
return acc;
}
}, {});
};
Run Code Online (Sandbox Code Playgroud)
所以,你可以这样使用它:
const geoCoderResponse = await geocodeByAddress(value);
const geoCoderData = getDataFromGeoCoderResult(geoCoderResponse);
Run Code Online (Sandbox Code Playgroud)
假设您要搜索Santiago Bernabéu Stadium,那么结果将是:
{
country: 'Spain',
locality: 'Madrid',
route: 'Avenida de Concha Espina',
streetNumber: '1',
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77112 次 |
| 最近记录: |