Gps android - 获取纬度和经度
Posted
技术标签:
【中文标题】Gps android - 获取纬度和经度【英文标题】:Gps android - getting Latitude and Longitude 【发布时间】:2011-05-23 17:55:52 【问题描述】:你能帮我解决这个问题吗.. 当我添加地理编码器以使用纬度和经度获取城市名称时,应用程序开始崩溃..
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);
【问题讨论】:
【参考方案1】:lm.getLastKnownLocation
可以返回 null,然后你尝试getLongitude()
你会得到NullPointerException
lala.setText((CharSequence) myList);
也可以做同样的事情(抛出异常),因为这里的 myList 可以为空(如果 Geocoder
失败)。
编辑 要处理 Geocoder 中的异常,请考虑以下清单
-
确保您的纬度和经度不为空
确保 -90
确保您有可用的网络连接
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();
【讨论】:
你是对的..但是你能告诉我该怎么做才能使 Geocoder 不会失败.. 对不起,我是 android 和 java 的初学者.. okey.. 现在我有纬度:59.412.. 和经度:24.689.. 而且我的网络连接工作正常.. 也许我的代码有问题 是的,我又发现了一个错误 - 您正在将 List 转换为 CharSequence,这是无法完成的。请参阅我的第二次编辑。 好吧...你是我的救命恩人...谢谢【参考方案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);
【讨论】:
以上是关于Gps android - 获取纬度和经度的主要内容,如果未能解决你的问题,请参考以下文章