计算给定2个点,纬度和经度的距离

Calculate distance given 2 points, latitude and longitude(计算给定2个点,纬度和经度的距离)

本文介绍了计算给定2个点,纬度和经度的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
MySQL 经纬度表设置

我知道这个问题可能已经被问过很多次了,我已经研究了很多,我需要特定事情的帮助.

I know this question has probably been asked many times, I've researched a lot and I need help with specific thing.

假设我有一个表单,用户输入经度和纬度,并且我有一个包含经度和纬度表的数据库,我将如何在该表中搜索半径 15 英里内的一个或多个点?

Lets say I have a form and user enters longitude and latitude, and I have a database which has table containing longitudes and latitudes, how would I search for a point or points in that table that are within 15 miles of radius?

推荐答案

您可以使用计算两点之间距离的公式.例如:

You can use the formula that calculates distances between two points. For example:

function get_distance($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') { 
    $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 'Mi': 
            break; 
        case 'Km' : 
            $distance = $distance * 1.609344; 
    } 
    return (round($distance,2)); 
}

您还可以执行以下操作:

You can also do something like:

$query = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * 
            sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * 
            cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)* 
            pi()/180))))*180/pi())*60*1.1515
        ) as distance 
        FROM `MyTable` 
        HAVING distance >= ".$distance.";

这篇关于计算给定2个点,纬度和经度的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:计算给定2个点,纬度和经度的距离

基础教程推荐