Google地理编码 - 无法拉动坐标 [英] Google Geocoding - Cannot pull co-ordinates

查看:126
本文介绍了Google地理编码 - 无法拉动坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里得到了一些帮助,并且试图将两段代码结合在一起以获得以下结果:用户单击地理编码按钮
向用户显示以下信息(输入地址,城市,州,国家,坐标)

这是我用一点help:
http://jsfiddle.net/QA7Xr/25/



这是完整的书面代码:

  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);
}

// GEOCODE
函数codeAddress(){
var address = document.getElementById(address)。value;
geocoder.geocode({
'address':address
},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
var address =,
city =,
state =,
zip =,
country =;
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.long_name;
else if(addr.types [0] =='street_address')// address 1
address = address + addr.long_name; $ b $ (else)if(addr.types [0] ==''''')address = address + addr.long_name; ;
else if(addr.types [0] =='postal_code')zip = addr.sh ort_name;
else if(addr.types [0] == ['administrative_area_level_1'])state = addr.long_name;
else if(addr.types [0] == ['locality'])city = addr.long_name;
}
alert('City:'+ city +'\\\
'+'State:'+ state +'\\\
'+'Zip:'+ zip +'\\\
'+ '国家:'+国家);
}
}
});
} else {
alert(地理编码不成功,原因如下:+ status);
};
});
}

initailize();
document.getElementById(codeAddress)。onclick = function(){
codeAddress();
返回false;
};

我是JavaScript编程的新手,但它的工作方式是,如果地理编码成功,变量并将它们转储到对话框中,但它只是没有做到。它在简单的括号错误上写错了还是写了一些更加错误的东西?

解决方案

initailize(); 。只要说。



修正小提琴(再次): http: //jsfiddle.net/d5DBh/



这一次,我把它分开了,所以没有人会覆盖它。修正:


  • 您在其他附近有一个额外的支架。已清除。

  • 您也从未定义或实例化 geocoder 。已添加

  • 初始化中输入错字。



请简化您的代码并实际阅读其他问题的更正。三个错误之一,我已经纠正。


I've had some help on here and I have tried to frankenstine two pieces of code together to get the following result:

User clicks Geocode button User is presented with the following information (Typed Address, City, State, Country, Co-ordinates)

This is the code I've pieced together with a bit of help: http://jsfiddle.net/QA7Xr/25/

This is the full written 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;
     geocoder.geocode({
         'address': address
     }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
             var address = "",
                 city = "",
                 state = "",
                 zip = "",
                 country = "";
             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.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 = address + addr.long_name;
                 else if (addr.types[0] == 'postal_code') zip = addr.short_name;
                 else if (addr.types[0] == ['administrative_area_level_1']) state = addr.long_name;
                 else if (addr.types[0] == ['locality']) city = addr.long_name;
             }
             alert('City: ' + city + '\n' + 'State: ' + state + '\n' + 'Zip: ' + zip + '\n' + 'Country: ' + country);
         }
     }
     });
 } else {
     alert("Geocode was not successful for the following reason: " + status);
 };
 });
 } 

 initailize();
 document.getElementById("codeAddress").onclick = function () {
     codeAddress();
     return false;
 };

I am new to javascript programming but the way it should work is if the geocoding is successful it grabs the variables and dumps them in a dialogue box, but it's just not doing it. Is it falling short on a simple bracket error or have a written something that is even more erroneous?

解决方案

initailize();. Just saying.

Have a corrected fiddle (again): http://jsfiddle.net/d5DBh/

This time, I have forked it so no-one will overwrite it. The fixes:

  • You had one extra bracket near else. Cleared.
  • You also never defined or instantiated a geocoder. This has been added
  • Typo on initialize.

Please simplify your code and actually read the corrections on other questions. One of the three bugs, I had already corrected.

这篇关于Google地理编码 - 无法拉动坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆