The realization of different languages by the relative calculation of latitude distance

Source: Internet
Author: User
Tags asin cos pow sin

1. Calculate distance to maximum minimum latitude and longitude according to latitude and longitude
C #

 public class Latlonutil {private static double PI = 3.14159265;//pi private static double  
        _radius = 6378137;  

        private static Double RAD = math.pi/180.0;  
        @see//The circumference of the Earth is 24,901 miles. 24,901/360 = 69.17 miles/degree/** * @param raidus * unit: M return minlat,min  

            LNG,MAXLAT,MAXLNG */public static double[] Getaround (double lat, double lon, int raidus) {  
            Double latitude = lat;  

            Double longitude = lon;  
            Double degree = (24901 * 1609)/360.0;  

            Double raidusmile = Raidus;  
            Double Dpmlat = 1/degree;  
            Double Radiuslat = Dpmlat * raidusmile;  
            Double Minlat = Latitude-radiuslat;  

            Double Maxlat = latitude + Radiuslat;  
            Double mpdlng = degree * Math.Cos (latitude * (pi/180)); Double DPMLNG = 1/ MPDLNG;  
            Double radiuslng = dpmlng * raidusmile;  
            Double minlng = longitude-radiuslng;  
            Double maxlng = longitude + radiuslng;  
            System.out.println ("[+minlat+", "+minlng+", "+maxlat+", "+maxlng+"]);  
        return new double[] {Minlat, minlng, Maxlat, maxlng}; /** * Calculates the distance between two points according to the latitude and longitude coordinates between two points (double value), the unit is M * @param lng1 * @param lat1 * @param lng2 * @param lat2 * @return/public static double getdistance (double LNG  
            1-double LAT1, double lng2, double lat2) {Double RADLAT1 = lat1 * RAD;  
            Double RADLAT2 = lat2 * RAD;  
            Double A = RADLAT1-RADLAT2;  
            Double b = (lng1-lng2) * RAD; Double s = 2 * Math.asin (MATH.SQRT (Math.pow (Math.sin (A/2), 2) + Math.Cos (RADLAT1) * Math.Cos (Radla  
   T2) * Math.pow (Math.sin (B/2), 2));         s = S * earth_radius;  
            s = Math.Round (S * 10000)/10000;  
        return s; }  

    }

2. Distance between two latitude and longitude points
Php

<?php   
  define (' Earth_radius ', 6378.137);//Earth radius, assuming that the earth is a regular sphere of  
  define (' PI ', 3.1415926);  
    /** 
     *  calculates the distance between the latitude and longitude coordinates of the two groups 
     *   PARAMS:LAT1 latitude 1; lng1 longitude 1; lat2 latitude 2; lng2 longitude 2; Len_type (1:m or 2:km); 
     * Return   m or km  
    /function Getdistance ($lat 1, $lng 1, $lat 2, $lng 2, $len _type = 1, $decimal = 2)  
    {
  
    $radLat 1 = $lat 1 * PI ()/180.0;   Pi () pi  
       $radLat 2 = $lat 2 * PI ()/180.0;  
       $a = $radLat 1-$radLat 2;  
       $b = ($lng 1 * PI ()/180.0)-($lng 2 * PI ()/180.0);  
       $s = 2 * ASIN (SQRT (POW) (sin ($a/2), 2) + cos ($radLat 1) * cos ($radLat 2) * POW (sin ($b/2), 2));  
       $s = $s * earth_radius;  
       $s = round ($s * 1000);  
       if ($len _type--> 1)  
       {  
           $s/= 1000;  
       }  
   Return round ($s, $decimal);  
}  
Echo getdistance (38.0822841,114.4774532,38.0882967,114.4776678, 1);//output distance/m  


?>   
  

Java

/** 
 * Calculates any two points on Earth (latitude and longitude) distance 
 *  
 * @param long1 
 *            1th Longitude 
 * @param lat1 
 *            1th latitude 
 * @param Long2 
 *            2nd Longitude 
 * @param lat2 
 *            2nd latitude 
 * @return return distance unit: M/public  
static Double Distance (double long1, double lat1, double long2,  
        double lat2) {  
    Double A, B, R;  
    R = 6378137; Earth radius  
    lat1 = lat1 * math.pi/180.0;  
    LAT2 = Lat2 * math.pi/180.0;  
    A = LAT1-LAT2;  
    b = (long1-long2) * math.pi/180.0;  
    Double D;  
    Double SA2, SB2;  
    SA2 = Math.sin (a/2.0);  
    SB2 = Math.sin (b/2.0);  
    D = 2  
            * R  
            * Math.asin (MATH.SQRT (SA2 * SA2 + math.cos (LAT1)  
                    * MATH.COS (LAT2) * SB2 * sb2));  
    return D;  
}  

Sql server

CREATE FUNCTION [f_getdistance]  
(   
@GPSLng Decimal (12,6),  
@GPSLat Decimal (12,6),  
@Lng  Decimal ( 12,6),  
@Lat Decimal (12,6)  
)  
RETURNS Decimal (12,4)  
as  
BEGIN  
   DECLARE @result Decimal (12,4)  
   SELECT @result = 6378137.0*acos (SIN (@GPSLat/180*pi ()) *sin (@Lat/180*pi ()) +cos (@GPSLat/180*pi ()) *cos (@Lat/180 *pi ()) *cos ((@GPSLng-@Lng)/180*pi ()) return  
   @result end  
Go  
  

Js:

function Torad (d) {return d * math.pi/180;}  
    function Calculatedistance (LNGSRC, LATSRC, LNGDST, latdst) {Earth_radius = 6378137;  
    Alert (lngsrc+ "+latsrc+" "+lngdst+" "+latdst") radlngsrc = Torad (LNGSRC);  
    RADLNGDST = Torad (LNGDST);  
    Lngdiff = RADLNGSRC-RADLNGDST;  
    RADLATSRC = Torad (LATSRC);  
    RADLATDST = Torad (LATDST);  
    Latdiff = RADLATSRC-RADLATDST; s = 2 * Math.asin (MATH.SQRT (Math.pow) (Math.sin (LATDIFF/2), 2) + Math.Cos (RADLATSRC) * MATH.COS (RADLATDST) * M  
    Ath.pow (Math.sin (LNGDIFF/2), 2));  
    s = S * earth_radius;  
return Math.Round (s);  

    function Getdistancetext (distance) {distancetext = "";  
    if (distance <=) {//53 0.053km 53m <100m distancetext = "<100m";  
    else if (distance < 1000) {//532 0.532km 532m distancetext = distance + "M"; else if (distance < 10000) {//1532 1.532km 
        Distance = distance/1000; Distancetext = Distance.tostring (). substr (0, Distance.tostring (). indexOf (' \. ')  
    +2) + "km";  
        else if (distance < 100000) {//11532 11.532km distance = distance/1000; Distancetext = Distance.tostring (). substr (0, Distance.tostring (). indexOf (' \. ')  
    +2) + "km";  
    else {//111532 111.532km >100km distancetext = ">100km";  
return distancetext;   }

3. Map Tools

Package utils;  

Import Java.text.DecimalFormat;  /** * Map tool * @author LWG 2015-11-05 */public class Maputil {/** * return **.**km * @param lon1  1th Longitude * @param lat1 1th latitude * @param lon2 2nd Longitude * @param lat2 2nd latitude * @return * * Public static string Getstrdistance (String lon1, String lat1, String lon2, String lat2) {return getstrdistance (Maputi  
    L.distance (Lon1, LAT1, Lon2, LAT2)); /** * Returns **.**KM * @param distance distance * @return/public static String Getstrdistan  
        CE (double distance) {if (distance<100) {return "0.01KM";  
        } DecimalFormat df=new DecimalFormat ("#.##");  
    Return Df.format (distance/1000d) + "KM"; /** * Calculates any two points on Earth (latitude and longitude) distance * @param coordinate1 1th (longitude LNG, latitude lat) * @param coordinate2 2nd (longitude LNG, latitude L AT) * @return/public static double Distance (String coordinate1, String Coordinate2) {String log_lat1[] = Coordinate1.split (",");  
        String gg_lon1 = log_lat1[0];  

        String GG_LAT1 = log_lat1[1];  
        String log_lat2[] = Coordinate2.split (",");  
        String gg_lon2 = log_lat2[0];  
        String gg_lat2 = log_lat2[1];  
    Return Distance (Gg_lon1, GG_LAT1, Gg_lon2, GG_LAT2); 
     /** * Calculates any two points on Earth (latitude and longitude) distance * @param lng1 1th Longitude * @param lat1 1th latitude * @param lng2 2nd Longitude * @param lat2 2nd latitude * @return */public static double Distance (string lon1, String lat1, String lon2, St Ring lat2) {return Distance (double.parsedouble (Lon1), double.parsedouble (LAT1), double.parsedouble (Lon2), Doubl  
    E.parsedouble (LAT2)); /** * Calculates any two points on Earth (latitude and longitude) distance * * @param long1 1th Longitude * @param lat1 1th latitude * @param long2  Two point longitude * @param lat2 2nd latitude * @return return distance unit: M/public static double Distance (double long1, double Lat1, double long2, double lat2) {Double A, B, R; R = 6378137;  
        Earth radius lat1 = lat1 * math.pi/180.0;  
        LAT2 = Lat2 * math.pi/180.0;  
        A = LAT1-LAT2;  
        b = (long1-long2) * math.pi/180.0;  
        Double D;  
        Double SA2, SB2;  
        SA2 = Math.sin (a/2.0);  
        SB2 = Math.sin (b/2.0);  
        D = 2 * R * Math.asin (MATH.SQRT (SA2 * SA2 + math.cos (LAT1) * MATH.COS (LAT2) * SB2 * sb2));  
    return D; public static void Main (string[] args) {System.out.println ("correct:" +distance ("121.41128,31.170049", "121.")  
        411693,31.169045 "));  
        System.out.println ("Error:" +distance ("31.170049,121.41128", "31.169045,121.411693"));  
    System.out.println (Getstrdistance (987654321));   }  
}

Original link http://blog.csdn.net/boonya/article/details/52023275

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.