2015-04-23 82 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米,在两种情况下它都会更新一个新位置?“ –