2011-05-23 87 views
1

你能幫我的..應用開始崩潰的時候我加入了地理編碼器使用經緯度來獲得城市的名字..Android的全球定位系統 - 讓緯度和經度

double latitude, longitude; 
    private TextView lala; 

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    longitude = location.getLongitude(); 
    latitude = location.getLatitude(); 
    final LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
     } 

     public void onProviderDisabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onProviderEnabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
      // TODO Auto-generated method stub 

     } 
    }; 

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 
    lala = (TextView) findViewById(R.id.textView1); 

    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault()); 
    List<Address> myList = null; 
    try { 
     myList = myLocation.getFromLocation(latitude, longitude, 1); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 


    lala.setText((CharSequence) myList); 
+0

堆棧跟蹤嗎? – matsjoe 2011-05-23 17:59:41

回答

8

lm.getLastKnownLocation能返回null,然後土特產品嚐試getLongitude()你會得到NullPointerException

而且lala.setText((CharSequence) myList);可以做同樣的(拋出異常)原因」 myList中可以爲空在這裏(如果Geocoder失敗)。

編輯 要在地理編碼處理異常考慮下面的清單

  1. 確保您的經度和緯度不是空
  2. 確保什麼-90 < =緯度< = 90和-180 < =經度< = 180
  3. 確保您有可用的網絡連接

EDIT2 改進代碼

private TextView lala; 

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

if(location != null) { 
    showMyAddress(location); 
} 

final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     showMyAddress(location); 
    } 

    public void onProviderDisabled(String arg0) { 
     // TODO Auto-generated method stub 

    } 

    public void onProviderEnabled(String arg0) { 
     // TODO Auto-generated method stub 

    } 

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
     // TODO Auto-generated method stub 

    } 
}; 

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 
lala = (TextView) findViewById(R.id.textView1); 



// Also declare a private method 

private void showMyAddress(Location location) { 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault()); 
    List<Address> myList; 
    try { 
     myList = myLocation.getFromLocation(latitude, longitude, 1); 
     if(myList.size() == 1) { 
      lala.setText(myList.get(0).toString());    
     } 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
} 
+0

你是對的..但你能告訴我該怎麼做,使Geocoder不失敗..對不起,我是初學者在Android和Java .. – Sergio 2011-05-23 18:20:14

+0

我編輯了我的答案 – Olegas 2011-05-23 18:38:15

+0

okey .. 現在我有緯度:59.412 ..和經度:24.689 .. 我的網絡連接工作正常.. 也許有什麼錯我的代碼 – Sergio 2011-05-23 18:44:12

2
Criteria criteria = new Criteria(); 
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
String provider = locationManager.getBestProvider(criteria, false); 
Location location = locationManager.getLastKnownLocation(provider); 
int la = (int) (location.getLatitude() * 1E6); 
int lo = (int) (location.getLongitude() * 1E6);