将来自Google地理编码的返回值存储在外部JavaScript变量中 [英] storing returned value from google geocoding in an external javascript variable

查看:64
本文介绍了将来自Google地理编码的返回值存储在外部JavaScript变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法将来自Google地理编码的返回值存储到全局/外部(javascript)变量( latlng1 ,在以下情况下)....可能是因为该变量在地理编码完成之前获取其值...



在下面的代码中:

<$ p $ (('latlon ='+ latlng1); //显示未定义的

但是,

  alert('got value ='+ latLng); //给出合计值

那么,如何等待地理编码在将其分配给变量之前返回非空值?



这会解决问题吗?还是在代码中还有其他缺陷?



除此之外,所有部分代码工作正常(通过下面的代码中的评论中提到);我看到标记也正确地放置在地图上;



这是我的代码: -

 < script src =path_to_javascrip t_file.js>< /脚本> 
$ b $(some_element).click(function(){

var input = document.getElementById(some_input_element).vlaue;

var get_geocodes =函数get_value(latLng){
alert('got value ='+ latLng); //给出coorect的值
if(latLng == null){
geocode(input,get_geocodes)}
return latLng;
}

latlng1 = geocode(input,get_geocodes);
alert('latlon ='+ latlng1); // undefined

以下是我的javascript_file.js(包含在上面代码的开头):

 函数geocode(query,mycallback){

var geocoder = new google.maps.Geocoder();
latLng = null
geocoder.geocode({'address :查询}
回调函数(结果状态){
如果(状态== google.maps.GeocoderStatus.OK和放大器;&安培; results.length){
var latLng = results [0] .geometry.location;
console.log('geocoding successful:'+ latLng); //给出正确的值
add_marker(latLng,query);
mycallback(latLng);
}

else {
console.log(地理编码不成功,因为:+状态);
}
});


$ b函数add_marker(latLng,query){

var new_marker = new google.maps.Marker({
position: new google.maps.LatLng(latLng.lat(),latLng.lng()),
map:map,
title:查询,
动画:google.maps.Animation.DROP
});

console.log(new_marker.getPosition()。lat()); //给出正确的值
console.log(mew_marker.getPosition()。lng()); //给出正确的值
alert('added_marker'+ latLng +',,'+ location); //给出正确的值

$ b


解决方案

正如评论中所述,您需要等待 google.maps.Geocoder()完成其执行。 您的函数 geocode 不会返回任何东西,所以没有定义变量 latlng1 的惊喜。无论如何,如果 geocode 将返回 latLng 值,那么您不能保证将定义 latlng1



我的建议:


  1. 只需调用 geocoder 函数,而不将其分配给 latlng1

  2. 通过在全部函数外部初始化 latlng1 b
  3. 不要使用 latLng 变量,只需将所需的值直接赋值给全局变量 latlng1 就可以了:

      latlng1 = results [0] .geometry.location; 


  4. latLng 替换为 latLng 变量>在这两个调用中:add_marker(latlng1,query)和 mycallback(latlng1)函数或(这个最后的建议不是必需的,只是推荐。)只需重新定义add_marker()以接受一个值 - em> query 和 mycallback()不接受任何值,只需在这些函数中使用全局 latlng1 即可。



Unable to store the returned value from google geocoding into a global/external (javascript) variable (latlng1, in the following case).... perhaps because the the variable gets its value before the geocoding could complete...

For in the code below:

alert(('latlon='+latlng1); //shows undefined

But,

alert('got value = '+latLng);   //gives the coorect value

So, how to wait for geocoding to return a non-null value before assigning it to the variable??

And would this solve the problem? Or are there other flaws in the code too??

besides this however all parts of the code work fine (as mentioned through comments in code below) ; I see the marker also being placed correctly on the map;

Here is my code:-

    <script src="path_to_javascript_file.js"></script>

    $(some_element).click(function() {

             var input = document.getElementById(some_input_element).vlaue ;

             var get_geocodes =   function get_value(latLng) {
                              alert('got value = '+latLng);   //gives the coorect value
                              if (latLng == null){
                                  geocode(input, get_geocodes)} 
                              return latLng;
                                     }

             latlng1 =  geocode(input, get_geocodes);
             alert('latlon='+latlng1);  //says undefined

And following is my javascript_file.js (that is included at the beginning of the above code) :

function geocode(query, mycallback) { 

            var geocoder = new google.maps.Geocoder();
            latLng = null
            geocoder.geocode( { 'address': query}, 
            function callback(results, status) {
                if (status == google.maps.GeocoderStatus.OK && results.length) {
                  var latLng = results[0].geometry.location;
                  console.log('geocoding successful : ' + latLng);   //gives the correct value
                  add_marker(latLng, query);                 
                  mycallback(latLng);                    
                }

             else {
                  console.log("geocoding unsuccessful because of: " + status);
                }
            });  
          }


function add_marker(latLng , query) {

                    var new_marker = new google.maps.Marker({
                      position: new google.maps.LatLng(latLng.lat(), latLng.lng()),
                      map: map,
                      title: query ,
                      animation: google.maps.Animation.DROP                
                      }); 

                    console.log(new_marker.getPosition().lat());  //gives the correct value
                    console.log(mew_marker.getPosition().lng());  //gives the correct value
                    alert('added_marker'+latLng+',,'+location);   //gives the correct value

}

解决方案

As mentioned in comments you need to wait until google.maps.Geocoder() finishes its execution.

Your function geocode doesn't return anything, so no suprises that variable latlng1 is undefined. Anyway if geocode would return latLng value you have no guarantee that latlng1 will be defined.

My suggestion:

  1. Just call geocoder function without assigning it to latlng1.
  2. Make latlng1 variable global by initialising it outside all functions.
  3. Don't use latLng variable at all and just assign the required value directly to the global variable latlng1 like this:

    latlng1 = results[0].geometry.location;
    

  4. Replace latLng variable with latlng1 in these two calls: add_marker(latlng1, query) and mycallback(latlng1) functions or (This last advice is not essential, just recommended.) just redefine add_marker() to accept only one value - query and mycallback() accept no values and just use global latlng1 inside those functions.

这篇关于将来自Google地理编码的返回值存储在外部JavaScript变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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