2016-06-07 120 views
0

我在此处指定了place id“placeid = results [0] .place_id”但它没有设置。它返回给我未定义。分配操作在那里失败。Javascript值不分配

function match_up_dn(address) 
{ 
    geocoder = new google.maps.Geocoder(); 
    var placeid; 

    if (geocoder) 
    { 
     geocoder.geocode({ 
      'address': address 
     }, function (results, status) { 

      if (status == google.maps.GeocoderStatus.OK) { 
       console.log("al:-"+results[0].place_id); 
       placeid = results[0].place_id; 
      } 
      else 
      { 
       alert("undefined address"); 
      } 
     }); 
    } 

    return placeid; 
} 
+0

重写问题,以便它使一点点意义。 – jitendragarg

+0

我在此处指定了place id“placeid = results [0] .place_id”,但它没有设置。它返回给我未定义。分配操作在那里失败。 –

+0

我要求重写,而不是复制粘贴问题中提到的同一行。如果你无法忍受试图解释你的问题,不要指望任何人帮助你解决问题。 – jitendragarg

回答

0

试试这个

function match_up_dn(address) 
{ 
    geocoder = new google.maps.Geocoder(); 
    var placeid; 

     geocoder.geocode({ 
      'address': address 
     }, function (results, status) { 

      if (status == google.maps.GeocoderStatus.OK) { 
       console.log("al:-"+results[0].place_id); 
       placeid = results[0].place_id; 
       return placeid; 
      } 
      else 
      { 
       alert("undefined address"); 
      } 
     }); 

} 
+0

它没有工作! –

+0

@MitulLakhani现在尝试这个肯定会工作。 –

+0

您确实没有得到输出,因为API调用响应函数在后台线程上运行,所以主线程将在后台线程更新值之前返回空值。现在,当您将return语句移动到后台线程时,它将解决您的问题 –