简体   繁体   English

使用google gps api和php获取两点之间的距离

[英]getting distance between two points using google gps api with php

I found this link on Stack Overflow Google Maps - How to get the distance between two point in metre? 我在Stack Overflow Google Maps上找到了此链接-如何获取两点之间的距离以米为单位? which shows the following solution in regards to finding a km distance given two different latitude and longitude values: 在给出两个不同的纬度和经度值的情况下,对于找到km距离,它显示了以下解决方案:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);

alert(calcDistance(p1, p2));

//calculates distance between two points in km's
function calcDistance(p1, p2){
    return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
   }

}

This is great and works however I am creating an app that pulls lat and long values from a database and compares each one to a constant lat and longitude value to determine if the users from which I am pulling from the db are within a certain range of a set lat and long. 这很棒,并且可以运行,但是我正在创建一个应用程序,该应用程序从数据库中提取经度和纬度值,并将每个值与一个恒定的经度和纬度值进行比较,以确定我从数据库中提取的用户是否在某个范围内既定纬度又长。

I'm doing this in PHP but I want to pass the constant lat and long values along with the current lat and long values to the above JavaScript function, calculate the distance and then return the value to a PHP variable. 我在PHP中进行此操作,但我想将常量lat和long值以及当前lat和long值传递给上述JavaScript函数,计算距离,然后将值返回给PHP变量。 In other words I want to call a JavaScript function in PHP and return the JavaScript value back to the PHP variable. 换句话说,我想在PHP中调用JavaScript函数并将JavaScript值返回给PHP变量。

I'm seeing a lot of forums that state this is impossible then some say it is. 我看到很多论坛都说这不可能,然后有人说是不可能。 Can anyone help me with this or suggest another approach? 谁能帮我这个忙或建议另一种方法? will I just have to do some old fashioned calculus within PHP and forget the Google API plugin? 我是否只需要在PHP中进行一些老式的演算,而忘记Google API插件?

You can use below query if you have latitude and longitude value in your database. 如果数据库中具有经度和纬度值,则可以使用以下查询。

SELECT a.*,
            3956 * 2 * ASIN(SQRT( POWER(SIN(($lat - lat) * pi()/180 / 2), 2) + COS($lat * pi()/180) * COS(lat * pi()/180) *
            POWER(SIN(($long - longi) * pi()/180 / 2), 2) )) as
            distance FROM table
            GROUP BY id HAVING distance <= 500 ORDER by distance ASC

$lat and $long variable is the current position of user. $ lat和$ long变量是用户的当前位置。 lat and longi is the latitude and longitudle of entries. lat和longi是条目的纬度和经度。

Also refer to " Creating a Store Locator with PHP, MySQL & Google Maps " to understand how above query will work 另请参阅“ 使用PHP,MySQL和Google Maps创建商店定位器 ”以了解上述查询的工作方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM