在获得GPS问题的Andr​​oid 2.1(找到答案)坐标 [英] problem in getting gps coordinates in android 2.1(Found Answer)

查看:208
本文介绍了在获得GPS问题的Andr​​oid 2.1(找到答案)坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在获得2.1的GPS坐标的一个问题。

在code我使用的,现在在1.6工作良好,但是当我测试
1.6设备同样的apk是显示空值

请帮我找到一种方法一起工作2.1设备也

下面是我的code。

 公共类GpsLocator {   私人静态字符串提供程序=GPS;
   私人的LocationManager myLocationManager = NULL;   公共GpsLocator(上下文的背景下){
      myLocationManager =(的LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
   }   公共无效myOnresume(){
      myLocationManager.requestLocationUpdates(PROVIDER,0,0,onLocationChange);
   }   公共无效myonPause(){
       myLocationManager.removeUpdates(onLocationChange);
   }   公共双getLatitude(){
      位置LOC = myLocationManager.getLastKnownLocation(供应商);
      如果(LOC == NULL){
         返回(0);
      }
      返回(loc.getLatitude());
   }   公共双getLongitude(){
      位置LOC = myLocationManager.getLastKnownLocation(供应商);
      如果(LOC == NULL){
         返回(0);
      }      返回(loc.getLongitude());
   }   LocationListener的onLocationChange =新LocationListener的(){      公共无效onLocationChanged(地点){
      }      公共无效onProviderDisabled(字符串提供商){
         //所需的接口,而不是使用
      }      公共无效onProviderEnabled(字符串提供商){
         //所需的接口,而不是使用
      }      公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
         //所需的接口,而不是使用
      }
   };
}

在manifest文件中我添加权限访问文件它们是

 <使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
<使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>


解决方案

 的LocationManager的LocationManager;
    字符串语境= Context.LOCATION_SERVICE;
    的LocationManager =(的LocationManager)getSystemService(背景);
    标准标准=新标准();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(假);
    criteria.setBearingRequired(假);
    criteria.setCostAllowed(真);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    字符串提供商= locationManager.getBestProvider(标准,真正的);    地点= locationManager.getLastKnownLocation(供应商);    updateWithNewLocation(位置);    locationManager.requestLocationUpdates(供应商,2000年,10,
                                           LocationListener的);
  }  私人最终LocationListener的LocationListener的=新LocationListener的(){
    公共无效onLocationChanged(地点){
      updateWithNewLocation(位置);
    }    公共无效onProviderDisabled(字符串提供商){
      updateWithNewLocation(NULL);
    }    公共无效onProviderEnabled(字符串提供商){}
    公共无效onStatusChanged(字符串提供商,INT状态,
                                捆绑演员){}
  };

I am having a problem in getting GPS coordinates in 2.1.

The code i am using right now is working well in 1.6 but when i test this same apk in 1.6 device is showing null values

please help me to find a way to work with 2.1 devices also

Here is my code.

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
      }
   };
}

in the manifest file i add permission for accessing file they are

<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" />

解决方案

    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){ }
  };

这篇关于在获得GPS问题的Andr​​oid 2.1(找到答案)坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆