2013-08-26 105 views
1

我需要获取当前坐标。我接下来尝试,但它没有得到结果。使用asyncTask获取GPS坐标

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

} 
} 

我的逻辑是在画面image one

+0

太复杂了。你可以做些简单的mooooore –

+0

怎么样?我只想看到progressBar,当坐标加载gom gps时? –

+0

啊,你没有问在这个问题^^ –

回答

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 /手机(谢谢安卓)你只需要创建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(); 
} 
+0

即坐标是否快速返回?这个班是内部班?我必须做什么? - 创建这个类的一个对象并调用gerLocation方法 –

+0

看到我的编辑顶部 –

+0

很多谢谢 –