Kev Tshawb Xyuas thiab Kev NtsuasCRM thiab Cov Ntaub Ntawv PlatformsCov cuab yeej ua lag luam

Xam lossis nug Lub voj voog loj nyob nruab nrab ntawm cov ntsiab lus ntawm Latitude thiab Longitude Siv Haversine Formula (PHP, JavaScript, Java, Python, MySQL, MSSQL Piv txwv)

Lub hli no, kuv tau ua haujlwm hauv PHP thiab MySQL rau GIS. Thaum tshawb nrhiav lub ntsiab lus, kuv nyuaj nrhiav geographic xam nrhiav kev deb ntawm ob qhov chaw, yog li kuv xav qhia lawv ntawm no.

Davhlau Daim Ntawv Qhia Tebchaws Europe Nrog Zoo Lub voj voog deb

Txoj kev yooj yim ntawm kev suav qhov deb ntawm ob lub ntsiab lus yog siv Pythagorean mis los laij hypotenuse ntawm ib daim duab peb sab (A² + B² = C²). Tus no lub npe hu ua cov Euclidean nrug.

Qhov ntawd yog qhov pib nthuav, tab sis nws tsis siv rau thaj chaw vim qhov kev ncua deb ntawm kab latitude thiab longitude yog tsis sib npaug sib nrug. Raws li koj tau los ze rau ntawm txoj kab nruab nrab, kab ntawm latitude sib nrug ntxiv. Yog tias koj siv qhov sib npaug triangulation yooj yim, nws yuav ntsuas qhov deb ntawm ib qho chaw thiab tsis ncaj ncees lawm rau lwm qhov vim yog qhov curvature ntawm lub ntiaj teb.

Yawm Vauv Sib Nrauj

Cov kev taug kev mus ntev nyob ib ncig ntawm lub ntiaj teb yog hu ua Great Circle Distance. Qhov ntawd yog… qhov luv tshaj ntawm ob lub ntsiab lus ntawm ib lub kheej kheej txawv ntawm cov ntsiab lus ntawm daim ntawv qhia tiaj tus. Ua ke nrog qhov tseeb tias kab latitude thiab longitude tsis sib npaug ... thiab koj tau txais kev suav nyuaj.

Ntawm no yog cov lus piav qhia zoo heev ntawm Kev Ua Haujlwm Loj Li Cas ua haujlwm.

Cov Qauv Haversine

Qhov kev ncua deb siv lub curvature ntawm lub ntiaj teb yog muab tso rau hauv cov qauv Haversine, uas siv trigonometry tso cai rau lub ntiaj teb curvature. Thaum koj tab tom nrhiav qhov kev ncua deb ntawm 2 qhov chaw hauv ntiaj teb (raws li tus noog ya), txoj kab ncaj nraim yog qhov arc.

Qhov no muaj feem xyuam rau hauv huab cua ya davhlau - koj puas tau saib daim ntawv qhia tseeb ntawm kev ya davhlau thiab pom tias lawv arched? Tias yog vim hais tias ya nyob rau hauv ib tug arch ntawm ob lub ntsiab lus yog luv dua ncaj qha mus rau qhov chaw.

PHP: Xam Qhov Nrig Nruab Nrab ntawm 2 Cov Lus Nkag Nruab Nrab thiab Ntev

Nov yog PHP formula rau xam qhov kev ncua deb ntawm ob lub ntsiab lus (nrog rau Mile vs. Kilometer conversion) sib npaug mus rau ob qhov chaw decimal.

function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'miles') {
  $theta = $longitude1 - $longitude2; 
  $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); 
  $distance = acos($distance); 
  $distance = rad2deg($distance); 
  $distance = $distance * 60 * 1.1515; 
  switch($unit) { 
    case 'miles': 
      break; 
    case 'kilometers' : 
      $distance = $distance * 1.609344; 
  } 
  return (round($distance,2)); 
}

Cov variables yog:

  • $Latitude1 - qhov sib txawv rau koj qhov chaw thawj qhov chaw latitude.
  • $Longitude1 - qhov sib txawv rau koj thawj qhov chaw longitude
  • $Latitude2 - qhov sib txawv rau koj qhov chaw thib ob lub latitude.
  • $Longitude2 - qhov sib txawv rau koj qhov chaw thib ob qhov longitude.
  • $unit - lub neej qub txhiab txhiab. Qhov no tuaj yeem hloov kho lossis dhau li mais.

Java: xam qhov nrug nruab nrab ntawm 2 cov ntsiab lus ntawm Latitude thiab Longitude

public static double getDistanceBetweenPointsNew(double latitude1, double longitude1, double latitude2, double longitude2, String unit) {
    double theta = longitude1 - longitude2;
    double distance = 60 * 1.1515 * (180/Math.PI) * Math.acos(
        Math.sin(latitude1 * (Math.PI/180)) * Math.sin(latitude2 * (Math.PI/180)) + 
        Math.cos(latitude1 * (Math.PI/180)) * Math.cos(latitude2 * (Math.PI/180)) * Math.cos(theta * (Math.PI/180))
    );
    if (unit.equals("miles")) {
        return Math.round(distance, 2);
    } else if (unit.equals("kilometers")) {
        return Math.round(distance * 1.609344, 2);
    } else {
        return 0;
    }
}

Cov variables yog:

  • latitude 1 - qhov sib txawv rau koj qhov chaw thawj qhov chaw latitude.
  • longitude 1 - qhov sib txawv rau koj thawj qhov chaw longitude
  • latitude 2 - qhov sib txawv rau koj qhov chaw thib ob lub latitude.
  • longitude 2 - qhov sib txawv rau koj qhov chaw thib ob qhov longitude.
  • chav tsev - lub neej qub txhiab txhiab. Qhov no tuaj yeem hloov kho lossis dhau li mais.

JavaScript: xam qhov nrug ntawm 2 cov ntsiab lus ntawm Latitude thiab Longitude

function getDistanceBetweenPoints(latitude1, longitude1, latitude2, longitude2, unit = 'miles') {
    let theta = longitude1 - longitude2;
    let distance = 60 * 1.1515 * (180/Math.PI) * Math.acos(
        Math.sin(latitude1 * (Math.PI/180)) * Math.sin(latitude2 * (Math.PI/180)) + 
        Math.cos(latitude1 * (Math.PI/180)) * Math.cos(latitude2 * (Math.PI/180)) * Math.cos(theta * (Math.PI/180))
    );
    if (unit == 'miles') {
        return Math.round(distance, 2);
    } else if (unit == 'kilometers') {
        return Math.round(distance * 1.609344, 2);
    }
}

Cov variables yog:

  • latitude 1 - qhov sib txawv rau koj qhov chaw thawj qhov chaw latitude.
  • longitude 1 - qhov sib txawv rau koj thawj qhov chaw longitude
  • latitude 2 - qhov sib txawv rau koj qhov chaw thib ob lub latitude.
  • longitude 2 - qhov sib txawv rau koj qhov chaw thib ob qhov longitude.
  • chav tsev - lub neej qub txhiab txhiab. Qhov no tuaj yeem hloov kho lossis dhau li mais.

Python: xam qhov nrug ntawm 2 cov ntsiab lus ntawm Latitude thiab Longitude

Ntawm no yog Python mis rau xam qhov kev ncua deb ntawm ob lub ntsiab lus (nrog rau Mile vs. Kilometer conversion) sib npaug rau ob qhov zauv. Credit rau kuv tus tub, Bill Karr, Tus Kws Tshawb Fawb Txog Kev Tshawb Fawb rau OpenINSIGHTS, rau code.

from numpy import sin, cos, arccos, pi, round

def rad2deg(radians):
    degrees = radians * 180 / pi
    return degrees

def deg2rad(degrees):
    radians = degrees * pi / 180
    return radians

def getDistanceBetweenPointsNew(latitude1, longitude1, latitude2, longitude2, unit = 'miles'):
    
    theta = longitude1 - longitude2
    
    distance = 60 * 1.1515 * rad2deg(
        arccos(
            (sin(deg2rad(latitude1)) * sin(deg2rad(latitude2))) + 
            (cos(deg2rad(latitude1)) * cos(deg2rad(latitude2)) * cos(deg2rad(theta)))
        )
    )
    
    if unit == 'miles':
        return round(distance, 2)
    if unit == 'kilometers':
        return round(distance * 1.609344, 2)

Cov variables yog:

  • latitude 1 - qhov sib txawv rau koj thawj qhov chaw latitude.
  • longitude 1 - qhov sib txawv rau koj thawj qhov chaw ntev ntev
  • latitude 2 - qhov sib txawv rau koj qhov chaw thib ob latitude.
  • longitude 2 - qhov sib txawv rau koj qhov chaw thib ob ntev ntev.
  • chav tsev - lub neej qub txhiab txhiab. Qhov no tuaj yeem hloov kho lossis dhau li mais.

MySQL: Retrieving tag nrho cov ntaub ntawv nyob rau hauv ib tug ntau yam los ntawm xam qhov deb nyob rau hauv mais siv latitude thiab longitude

Siv Spatial Data Types hauv MySQL yog ib txoj hauv kev zoo dua thiab yooj yim rau kev ua haujlwm nrog cov ntaub ntawv thaj chaw, suav nrog kev xam qhov nrug ntawm cov ntsiab lus. MySQL txhawb Spatial Data Types xws li POINT, LINESTRING, Thiab POLYGON, nrog rau spatial functions zoo li ST_Distance.

Thaum koj siv lub ST_Distance ua haujlwm hauv MySQL nrog cov ntaub ntawv thaj chaw sawv cev ua POINT coordinates, nws yuav siv sij hawm mus rau hauv tus account lub curvature ntawm lub ntiaj teb nto. Cov qauv kheej kheej siv los ntawm ST_Distance siv cov qauv Haversine. Qhov kev kwv yees no yog tsim rau feem ntau cov hom phiaj tab sis yuav qhia me ntsis qhov tsis raug rau qhov kev ncua deb heev.

Nov yog qhov koj tuaj yeem xam qhov nrug ntawm ob lub ntsiab lus siv Spatial Data Types:

  1. Tsim ib lub rooj nrog Spatial Data Type: Ua ntej, tsim ib lub rooj nrog a POINT kem khaws cov ntsiab lus ntawm thaj chaw. Piv txwv li:
CREATE TABLE locations (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255),
    coordinates POINT
);

Ntxig koj cov ntsiab lus ntawm thaj chaw rau hauv lub rooj no siv lub POINT tus tsim:

INSERT INTO locations (name, coordinates)
VALUES
    ('Point A', POINT(40.7128, -74.0060)), -- New York City
    ('Point B', POINT(34.0522, -118.2437)); -- Los Angeles
  1. Xam qhov nrug siv ST_Distance: Koj tuaj yeem xam qhov kev ncua deb ntawm ob lub ntsiab lus siv lub ST_Distance muaj nuj nqi. Nov yog cov lus nug piv txwv los xam qhov kev ncua deb ntawm ob lub ntsiab lus:
SELECT
    id1,
    id2,
    (ST_Distance(coordinates1, coordinates2) / 1609.344) AS distance_in_miles
FROM (
    SELECT
        l1.id AS id1,
        l2.id AS id2,
        l1.coordinates AS coordinates1,
        l2.coordinates AS coordinates2
    FROM
        locations l1,
        locations l2
    WHERE
        l1.id = 1 AND l2.id = 2
) AS distances;

Hloov 1 thiab 2 nrog tus IDs ntawm ob lub ntsiab lus koj xav xam qhov kev ncua deb ntawm.

  1. tshwm sim: Cov lus nug yuav rov qab qhov kev ncua deb ntawm ob lub ntsiab lus hauv mais.

Siv Spatial Data Types thiab cov ST_Distance muaj nuj nqi muab txoj hauv kev zoo dua thiab muaj tseeb los ua haujlwm nrog cov ntaub ntawv thaj chaw hauv MySQL. Nws kuj ua kom yooj yim xam qhov nrug ntawm cov ntsiab lus, ua kom yooj yim rau kev tswj thiab nug koj cov ntaub ntawv.

MySQL: Retrieving tag nrho cov ntaub ntawv nyob rau hauv ib tug ntau yam los ntawm xam qhov deb nyob rau hauv kilometers siv latitude thiab longitude

Yog vim ST_Distance rov qab qhov kev ncua deb hauv meters, yog li koj tsuas yog yuav tsum hloov kho cov lus nug rau mais:

SELECT
    id1,
    id2,
    (ST_Distance(coordinates1, coordinates2) / 1000) AS distance_in_kilometers
FROM (
    SELECT
        l1.id AS id1,
        l2.id AS id2,
        l1.coordinates AS coordinates1,
        l2.coordinates AS coordinates2
    FROM
        locations l1,
        locations l2
    WHERE
        l1.id = 1 AND l2.id = 2
) AS distances;

Microsoft SQL Server Geographic Distance: STDistance

Yog tias koj siv Microsoft SQL Server, lawv muab lawv tus kheej ua haujlwm, STDistance rau xam qhov kev ncua deb ntawm ob lub ntsiab lus siv Geography cov ntaub ntawv hom.

DECLARE @g geography;  
DECLARE @h geography;  
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);  
SET @h = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);  
SELECT @g.STDistance(@h);  

Lub kaus mom lub taub hau rau Manash Sahoo, tus tsim thiab tus kws kos duab laus ntawm Ion peb.

Douglas Karr

Douglas Karr yog CMO OpenINSIGHTS thiab tus founder ntawm lub Martech Zone. Douglas tau pab ntau ntau qhov kev vam meej MarTech startups, tau pab nyob rau hauv kev mob siab rau ntau tshaj $ 5 bil nyob rau hauv Martech nrhiav thiab kev nqis peev, thiab txuas ntxiv pab cov tuam txhab hauv kev siv thiab automating lawv cov kev muag khoom thiab kev lag luam cov tswv yim. Douglas yog tus paub thoob ntiaj teb kev hloov pauv digital thiab MarTech kws tshaj lij thiab hais lus. Douglas tseem yog tus sau phau ntawv Dummie phau ntawv qhia thiab phau ntawv ua lag luam.

lwm yam khoom

Rov qab mus rau sab saum toj
Close

Adblock nrhiav tau

Martech Zone muaj peev xwm muab cov ntsiab lus no rau koj yam tsis muaj nqi vim tias peb tau txais peb lub vev xaib los ntawm kev tshaj tawm cov nyiaj tau los, koom nrog kev sib txuas, thiab kev txhawb nqa. Peb yuav txaus siab yog tias koj yuav tshem koj cov ad blocker thaum koj saib peb lub xaib.