原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/1730问题描述:我使用下面的代码来开发一个基于位置定位的项目,这个项目我是用的是google api 8。
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
currloc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
TextView t = (TextView)findViewById(R.id.textView1);
try{
t.setText("Your current location is - "+currloc.getLatitude()+","+currloc.getLongitude());
}catch (Exception e) {
// TODO: handle exception
t.setText("cant find current location ");
}这段代码在galaxy tab上能正常运行,htc上也可以。
但是当我使用nexus的时候,location得到的是null。对于galaxy nexus来说,我是不是得改变api版本,或者说还有其他一些特殊需求吗?解决方案:仿照下面的代码:
Step1: into your oncreateLocationListener locationListener = new LocalLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);Step2: into class body/**Listener on location change*/
private class LocalLocationListener implements LocationListener
{ public void onLocationChanged(Location location)
{
String text = "My current Location is: "+location.getLatitude()+", "+location.getLongitude();
GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude()* 1E6), (int)(location.getLatitude() * 1E6));
mapController_.animateTo(geoPoint);
Toast.makeText(LocalMap.this, text, Toast.LENGTH_SHORT).show();
Log.i("onLocationChanged", text); } public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(LocalMap.this, "GPS Disable", Toast.LENGTH_SHORT).show();
Log.i("onProviderDisabled", "GPS Disable");
} public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(LocalMap.this, "GPS Enable", Toast.LENGTH_SHORT).show();
Log.i("onProviderEnabled", "GPS Enable");
} public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub }android
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
currloc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
TextView t = (TextView)findViewById(R.id.textView1);
try{
t.setText("Your current location is - "+currloc.getLatitude()+","+currloc.getLongitude());
}catch (Exception e) {
// TODO: handle exception
t.setText("cant find current location ");
}这段代码在galaxy tab上能正常运行,htc上也可以。
但是当我使用nexus的时候,location得到的是null。对于galaxy nexus来说,我是不是得改变api版本,或者说还有其他一些特殊需求吗?解决方案:仿照下面的代码:
Step1: into your oncreateLocationListener locationListener = new LocalLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);Step2: into class body/**Listener on location change*/
private class LocalLocationListener implements LocationListener
{ public void onLocationChanged(Location location)
{
String text = "My current Location is: "+location.getLatitude()+", "+location.getLongitude();
GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude()* 1E6), (int)(location.getLatitude() * 1E6));
mapController_.animateTo(geoPoint);
Toast.makeText(LocalMap.this, text, Toast.LENGTH_SHORT).show();
Log.i("onLocationChanged", text); } public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(LocalMap.this, "GPS Disable", Toast.LENGTH_SHORT).show();
Log.i("onProviderDisabled", "GPS Disable");
} public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(LocalMap.this, "GPS Enable", Toast.LENGTH_SHORT).show();
Log.i("onProviderEnabled", "GPS Enable");
} public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub }android
解决方案 »
- 请教robotium写case的时候,在hierarchyviewer中看到的view id应该怎么用
- 菜鸟2帖
- 安卓tcp通信时休眠问题
- 如果想让自己的网站可以在手机上访问,应该如何入手啊?
- android 4.0 的系统 options menu 与 android 2.3 不一样
- 谁能帮我解释这段代码 虽然会很难 还请大家帮帮忙
- 图片横竖滚动效果
- ScrollView里嵌套ListView,如何禁用ListView的滚动事件,或者触发ListView滚动时调用父元素ScrollView的滚动事件?
- 后台运行成功,但是安卓模拟器显示不完全
- 求助!MPAndroidChart 只有一个数据点时 折线图不能描点的问题怎么解决?
- android 怎么支持语音聊天呢??
- android模拟器横竖屏切换问题
provider the name of the providerReturns
the last known location for the provider, or null