1

我需要得到我当前的坐标。我接下来尝试,但它没有得到结果。

public class FragmentNear extends SherlockFragment implements
    android.location.LocationListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    locationManager = (LocationManager) getActivity().getSystemService(
            Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 0, 0, this);
    v = inflater.inflate(R.layout.fragment_near, null);
    DownloadWebPageTask dw = new DownloadWebPageTask();
    dw.execute("");
    return v;
}

@Override
public void onLocationChanged(Location loc) {
    if (showMFD == 0) { 
    Log.d("myLogs", "-------- onLoChanged " + myLat + " " + myLong);
    myLat = loc.getLatitude();
    myLong = loc.getLongitude();
    Log.d("myLogs", "-------- onLoChangedEnd " + myLat + " " + myLong); 
    }
    }
public void showMFD() {
    showMFD++;
            //my Tasks
            }

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        Log.d("myLogs", "Background");
        while (myLat==0)
        {

        }
        showMFD();
        return "";
    }
@Override
protected void onPreExecute()
{
super.onPreExecute();

pb = new ProgressDialog(getActivity());
pb.setMessage("Получение данных GPS...");
pb.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pb.show();
}
 @Override
 protected void onPostExecute(String result) {
        pb.dismiss();

}
}

我的逻辑在图片中图一

4

1 回答 1

2

使用 wifi 或经典 gps(3g、h+、gprs...)的跟踪器类示例

public class GPSTracker extends Service implements android.location.LocationListener {
    /**
     * ATTRIBUTES
     */
    private static final String TAG                             = GPSTracker.class.getSimpleName();
    private Context             _context;
    private static GPSTracker   _instance;
    // The minimum distance to change Updates in meters
    private static final long   MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;                                // meters
    // The minimum time between updates in milliseconds
    private static final long   MIN_TIME_BW_UPDATES             = 1000 * 30 * 1;                    // minute
    // Declaring a Location Manager
    protected LocationManager   _locationManager;
    /**
     * PUBLIC ATTRIBUTES
     */
    boolean                     _isGPSEnabled                   = false;
    boolean                     _isNetworkEnabled               = false;
    boolean                     _canGetLocation                 = false;
    public Location             _location;
    double                      _latitude;
    double                      _longitude;

    public GPSTracker() {

        _context = null;
    }

    public static GPSTracker getInstance() {

        if (_instance == null) {
            _instance = new GPSTracker();
        }
        return _instance;
    }

    public void set_context(Context context) {

        this._context = context;
    }

    public Location getLocation(Context context) {

        _context = context;
        try {
            _locationManager = (LocationManager) _context.getSystemService(LOCATION_SERVICE);
            _isGPSEnabled = _locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            _isNetworkEnabled = _locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if (!_isGPSEnabled && !_isNetworkEnabled) {
                // no network provider is enabled
            }
            else {
                this._canGetLocation = true;
                if (_isNetworkEnabled) {
                    _locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (_locationManager != null) {
                        _location = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (_location != null) {
                            _latitude = _location.getLatitude();
                            _longitude = _location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (_isGPSEnabled) {
                    if (_location == null) {
                        _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (_locationManager != null) {
                            _location = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (_location != null) {
                                _latitude = _location.getLatitude();
                                _longitude = _location.getLongitude();
                            }
                        }
                    }
                }
            }
        }
        catch (Exception e) {
            // do nothing
        }
        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;
    }

    /**
     * Function to show settings alert dialog On pressing Settings button will lauch Settings Options
     */
    public void showSettingsAlert(String title, String msg) {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(_context);
        alertDialog.setTitle(title);
        alertDialog.setMessage(msg);
        // Settings button
        alertDialog.setPositiveButton("Paramètres", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                _context.startActivity(intent);
            }
        });
        // cancel button
        alertDialog.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                dialog.cancel();
            }
        });
        // show
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }
}

它和网络/gps/phone 一样快(感谢 android),您只需要创建 GPSTracker.java 然后创建一个对象:

//create an instance singleton
GPSTracker tracker = GPSTracker.getInstance();
// you can use this in "if" to test if location is updated ok
gpsTracker.canGetLocation()
//update the location as much as you want with this method
tracker.getLocation(this);
//retreive the location when you need it
Location location = EtablissementApplication._gpsTracker._location

在 onDestroy() 或当您不想再使用 gps 时,请不要忘记:

if (tracker != null) {
    tracker.stopUsingGPS();
}
于 2013-08-26T14:38:36.753 回答