2015-04-23 90 views
0

我寫了這個功能請求位置更新不更新:請求位置更新而駕駛

private void RequestLocations() { 
    locationListener = new LocationUpdater(); 
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 
       100, locationListener); 
    } else { 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 100, 
       locationListener); 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 
       100, locationListener); 
    } 

} 

我打電話,我已經取得了前臺服務的服務的onStart()此功能。 在locationUpdater我已實施locationListener。在位置改變,我正在烘烤緯度/朗。 如果我在步行速度,它工作正常。 但是,當我開車時,它並不像步行速度那樣準確和快速。如果我的速度超過50公里/小時,即使它不烘烤任何location,並且只要我停下來,它就會重新開始工作並烘烤新的location。 我想製作一些東西,只要我移動100米,無論速度如何,它都應該烤制新的location。 請指導我,如果我在任何地方錯了,請讓我正確。

+0

跟蹤器類的一個示例使用WiFi或經典的GPS(3G,H +,GPRS ...)[點擊這裏] (http://stackoverflow.com/questions/18446619/get-gps-coordinates-with-asynctask) – krishnan

+0

你的車內GPS信號如何? –

回答

0

嘗試這些代碼....它可以幫助你

public class GPSTracker extends Service implements LocationListener { 

    private final Context mContext; 

    // flag for GPS status 
    boolean isGPSEnabled = false; 

    // flag for network status 
    boolean isNetworkEnabled = false; 

    // flag for GPS status 
    boolean canGetLocation = false; 

    //flag for WIFi 
    boolean isWifiEnabled = false; 


    Location location; // location 
    double latitude; // latitude 
    double longitude; // longitude 

    // The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

    // Declaring a Location Manager 
    protected LocationManager locationManager; 

    public GPSTracker(Context context) { 
     this.mContext = context; 
     getLocation(); 
    } 

    public Location getLocation() { 
     try { 
      locationManager = (LocationManager) mContext 
        .getSystemService(LOCATION_SERVICE); 

      // getting GPS status 
      isGPSEnabled = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      isWifiEnabled = locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER); 


      if (!isGPSEnabled && !isNetworkEnabled&&!isWifiEnabled) { 
       // 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(); 

          } 
         } 
        } 
       } 


       if (isWifiEnabled) { 

        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.PASSIVE_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 

          } 
         } 
        } 

       } 



     } catch (Exception e) { 
     } 
     return location; 
    } 

    /** 
    * Stop using GPS listener 
    * Calling this function will stop using GPS in your app 
    * */ 
    public void stopUsingGPS(){ 
     if(locationManager != null){ 
      locationManager.removeUpdates(GPSTracker.this); 
     }  
    } 

    /** 
    * Function to get latitude 
    * */ 
    public double getLatitude(){ 
     if(location != null){ 
      latitude = location.getLatitude(); 
     } 

     // return latitude 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    * */ 
    public double getLongitude(){ 
     if(location != null){ 
      longitude = location.getLongitude(); 
     } 

     // return longitude 
     return longitude; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    * @return boolean 
    * */ 
    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 

    /** 
    * Function to show settings alert dialog 
    * On pressing Settings button will lauch Settings Options 
    * */ 
    public void showSettingsAlert(){ 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

     // Setting Dialog Title 
     alertDialog.setTitle("GPS is settings"); 

     // Setting Dialog Message 
     alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

     // On pressing Settings button 
     alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int which) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       mContext.startActivity(intent); 
      } 
     }); 

     // on pressing cancel button 
     alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
      } 
     }); 

     // Showing Alert Message 
     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; 
    } 

} 
+0

沒有額外的this.I嘗試這種組合也。我把gps和wifi上,並在此之後使服務一直活着,之後,我走了2公里,這是相同的代碼,除了你的getlocation函數的條件,但它當我開車時沒有烤過任何東西,只要我停下來烤了新的位置 –

+0

你好兄弟,你在代碼中看到的...它在1分鐘後更新位置..如果你想在移動時更改位置,請減少在代碼中的時間....這裏使用的參數是 「MIN_TIME_BW_UPDATES」.....給它的值是===「100 * 60 * 1」 就像它.. private static final long MIN_TIME_BW_UPDATES = 100 * 60 * 1; // 10秒 –

+0

如果我說我使用時間爲30秒,距離爲500米,那麼根據我在SO上的某處閱讀它的AND參數,這意味着什麼?這是否意味着「在30秒內如果我移動了500米,那麼它將更新新的位置?或者這意味着」如果30秒過去了,或者我移動了500米,在兩種情況下它都會更新一個新位置?「 –