如何制作“ xx公里”来自位置、地理/谷歌 API

发布于 2024-12-11 11:57:15 字数 682 浏览 0 评论 0原文

好的,我有一份餐厅名单。所有餐厅都有一个地址,我想这样做,您可以在输入字段中写一个位置,然后它将显示距离最近的餐厅列表,然后按降序排列。示例:

搜索:Valby(位于的城市)丹麦)

Restaurant Ankrara - 2 km from Valby
Restaurant JuicyFood - 3 km from Valby
Restaurant FoodJuicy - 5 km from Valby
Restaurant Blabla - 22 km from Valby

经过对 SO 的研究和提问,我发现我需要使用地理编码和谷歌地图 api。

好的,现在我已经将您输入的内容(上例中的城市瓦尔比)转换为地理编码(纬度/经度坐标)。

然后我就有了坐标中的地址。

如何计算城市与餐厅地址之间的公里数坐标?

例如,这里是城市“Valby”的绳索:

55.662133, 12.508028

这是 3 家餐厅的地址绳索:(

55.667029, 12.527715
55.6939821, 12.4934945
55.6696885, 12.3755492

我如何以最近/最近的方式显示它们,然后降序 <- 不需要接受的答案,我最终做出这是一个新问题)

Ok I have a restaurant list. All the restaurants have a adresses, and I would like to do so you can write a location in a input field, and then it will show the restaurant list with the closest first and then descending.. example:

Searching for: Valby (city in Denmark)

Restaurant Ankrara - 2 km from Valby
Restaurant JuicyFood - 3 km from Valby
Restaurant FoodJuicy - 5 km from Valby
Restaurant Blabla - 22 km from Valby

After research and question here on SO, I found out I need to use geocoding and google maps api.

Okay, so I got to the step where I have converted what you entered (the city Valby in above example), into geocode (lat/lng coordinates).

And then i have the adresses in the coordinates to.

How can i count the kilometers between the City and the restaurant address in coordinates?

For the example, here is the cords of the city "Valby":

55.662133, 12.508028

And here is 3 restaurants addresses cords:

55.667029, 12.527715
55.6939821, 12.4934945
55.6696885, 12.3755492

(And how can i show them with closest/nearest first and then descending <- not required for accepted answer, I eventually make a new question for this)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2024-12-18 11:57:15

您可以计算距离:

var pos = new google.maps.LatLng(55.662133, 12.508028);

var r1 = {name: "rest1", lat: 55.667029, lng: 12.527715, dist: 0};
var r2 = {name: "rest2", lat: 55.6939821, lng: 12.4934945, dist: 0};
var r3 = {name: "rest3", lat: 55.6696885, lng: 12.3755492, dist: 0};

var r = [r3, r2, r1];
for (var i = 0; i < r.length; ++i) {
    var p = new google.maps.LatLng(r[i].lat, r[i].lng);
    var d = google.maps.geometry.spherical.computeDistanceBetween(pos, p);
    r[i].dist = d;
}

然后对它们进行排序:

r.sort(function(r1, r2) {return r1.dist - r2.dist;});

有关详细信息,请参阅在 jsfiddle 上运行源代码

You can compute the distances:

var pos = new google.maps.LatLng(55.662133, 12.508028);

var r1 = {name: "rest1", lat: 55.667029, lng: 12.527715, dist: 0};
var r2 = {name: "rest2", lat: 55.6939821, lng: 12.4934945, dist: 0};
var r3 = {name: "rest3", lat: 55.6696885, lng: 12.3755492, dist: 0};

var r = [r3, r2, r1];
for (var i = 0; i < r.length; ++i) {
    var p = new google.maps.LatLng(r[i].lat, r[i].lng);
    var d = google.maps.geometry.spherical.computeDistanceBetween(pos, p);
    r[i].dist = d;
}

and then sort them:

r.sort(function(r1, r2) {return r1.dist - r2.dist;});

For details see the running source code on jsfiddle.

把昨日还给我 2024-12-18 11:57:15

您可以在服务器上计算距离。请参阅文章使用 PHP、MySQL 和 PHP 创建商店定位器谷歌地图

You can compute the distances on your server. See article Creating a Store Locator with PHP, MySQL & Google Maps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文