2011-05-23 45 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);