2017-02-24 38 views
0

我试图让纬度经度以下地址,但地理编码器无法找到纬度经度:Geocoder无法从android中的特定字符串获取纬度lng?

卡希纳Palazzetto酒店N.9 - 斯特拉达PROVINCIALE 134,波伊里诺(TO) - 10046,意大利

和我的代码片段如下:

public static LatLng getLocationFromAddress(Context context, String strAddress) { 
    Geocoder coder = new Geocoder(context); 
    List<Address> address; 
    LatLng p1 = null; 
    try { 
     address = coder.getFromLocationName(strAddress, 5); 
     if (address == null) { 
      return null; 
     } 
     Address location = address.get(0); 
     location.getLatitude(); 
     location.getLongitude(); 
    p1 = new LatLng(location.getLatitude(), location.getLongitude()); 
    } catch (Exception ex) { 
     Timber.d("getLocationFromAddress(printStackTrace): " + ex); 
    } 
    return p1; 
} 

编辑1:

而且,我试图与设置搜索在地理编码器上与意大利人手动地区设置,但仍然无法获得结果。

+1

没错这就是看起来像REST API的工作,但不知道为什么没有在Android上。 http://maps.google.com/maps/api/geocode/json?address=Cascina%20Palazzetto%20n.9%20-%20Strada%20Provinciale%20134,%20POIRINO%20(TO)%20-% 2010046,%20Italia&sensor = false –

+0

您能提供日志吗? – Cottontree

+0

列表对象地址的大小为0,而我无法从日志中获取任何信息。 –

回答

0

尝试运行地址列表的循环,当你得到拉特,长时间休息循环。

一样,

public static LatLng getLocationFromAddress(Context context, String strAddress) { 
     Geocoder coder = new Geocoder(context); 
     List<Address> address; 
     LatLng p1 = null; 
     try { 
      address = coder.getFromLocationName(strAddress, 5); 
      if (address == null) { 
       return null; 
      } 

      for(int i=0;i<address.size();i++) 
      { 


      Address location = address.get(i); 

      if(location.getLatitide!=0 && location.getLongitude!=0) 
      { 
         location.getLatitude(); 
         location.getLongitude(); 
         p1 = new LatLng(location.getLatitude(),      location.getLongitude()); 

        break; 
      } 
     } 
     } catch (Exception ex) { 
      Timber.d("getLocationFromAddress(printStackTrace): " + ex); 
     } 
     return p1; 
    } 
+0

但我得到的数组大小为0,所以情况将是相同的。 –

+0

使大小为10并尝试=> address = coder.getFromLocationName(strAddress,10); – Ragini

+0

相同的结果.... –