调用gps信息在android2.2下正常,但是2.3以上gps设备显示出来,就是无法读出经纬度
代码如下
private void openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
getLocation();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面 }



} private void getLocation() {
// 获取位置管理服务
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 低功耗
String provider = locationManager.getBestProvider(criteria, true);
// 获取GPS信息
Location location = locationManager.getLastKnownLocation(provider);
//Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);   

// 通过GPS获取位置
updateToNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
locationManager.requestLocationUpdates(provider, 2 * 1000, 500,
locationListener);
}
private void updateToNewLocation(Location location) {
TextView tv1;
tv1 = (TextView) this.findViewById(R.id.tv1);
Intent intent=getIntent();
  String name=intent.getStringExtra("name");
  String mima=intent.getStringExtra("mima");
if (location != null) {
double latitude = location.getLatitude();//经度   
double longitude = location.getLongitude();//纬度  
double altitude =  location.getAltitude();     //海拔  
tv1.setText("维度:" + latitude + "\n经度" + longitude+"\n海拔:"+altitude);}}

解决方案 »

  1.   

    没有GPS信号吧,到空旷的位置试试,室内一般是不行的。
      

  2.   

    我也遇到这样的问题,写的程序使用2.2以下的SDK能正常运行并能获取到经纬度,但是用2.3的SDK用模拟器或者真机测试无法获取定位信息,程序会出错退出。
    请问楼主解决这个问题没?怎么解决的?
      

  3.   

    用如下code:
    Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);startActivity(callGPSSettingIntent);替换你的:Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);//这个是启动gps?