【问题标题】:Why activate GPS is not enough to get user's position?为什么激活 GPS 不足以获取用户的位置?
【发布时间】:2013-11-20 10:38:22
【问题描述】:

我正在开发一个 Android 项目,该项目让用户可以打开他的 GPS。请求他的职位以启动该应用程序。

激活后,我使用 LocationManager 的 getLastKnownLocation 但此方法返回一个 Location 指示从给定提供程序获得的最后一个已知位置修复的数据。该文档解释说,这可以在不启动提供程序的情况下完成。请注意,此位置可能已过时,例如,如果设备已关闭并移动到另一个位置。

但是用户激活本地化服务。因此,打开 GPS 并不足以启动 GPS,因为它是由 Google 地图通过在设备顶部显示一个图标并检索 GPS 坐标来完成的。当我的应用程序允许用户激活 GPS 时,该图标不会出现(看起来很奇怪)。

我已经读过的 Theards 在选择最佳提供商后使用 getLastKnownLocation 方法。问题是打开 GPS 服务后我错过了什么吗? GPS启动时是否会自动启动?

我已经问过this,但回答不够。

【问题讨论】:

    标签: android google-maps gps


    【解决方案1】:

    要在地图上显示蓝点图标,请使用此行

    map_fragment.setMyLocationEnabled(true);
    

    使用此代码获取您想要获取位置的位置

          GPSTracker gps = new GPSTracker(this);
                Double latiDouble = gps.getLatitude();
    
    
                Double  loDouble = gps.getLongitude();
    

    创建一个 GPSTracker 类并添加此代码

        public class GPSTracker extends Service implements LocationListener {
    
        private final Context mContext;
        boolean isGPSEnabled=false;
        boolean isNetworkEnabled=false;
        boolean canGetLocation=false;
        Location location;
        double latitude;
        double longitude;
        private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10;  //10 meters
        private static final long MIN_TIME_BW_UPDATES=1000*60*1; //1 minute
        private LocationManager locationManager;
    
        public GPSTracker(Context context)
        {
            this.mContext=context;
            getLocation();
        }
    
        public Location getLocation()
        {
            locationManager=(LocationManager) mContext.getSystemService(LOCATION_SERVICE);
    
            isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            if(!isGPSEnabled && !isNetworkEnabled)
            {
                //showSettingAlert();
            }
            else
            {
                this.canGetLocation=true;
    
                if(isGPSEnabled)
                {
                    if(location==null)
                    {
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
                    }
                    if(locationManager != null)
                    {
                        location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if(location != null)
                        {
                            latitude=location.getLatitude();
                            longitude=location.getLongitude();
                        }
                    }
                }
                if(isNetworkEnabled)
                {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
                    if(locationManager!=null)
                    {
                        location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                                if(location!=null)
                                {
                                    latitude=location.getLatitude();
                                    longitude=location.getLongitude();
                                }
                    }
                }
    
            }
            return location;
        }
    
        public void stopUsingGPS()
        {
            if(locationManager!=null)
            {
                locationManager.removeUpdates(GPSTracker.this);
            }
        }
    
        public double getLatitude()
        {
            if(location != null)
            {
                latitude=location.getLatitude();
            }
            return latitude;
        }
    
        public double getLongitude()
        {
            if(location != null)
            {
                longitude=location.getLongitude();
            }
            return longitude;
        }
    
        public boolean canGetLocation(){
            return this.canGetLocation;
        }
    
        public void showSettingAlert()
        {
            AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
            alertDialog.setTitle("GPS Settings");
            alertDialog.setMessage("GPS is not enabled .Do you want to go to settings menu ?");
    
            alertDialog.setPositiveButton("Settings",new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Intent i=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    mContext.startActivity(i);
                }
            });
            alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                }
            });
        }
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
    
    
        }
    
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
    
    
    }
    

    【讨论】:

      【解决方案2】:

      不,打开 GPS 还不够,您还需要一个 Location Listener 来收听位置更新。

      类似Location Strategies的东西

      // Acquire a reference to the system Location Manager
      LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
      
      // Define a listener that responds to location updates
      LocationListener locationListener = new LocationListener() {
          public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
              makeUseOfNewLocation(location);
          }
      
          public void onStatusChanged(String provider, int status, Bundle extras) {}
      
          public void onProviderEnabled(String provider) {}
      
          public void onProviderDisabled(String provider) {}
      };
      
      // Register the listener with the Location Manager to receive location updates
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多