2011-12-27 57 views
0
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0, this); 

    Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    geocoder = new Geocoder(this); 
if(location!=null) 
    onLocationChanged(location); 


    public void onLocationChanged(Location location) { 

     try { 
     List<Address> addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(), 2); // <10> 
     for (Address address : addresses) { 
     tv.setText(address.getAddressLine(0)); 
     } 


}catch(Exception e){ 

} 

这是我的示例代码。我创建了一个扩展Locationlistener的类,我也实现了其他的抽象类。我能够使用此代码获取城市名称。现在它不工作。使用地理编码器查找地名的问题

这段代码有没有错误?

+1

什么是不工作?错误?堆栈跟踪? – jagsler 2011-12-27 13:20:42

+0

我得到空指针错误。我不知道Ÿ我没有得到经纬度。 – Jeet 2011-12-27 13:23:15

+0

在哪一行..? – jagsler 2011-12-27 13:23:59

回答

0

试试这个....可能是有益的

String add1 = ""; 
Geocoder geoCoder = new Geocoder(context, Locale.getDefault()); 

geoLatitude = p.getLatitudeE6()/1E6; 
geoLongitude = p.getLongitudeE6()/1E6; 

try { 
    List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6, 1); 

    if(addresses.size() > 0) { 
     for(int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++) { 
      add1 += addresses.get(0).getAddressLine(i) + "\n"; 
     }    
    } 
}catch(IOException ioe) { 
    ioe.printStackTrace(); 
}