2011-12-27 58 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(); 
}