在Geocoder中返回undefined的函数 [英] Function returning undefined in Geocoder

查看:127
本文介绍了在Geocoder中返回undefined的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google maps v3 geocoder对地址进行地理编码,然后使用 getJSON 将jQuery文件中的2个坐标点传递给PHP文件。



问题:然而,我注意到执行地理编码功能的函数一直返回一个未定义的值!因此PHP文件接收一个未定义的变量。
$ b

jQuery代码

  var search_latlng = geocodeAddress(search_location); 
console.log(search_latlng);

$ .getJSON('/ main / get_places',{search_location:search_latlng},function(json){
$(#result_listing)。html('');



Geocoder JS函数
$ b $ pre $ 函数geocodeAddress(address){

var latlng = new Array(2);

geocoder.geocode({'address':address},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
latlng [ 0] = results [0] .geometry.location.lat();
latlng [1] = results [0] .geometry.location.lng();
返回latlng;
} else {
console.log(由于以下原因,Geocode不成功:+ status);
}
});
}


解决方案

您无法通过回调函数将该函数的值返回给Google代码。没有任何意义;geocode()函数n是异步。当你的回调运行时,外部函数将会返回。



正确的方法是模仿Google API本身:给你的函数一个回调参数,并从那里执行你的事后工作:

  function geocodeAddress(address,callback){

var latlng = new Array(2);

geocoder.geocode({'address':address},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
latlng [0] = results [0] .geometry.location.lat();
latlng [1] = results [0] .geometry.location.lng();
callback(latlng); //在这里调用回调函数
} else {
console.log(由于以下原因Geocode不成功:+ status;
}
});
}

编辑—作为你如何使用它的例子:

  geocodeAddress(search_location,function(search_latlng){
console。 log(search_latlng);
$ .getJSON('/ main / get_places',{search_location:search_latlng},function(json){
$(#result_listing)。html('');
// ...
});
});

它就像您的原始代码,但不会返回地理编码结果返回到您的代码,它作为参数传递给您提供的回调函数。


I am using Google maps v3 geocoder to geocode an address then pass the 2 coordinate points from the jQuery file to a PHP file using getJSON.

Problem: However, I notice that the function that does the geocoding function keeps returning an undefined value! Thus the PHP file receives a undefined variable. Where did I go wrong?

jQuery Code

var search_latlng = geocodeAddress(search_location);
console.log(search_latlng);

$.getJSON('/main/get_places', {search_location: search_latlng}, function(json){
        $("#result_listing").html('');
 .
 .
 .

Geocoder JS function

function geocodeAddress(address) {

    var latlng = new Array(2);

    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            latlng[0] = results[0].geometry.location.lat();
            latlng[1] = results[0].geometry.location.lng();
            return latlng;
        } else {
            console.log("Geocode was not successful for the following reason: " + status);
        }
    });
}

解决方案

You can't return a value from that function via the callback to the Google code. It makes no sense; the "geocode()" function is asynchronous. The outer function will have returned by the time that your callback runs.

The proper way to do this is to mimic the Google API itself: give your function a callback parameter, and perform your "afterwards" work from there:

function geocodeAddress(address, callback) {

    var latlng = new Array(2);

    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            latlng[0] = results[0].geometry.location.lat();
            latlng[1] = results[0].geometry.location.lng();
            callback(latlng); // call the callback function here
        } else {
            console.log("Geocode was not successful for the following reason: " + status);
        }
    });
}

edit — as an example of how you'd use this:

geocodeAddress(search_location, function(search_latlng) {
  console.log(search_latlng);
  $.getJSON('/main/get_places', {search_location: search_latlng}, function(json){
    $("#result_listing").html('');
    // ...
  });
});

It's like your original code, but instead of having the geocode result returned to your code, it's passed as the parameter to the callback function you provide.

这篇关于在Geocoder中返回undefined的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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