Showing posts with label lattitude. Show all posts
Showing posts with label lattitude. Show all posts

Thursday, April 4, 2013

Calculate Haversine Distance in Java


The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical triangle

The hav­er­sine formula is used to cal­cu­late the dis­tance between two points on the Earth’s surface spe­cified in lon­gitude and latitude. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical "triangles".




d is the dis­tance between two points with lon­gitude and lat­itude (ψ,φ) and r is the radius of the Earth.

public class HaversineDistance {

public static double haversineDistance(double lat1,double lat2,double lon1,double lon2) {
           double deltaLat = Math.toRadians(lat1 - lat2);
           double deltaLong = Math.toRadians(lon1 - lon2);
           double lat1R = Math.toRadians(lat1);
           double lat2R = Math.toRadians(lat2);
           double a = (Math.sin(deltaLat/2.0) * Math.sin(deltaLat/2.0)) +
                      (Math.sin(deltaLong/2.0) * Math.sin(deltaLong/2.0) *
                      Math.cos(lat1R) * Math.cos(lat2R));
           double c = 2.0*Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));
           double d = 3959.0*c;
           return d;
         }

         public static void main(String[] args) {
           System.out.println(haversineDistance(21.7679,40.4230,78.8718,98.7372));
         }

}

Monday, October 22, 2012

Google MAPS With JQuery


Implementing Google maps with with jQuery is quite easy once you know it. In this tip I am going to demonstrate how it can be done.

The prerequisites of the plugin are jQuery and google maps. You will need to obtain your own Google API Key


<html<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Google MAPS in jQuery</title>

<style type="text/css" media="screen">
  #map { float:left; width:500px; height:500px; }
  #list { float:left; width:200px; background:#eee; list-style:none; padding:0; }
  #list li { padding:10px; }
  #list li:hover { background:#555; color:#fff; cursor:pointer; cursor:hand; }
  #message { position:absolute; padding:10px; background:#555; color:#fffwidth:75px; }
</style>


<style media="screen"type="text/css">#map { width:500px; height:500px; }</style>
<script type="text/javascript" src="http://www.google.com/jsapi?key=YOUR API KEY"></script>

<script type="text/javascript"charset="utf-8">
  google.load("maps","2.x");
  google.load("jquery","1.3.1");
</script>

</head>

<body>
  <div id="map"></div>
  <ul id="list"></ul>
  <div id="message"style="display:none;">Hi vicky</div>
  <script type="text/javascript">
  $(document).ready(function(){
    var divMap=document.getElementById("map"); 
    var map = new GMap2(divMap);
    var chennai = new GLatLng(13.060422,80.249583);
    map.setCenter(chennai,8);
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    var markers = [];
    for (var i = 0; i<10; i++) {
      var point = new GLatLng(southWest.lat() + latSpan * Math.random(),
      southWest.lng() + lngSpan * Math.random());
      marker = new GMarker(point);
      map.addOverlay(marker);
      markers[i] = marker;
    }
    $(markers).each(function(i,marker){
      GEvent.addListener(marker,"click", function(){
      map.panTo(marker.getLatLng());
    });
  });
  $("<li/>").html("Point"+i).click(function(){
    map.panTo(marker.getLatLng());
  }).appendTo("#list");
  $("#message").appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));
});

function displayPoint(marker,index){
  $("#message").hide();
  var moveEnd = GEvent.addListener(map,"moveend", function(){
  var markerOffset = map.fromLatLngToDivPixel(marker.getLatLng());
  $("#message").fadeIn().css({ top:markerOffset.y, left:markerOffset.x });
    GEvent.removeListener(moveEnd);
  });
  map.panTo(marker.getLatLng());
}
</script>
</body>
</html>