获得两点之间的距离使用谷歌GPS与PHP的API [英] getting distance between two points using google gps api with php

查看:175
本文介绍了获得两点之间的距离使用谷歌GPS与PHP的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个链接堆栈溢出 Google Maps - 如何获得两点之间的距离?,它给出了以下两种不同经纬度值查找公里距离的解决方案:

 < script type =text / javascriptsrc =http://maps.google.com/maps/api/js?sensor=false&libraries=geometry >< /脚本> 

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

alert(calcDistance(p1,p2));

//计算km中
函数的两点之间的距离calcDistance(p1,p2){
return(google.maps.geometry.spherical.computeDistanceBetween(p1,p2)/ 1000).toFixed(2);
}

}

这很棒,我正在创建一个应用程序,它可以从数据库中提取纬度和长度值,并将每个值与经常的经度和纬度值进行比较,以确定我从数据库中拉出的用户是否处于经度和纬度的一定范围内。 / p>

我在PHP中这样做,但我想将常数lat和long值以及当前的lat和long值传递给上述JavaScript函数,计算距离并然后将该值返回给PHP变量。换句话说,我想在PHP中调用JavaScript函数,并将JavaScript值返回给PHP变量。



我看到很多论述这一点的论坛那么有些人说这是不可能的。任何人都可以帮助我或建议另一种方法?我只需要在PHP中做一些老式的微积分并忘记Google API插件?解析方案

您可以使用下面的查询if您的数据库中有经度和纬度值。

  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
FROM table
GROUP BY id HAVING distance <= 500 ORDER by distance ASC

$ lat和$ long变量是用户的当前位置。 lat和longi是条目的纬度和高度。



另请参阅使用PHP,MySQL和Google Maps创建商店定位器了解上述查询的工作方式。


I found this link on Stack Overflow Google Maps - How to get the distance between two point in metre? which shows the following solution in regards to finding a km distance given two different latitude and longitude values:

<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. In other words I want to call a JavaScript function in PHP and return the JavaScript value back to the PHP variable.

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?

解决方案

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 and longi is the latitude and longitudle of entries.

Also refer to "Creating a Store Locator with PHP, MySQL & Google Maps" to understand how above query will work

这篇关于获得两点之间的距离使用谷歌GPS与PHP的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆