根據經緯度分別用java和Oracle預存程序計算兩點距離

來源:互聯網
上載者:User

在給定2點的經緯度,通過java代碼和oracle預存程序來計算出點的距離 單位是(米)

oracle預存程序:

create or replace procedure SP_GET_DISTANCE(cx in number,cy in number,sx in number, sy in number,distance out varchar2) isd number;x number;y number;r number;pi number;begin--開始計算  r:=6371229;--地球半徑  pi:=3.14159265358979323;--圓周率  x:=(sx-cx)*pi*r*cos((sy+cy)/2*pi/180)/180;  y:=(sy-cy)*pi*r/180;  d:=SQRT(power(x,2)+power(y,2));  distance:=to_char(d,9999999999999.99);end SP_GET_DISTANCE;

java代碼:

package com.wpn.web.util;public class Distance {private final static double PI = 3.14159265358979323;// 圓周率private final static double R = 6371229;  // 地球的半徑private Distance() {}/** * 緯度lat 經度lon * @param longt1 * @param lat1 * @param longt2 * @param lat2 * @return */public static double getDistance(double longt1, double lat1, double longt2, double lat2) {double x, y, distance;x = (longt2 - longt1) * PI * R * Math.cos(((lat1 + lat2) / 2) * PI / 180) / 180;y = (lat2 - lat1) * PI * R / 180;distance = Math.hypot(x, y);return distance;}/*public enum GaussSphere {Beijing54, Xian80, WGS84,}private static double Rad(double d) {return d * Math.PI / 180.0;}public static double DistanceOfTwoPoints(double lng1, double lat1, double lng2, double lat2, GaussSphere gs) {double radLat1 = Rad(lat1);double radLat2 = Rad(lat2);double a = radLat1 - radLat2;double b = Rad(lng1) - Rad(lng2);double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));s = s * (gs == GaussSphere.WGS84 ? 6378137.0 : (gs == GaussSphere.Xian80 ? 6378140.0 : 6378245.0));s = Math.round(s * 10000) / 10000;return s;}*/public static void main(String[] arg){double longt1 = 116.515502;double lat1 = 39.863898;double longt2 = 116.304187;double lat2 = 40.052584;System.out.println(getDistance(longt1,lat1,longt2,lat2));}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.