谷歌地理编码v3 - 获取格式化地址 [英] Google Geocoding v3 - Getting formatted Address
本文介绍了谷歌地理编码v3 - 获取格式化地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我使用的代码:
http: //jsfiddle.net/d5DBh/5/
代码
var myLatlng = new google.maps.LatLng(31.272410,0.190898);
// INITALIZATION
函数initialize(){
var mapOptions = {
zoom:4,
center:myLatlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
var map = new google.maps.Map(document.getElementById(map-canvas),mapOptions);
$ b // GEOCODE
函数codeAddress(){
var address = document.getElementById(address)。value;
new google.maps.Geocoder()。geocode({
'address':address
},function(results,status){
if(status == google.maps .GeocoderStatus.OK){
if(results [0]){
var address =,
city =,
town =,
(var i = 0; i< results [0])的状态=,
country =,
location =,
format =;
.address_components.length; i ++){
var addr = results [0] .address_components [i];
if(addr.types [0] =='country')country = addr.short_name;
else if(addr.types [0] =='street_address')
address = address + addr.long_name;
else if(addr.types [0] =='route')
地址=地址+地址g_name;
else if(addr.types [0] == ['administrative_area_level_1'])
state = addr.long_name;
else if(addr.types [0] == ['administrative_area_level_2'])
town = addr.long_name;
else if(addr.types [0] == ['locality'])
city = addr.long_name;
else if(addr.types [0] == ['location'])
location = addr.location;
}
alert('Forming Address:'+ format +'\\\
'+'City:'+ city +'\\\
'+'Town:'+ town +'\\\
' +'State:'+ state +'\\\
'+'Country:'+ country +'\\\
'+'Coordinates:'+ location);
} else {
alert(错误:我们找不到这个地址 - 请尝试和其他位置一致);
};
});
}
initialize();
document.getElementById(searchItem)。onclick = function(){
codeAddress();
返回false;
};
另外,由于我是Javascript的新手,我很乐意接受任何关于更干净地编写代码的想法。
解决方案
function getLatLongDetail(myLatlng){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng':myLatlng},
function(results,status){
if(status == google.maps.GeocoderStatus.OK){
if (results [0]){
var address =,city =,state =,zip =,country =,formattedAddress =;
var lat;
var lng;
for(var i = 0; i< results [0] .address_components.length; i ++){
var addr = results [0 ] .address_components [i];
//检查address_components中的这个条目是否具有国家类型
if(addr.types [0] =='country')
country = addr。 long_name;
else if(addr.types [0] =='street_address')// address 1
address = address + addr.long_name;
e lse if(addr.types [0] =='establishment')
address = address + addr.long_name;
else if(addr.types [0] =='route')// address 2
address = address + addr.long_name;
else if(addr.types [0] =='postal_code')// Zip
zip = addr.short_name;
else if(addr.types [0] == ['administrative_area_level_1'])// State
state = addr.long_name;
else if(addr.types [0] == ['locality'])// City
city = addr.long_name;
}
if(results [0] .formatted_address!= null){
formattedAddress = results [0] .formatted_address;
}
// debugger;
var location = results [0] .geometry.location;
lat = location.lat();
lng = location.lng();
alert('City:'+ city +'\\\
'+'State:'+ state +'\\\
'+'Zip:'+ zip +'\\\
'+'格式化地址:'+ formattedAddress +'\\\
'+'纬度:'+ lat +'\\\
'+'Lng:'+ lng);
}
}
});
}
I was wondering if anyone is able to help me. I am having a hard time getting both the coordinates and the formatted address returned to me in my dialogue box on my geocoding result, even though I seem to be able to grab the city, country.
Here is the code I am working with:
http://jsfiddle.net/d5DBh/5/
Code
var myLatlng = new google.maps.LatLng(31.272410, 0.190898);
// INITALIZATION
function initialize() {
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
// GEOCODE
function codeAddress() {
var address = document.getElementById("address").value;
new google.maps.Geocoder().geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = "",
city = "",
town = "",
state = "",
country = "",
location = "",
format = "";
for (var i = 0; i < results[0].address_components.length; i++) {
var addr = results[0].address_components[i];
if (addr.types[0] == 'country') country = addr.short_name;
else if (addr.types[0] == 'street_address')
address = address + addr.long_name;
else if (addr.types[0] == 'route')
address = address + addr.long_name;
else if (addr.types[0] == ['administrative_area_level_1'])
state = addr.long_name;
else if (addr.types[0] == ['administrative_area_level_2'])
town = addr.long_name;
else if (addr.types[0] == ['locality'])
city = addr.long_name;
else if (addr.types[0] == ['location'])
location = addr.location;
}
alert('Formated Address: ' + format + '\n' + 'City: ' + city + '\n' + 'Town: ' + town + '\n' + 'State: ' + state + '\n' + 'Country: ' + country + '\n' + 'Coordinates: ' + location);
}
} else {
alert("Error: We could not find this address - please try and different location");
};
});
}
initialize();
document.getElementById("searchItem").onclick = function () {
codeAddress();
return false;
};
Also, since I am new to Javascript I am open to any ideas about writing this more cleanly.
解决方案
function getLatLongDetail(myLatlng) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': myLatlng },
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = "", city = "", state = "", zip = "", country = "", formattedAddress = "";
var lat;
var lng;
for (var i = 0; i < results[0].address_components.length; i++) {
var addr = results[0].address_components[i];
// check if this entry in address_components has a type of country
if (addr.types[0] == 'country')
country = addr.long_name;
else if (addr.types[0] == 'street_address') // address 1
address = address + addr.long_name;
else if (addr.types[0] == 'establishment')
address = address + addr.long_name;
else if (addr.types[0] == 'route') // address 2
address = address + addr.long_name;
else if (addr.types[0] == 'postal_code') // Zip
zip = addr.short_name;
else if (addr.types[0] == ['administrative_area_level_1']) // State
state = addr.long_name;
else if (addr.types[0] == ['locality']) // City
city = addr.long_name;
}
if (results[0].formatted_address != null) {
formattedAddress = results[0].formatted_address;
}
//debugger;
var location = results[0].geometry.location;
lat = location.lat();
lng = location.lng();
alert('City: '+ city + '\n' + 'State: '+ state + '\n' + 'Zip: '+ zip + '\n' + 'Formatted Address: '+ formattedAddress + '\n' + 'Lat: '+ lat + '\n' + 'Lng: '+ lng);
}
}
});
}
这篇关于谷歌地理编码v3 - 获取格式化地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文