为什么激活GPS不足以获得用户的位置?

时间:2013-11-20 10:38:22

标签: android google-maps gps

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

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

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

我已经读过的那些在选择最佳提供者后使用了getLastKnownLocation方法。在打开GPS服务后我有什么问题? GPS启动后GPS是否自动启动?

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

2 个答案:

答案 0 :(得分:1)

不,打开GPS是不够的,你还需要一个位置监听器来听取位置的更新。

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

答案 1 :(得分: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;
    }



}