Android Open Source - WhatsUp Geodetics






From Project

Back to project page WhatsUp.

License

The source code is released under:

GNU General Public License

If you think the Android project WhatsUp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**************
 * Copyright Albin Bramstng, Sebastian Ljunggren, Max Witt and Andreas Wnge 2011
 * // w ww. j  ava  2s . c o  m
    This file is part of WhatsUp.

    WhatsUp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    WhatsUp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with WhatsUp.  If not, see <http://www.gnu.org/licenses/>.
    */

package nu.placebo.whatsup.util;

import com.google.android.maps.GeoPoint;
public class Geodetics {
  
  
  /**
   * Pythagoras theorem with a  mercator projection. Efficient and accurate enough on short distances.
   * 
   * 
   * 
   * @param a - GeoPoint
   * @param b - GeoPoint
   * @return distance in meters
   */
  public static double distance(GeoPoint a, GeoPoint b){
    
    double[] alpha = GeoPointUtil.convertGeoPointToDoubles(a);
    double[] beta = GeoPointUtil.convertGeoPointToDoubles(b);
    
    double aLat = Math.toRadians(alpha[0]);  double bLat = Math.toRadians(beta[0]);
    double aLong = Math.toRadians(alpha[1]);  double bLong = Math.toRadians(beta[1]);    
    
    double x =  (bLong-aLong) * Math.cos((aLat+bLat)/2);
    double y = (bLat - aLat);
    
    return Math.sqrt(x*x + y*y)* 6371000;
  }
  
  /**
   * Produces a formatted string with a convenient unit depending on the actual distance.
   * 
   * Distances between 1 and 1000 m are presented: "123 m" (ex. dist = 123.4567)
   * Distances between 1000 and 100 000 m are presented "12.3 km" (ex. dist = 12345.67)
   * Distances from (including) 1000 km will result in a "far away" string (ex. dist = 1234567) 
   * 
   * @param distance
   * @return String with formatted value and approperiate unit
   */
  public static String distanceWithUnit(double distance){
    String result = "";
    if(distance<0)
      distance = -distance;
    
    distance = Math.round(distance);
    if(distance >= 1000){
      distance = ((double) Math.round(distance/100))*100;
    }
    
    
    
    if(distance < 1000){
      result = ((int)distance)+" m";
    } else if(distance < 1000000){
      distance = distance / 1000;
      result = distance+" km";
    } else {
      result = "Very far away";
    }
    
    
    return result;
  }
  
  public static String distanceWithUnit(GeoPoint a, GeoPoint b){
    return distanceWithUnit(distance(a,b));
  }
}




Java Source Code List

nu.placebo.whatsup.activity.AnnotationActivity.java
nu.placebo.whatsup.activity.CreateAnnotationActivity.java
nu.placebo.whatsup.activity.CreateCommentActivity.java
nu.placebo.whatsup.activity.ListViewActivity.java
nu.placebo.whatsup.activity.LogInActivity.java
nu.placebo.whatsup.activity.LoginRegTabActivity.java
nu.placebo.whatsup.activity.MapViewActivity.java
nu.placebo.whatsup.activity.Marker.java
nu.placebo.whatsup.activity.MenuHelper.java
nu.placebo.whatsup.activity.PositionPickerActivity.java
nu.placebo.whatsup.activity.RefPointActivity.java
nu.placebo.whatsup.activity.RegisterActivity.java
nu.placebo.whatsup.android.os.AsyncTask.java
nu.placebo.whatsup.balloon.BalloonItemizedOverlay.java
nu.placebo.whatsup.balloon.BalloonOverlayView.java
nu.placebo.whatsup.constants.Constants.java
nu.placebo.whatsup.ctrl.SessionHandler.java
nu.placebo.whatsup.datahandling.DataProvider.java
nu.placebo.whatsup.datahandling.DataReturnListener.java
nu.placebo.whatsup.datahandling.DataReturn.java
nu.placebo.whatsup.datahandling.DatabaseConnectionLayer.java
nu.placebo.whatsup.datahandling.DatabaseHelper.java
nu.placebo.whatsup.model.Annotation.java
nu.placebo.whatsup.model.Comment.java
nu.placebo.whatsup.model.ExtendedOverlayItem.java
nu.placebo.whatsup.model.GeoLocation.java
nu.placebo.whatsup.model.ListMarker.java
nu.placebo.whatsup.model.ReferencePoint.java
nu.placebo.whatsup.model.SessionInfo.java
nu.placebo.whatsup.network.AbstractNetworkOperation.java
nu.placebo.whatsup.network.Action.java
nu.placebo.whatsup.network.AnnotationCreate.java
nu.placebo.whatsup.network.AnnotationRetrieve.java
nu.placebo.whatsup.network.CommentCreate.java
nu.placebo.whatsup.network.GeoLocationsRetrieve.java
nu.placebo.whatsup.network.Login.java
nu.placebo.whatsup.network.NetworkCalls.java
nu.placebo.whatsup.network.NetworkOperationListener.java
nu.placebo.whatsup.network.NetworkOperation.java
nu.placebo.whatsup.network.NetworkTask.java
nu.placebo.whatsup.network.OperationResult.java
nu.placebo.whatsup.network.RegisterOperation.java
nu.placebo.whatsup.network.SessionTest.java
nu.placebo.whatsup.util.GeoPointUtil.java
nu.placebo.whatsup.util.Geodetics.java
nu.placebo.whatsup.util.ValidationUtil.java
nu.placebo.whatsuptest.TestStarter.java
nu.placebo.whatsuptest.activitytest.AnnotationActivityTest.java
nu.placebo.whatsuptest.activitytest.ListViewActivityTest.java
nu.placebo.whatsuptest.activitytest.MapViewActivityTest.java
nu.placebo.whatsuptest.networktest.NetworkTest.java
nu.placebo.whatsuptest.utiltest.GeoPointUtilTest.java
nu.placebo.whatsuptest.utiltest.GeodeticsTest.java
nu.placebo.whatsuptest.utiltest.ValidationTest.java