2013-09-25 13 views

回答

1

查看下面的代码,由我完成,其工作良好。在这个很多代码与你的东西相关,因为我已经做了完全相同的要求。所以你可以使用下面给出的所有代码。如果你觉得它没有问题,那就不要使用它。

@Override 
    public void onLocationChanged(Location location) { 

     try { 
      if (location != null) { 

       if (current_lat != null && current_lat > 0) { 
        latitude = current_lat; 
       } 
       if (current_long != null && current_long > 0) { 
        longitude = current_long; 
       } 
       current_lat = location.getLatitude(); 
       current_long = location.getLongitude(); 
       distanceBetweenTwoPoint = getDistance(latitude, longitude, current_lat, current_long); 
       if ((current_lat > 0 && current_long > 0) && distanceBetweenTwoPoint > IjoomerApplicationConfiguration.track_DistanceBetweenPoints_IN_METERS) { 
        if (location.hasSpeed()) { 
         speedInKm = location.getSpeed() * 3.6; 
        } else { 
         speedInKm = 0.0; 
        } 
        row = new HashMap<String, String>(); 
        row.put(TRACKID, IN_TRACKID + ""); 
        row.put(LATITUDE, current_lat.toString()); 
        row.put(LONGITUDE, current_long.toString()); 
        row.put(SPEED, speedInKm + ""); 
        row.put(TIMESTAMP, System.currentTimeMillis() + ""); 
        row.put(STATUS, status); 

        distance = distance + (distanceBetweenTwoPoint/1000); 
        row.put(DISTANCE, "" + distance); 
        dataProvider.InsertRow("TrackDetail", row); 
        row.put(LASTKNOWNLATITUDE, latitude.toString()); 
        row.put(LASTKNOWNLONGITUDE, longitude.toString()); 
        int seconds = (int) ((System.currentTimeMillis() - trackStartTime)/1000) % 60; 
        int minutes = (int) (((System.currentTimeMillis() - trackStartTime)/(1000 * 60)) % 60); 
        int hours = (int) (((System.currentTimeMillis() - trackStartTime)/(1000 * 60 * 60)) % 24); 

        row.put(DURATION, String.valueOf(hours) + " : " + String.valueOf(minutes) + " : " + String.valueOf(seconds)); 
        setNotification(speedInKm, String.valueOf(hours) + " : " + String.valueOf(minutes) + " : " + String.valueOf(seconds)); 
        if (status.equalsIgnoreCase("1")) { 

         builder.append("|" + current_lat + "," + current_long); 
         trackDuration = String.valueOf(hours) + " : " + String.valueOf(minutes) + " : " + String.valueOf(seconds); 
         if (speedInKm > maxSpeed) { 
          maxSpeed = speedInKm; 
         } 
         totalSpeed = totalSpeed + speedInKm; 
         ++totalTrackPoint; 
         sendBrodcastToActivity(); 
        } 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
2

速度:行驶距离的行驶时间划分。

所以计算当前速度,计算你需要通过该距离的时间,这是时间差了(由Location.distanceTo())和除法的最后两个位置之间的距离 - 地点的邮票。

或者,只需使用Location.getSpeed()

相关问题