在Android 2.1中获取gps坐标的问题(找到的答案)

时间:2010-09-29 07:17:18

标签: android gps android-emulator

我在2.1中获取GPS坐标时遇到了问题。

我现在使用的代码在1.6中运行良好,但是当我测试时 这个相同的apk在1.6设备中显示空值

请帮助我找到使用2.1设备的方法

这是我的代码。

public class GpsLocator {

   private static String PROVIDER="gps";
   private LocationManager myLocationManager=null;

   public GpsLocator(Context context) {
      myLocationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
   }

   public void myOnresume() {
      myLocationManager.requestLocationUpdates(PROVIDER, 0,   0, onLocationChange);
   }

   public void myonPause() {
       myLocationManager.removeUpdates(onLocationChange);
   }

   public double getLatitude() {
      Location loc=myLocationManager.getLastKnownLocation(PROVIDER);
      if (loc==null) {
         return(0);
      }
      return(loc.getLatitude());
   }

   public double getLongitude() {
      Location loc=myLocationManager.getLastKnownLocation(PROVIDER);
      if (loc==null) {
         return(0);
      }

      return(loc.getLongitude());
   }

   LocationListener onLocationChange=new LocationListener() {

      public void onLocationChanged(Location location) {
      }

      public void onProviderDisabled(String provider) {
         // required for interface, not used
      }

      public void onProviderEnabled(String provider) {
         // required for interface, not used
      }

      public void onStatusChanged(String provider, int status,Bundle extras) {
         // required for interface, not used
      }
   };
}

在清单文件中,我添加了访问文件的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

3 个答案:

答案 0 :(得分:3)

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);
    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);

    Location location = locationManager.getLastKnownLocation(provider);

    updateWithNewLocation(location);

    locationManager.requestLocationUpdates(provider, 2000, 10,   
                                           locationListener);
  }

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

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status, 
                                Bundle extras){ }
  };

答案 1 :(得分:2)

LocationManager.getLastKnownLocation并不保证会返回有效位置。如果尚未建立GPS修复,则它将返回null。获取实际位置的唯一可靠方法是使用LocationListener - 接口。我看到您已经定义了LocationListener,但没有使用它。

您需要修改代码,以便在尝试对该位置执行任何操作之前等待对onLocationChanged的第一次调用。

答案 2 :(得分:1)

嗨我在2.1之后也遇到了同样的问题...... 它会对你有用

myManager = ((LocationManager) ApplicationController.getAppContext()
            .getSystemService(Context.LOCATION_SERVICE));

myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            1 * 1000, 0.00001f, this);

按照位置更改asy方法,你将获得lat / and long

不要在那里使用捆绑对象它不会工作而不是你可以使用Gps Satellite类2.1 ..