Javascript正在等待谷歌地理编码器
我有这个代码,问题是它警告“未定义”。如何发出Javascript正在等待谷歌地理编码器,javascript,google-maps,Javascript,Google Maps,我有这个代码,问题是它警告“未定义”。如何发出警报(添加)等待地理编码器?(我不想在函数codeAdd中使用警报(添加);) <script> function codeAdd(address){ var geocoder = new google.maps.Geocoder(); var address = "new york"; geocoder.geocode( { 'address': address}, function(results, status) { i
警报(添加)代码>等待地理编码器?(我不想在函数codeAdd
中使用警报(添加);
)
<script>
function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
return latitude;
}
}); }
var add = codeAdd("new york");
alert (add);
</script>
</body>
</html>
功能代码添加(地址){
var geocoder=new google.maps.geocoder();
var address=“纽约”;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var latitude=results[0]。geometry.location.lat();
var longitude=results[0]。geometry.location.lng();
返回纬度;
}
}); }
var add=代码添加(“纽约”);
警报(添加);
您遇到的问题是geoencode是异步的,因此它会在返回结果之前继续发送警报
<script>
function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
return latitude;
}
}); }
var add = codeAdd("new york");
alert (add);
</script>
</body>
</html>
您需要将警报放在异步回调中。换句话说,一旦完成查找,需要执行警报(取决于返回的数据)
<script>
function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
return latitude;
}
}); }
var add = codeAdd("new york");
alert (add);
</script>
</body>
</html>
// some calling code
function onLoad(){
codeAdd("new york");
}
// the function
function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
// we've got the details, show the alert
alert (latitude);
}
});
}
由于不使用codeAdd函数返回任何内容,因此无需执行以下操作:
<script>
function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
return latitude;
}
}); }
var add = codeAdd("new york");
alert (add);
</script>
</body>
</html>
var add = codeAdd("new york");
但是正义
<script>
function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
return latitude;
}
}); }
var add = codeAdd("new york");
alert (add);
</script>
</body>
</html>
codeAdd("new york");
您可以使用geocode的回调函数:
<script>
function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
return latitude;
}
}); }
var add = codeAdd("new york");
alert (add);
</script>
</body>
</html>
geocoder.geocode({
'address': address
}, function (results, status) {
alert(results);
//here the code to execute once geocode is loaded
});