Javascript 谷歌地理编码v3-获取格式化地址
我想知道是否有人能帮助我。我很难在我的地理编码结果对话框中获得坐标和格式化地址,尽管我似乎能够抓住城市、国家 以下是我正在使用的代码:Javascript 谷歌地理编码v3-获取格式化地址,javascript,jquery,google-maps,google-maps-api-3,geolocation,Javascript,Jquery,Google Maps,Google Maps Api 3,Geolocation,我想知道是否有人能帮助我。我很难在我的地理编码结果对话框中获得坐标和格式化地址,尽管我似乎能够抓住城市、国家 以下是我正在使用的代码: http://jsfiddle.net/d5DBh/5/ 代码 var mylatng=new google.maps.LatLng(31.272410,0.190898); //初始化 函数初始化(){ 变量映射选项={ 缩放:4, 中心:myLatlng, mapTypeId:google.maps.mapTypeId.ROADMAP } var map=
http://jsfiddle.net/d5DBh/5/
代码
var mylatng=new google.maps.LatLng(31.272410,0.190898);
//初始化
函数初始化(){
变量映射选项={
缩放:4,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var map=new google.maps.map(document.getElementById(“地图画布”),mapOptions);
}
//地理编码
函数代码地址(){
var address=document.getElementById(“地址”).value;
新的google.maps.Geocoder().geocode({
“地址”:地址
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
var address=“”,
城市=”,
镇=”,
state=“”,
国家=”,
位置=”,
格式=”;
对于(var i=0;i
此外,由于我是Javascript新手,我愿意接受任何关于更干净地编写此文档的想法 函数getLatLongDetail(myLatlng){
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);
}
}
});
}
var geocoder=new google.maps.geocoder();
geocoder.geocode({'latLng':myLatlng},
功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
var address=“”、city=“”、state=“”、zip=“”、country=“”、formattedAddress=“”;
var lat;
乏液化天然气;
对于(var i=0;i
注:lat=location.lat;&液化天然气=位置。液化天然气;而不是lat=location.lat();&lng=location.lng() 嗨。这是一个很好的答案,但不包括特定于这个问题的坐标或格式化地址。你看过地理编码器输出的API文档了吗?它显示返回坐标的位置。。谢谢你救了我的命
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);
}
}
});
}