如何从JSON数据中使用经度和纬度查找最近的位置

编程入门 行业动态 更新时间:2024-10-28 21:30:08
本文介绍了如何从JSON数据中使用经度和纬度查找最近的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一个网站,询问用户的位置,然后使用GeoLocation从该位置查找最近的位置(半径100m半径),并以HTML显示结果.

I'm trying to make a website that ask for user's location then find the closest location (100m radius) from it's position using GeoLocation and display the result in HTML.

我尝试过的.

$.getJSON("places.json", function (data) { for (var i = 0; i < data.length; i++) { if ((data[i].lat - poslat) > 0.00200 || (data[i].lng - poslng) > 0.00200) { return data[i]; } html += '<p>' + data[i].location + ' - ' + data[i].code + '</p>'; $('#nearbystops').append(html); } });

places.json

places.json

[ { "code": "0001", "lat": "1.28210155945393", "lng": "103.81722480263163", "location": "Stop 1" }, { "code": "0003", "lat": "1.2777380589964", "lng": "103.83749709165197", "location": "Stop 2" }, { "code": "0002", "lat": "1.27832046633393", "lng": "103.83762574759974", "location": "Stop 3" } ]

先谢谢您! :)

推荐答案

要计算两个坐标之间的距离,不能只减去这些值.很好,但是可以为您提供正方形内的坐标.这可能是合适的,但是大多数人确实倾向于按半径搜索位置.此功能可以做到这一点...

To calculate distance between two co-ordinates, you can't just subtract the values. That's fine but it gives you the co-ordinates that are within a square. This may be suitable but mostly people do tend to want to search locations by radius. This function will do that...

function distance(lat1, lon1, lat2, lon2, unit) { var radlat1 = Math.PI * lat1/180 var radlat2 = Math.PI * lat2/180 var theta = lon1-lon2 var radtheta = Math.PI * theta/180 var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta); if (dist > 1) { dist = 1; } dist = Math.acos(dist) dist = dist * 180/Math.PI dist = dist * 60 * 1.1515 if (unit=="K") { dist = dist * 1.609344 } if (unit=="N") { dist = dist * 0.8684 } return dist }

这是我从这里复制的普通代码...

It's a common piece of code which I copied from here...

www.geodatasource/developers/javascript

这里是您的示例中使用的...

And here it is, used in your example...

function distance(lat1, lon1, lat2, lon2, unit) { var radlat1 = Math.PI * lat1/180 var radlat2 = Math.PI * lat2/180 var theta = lon1-lon2 var radtheta = Math.PI * theta/180 var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta); if (dist > 1) { dist = 1; } dist = Math.acos(dist) dist = dist * 180/Math.PI dist = dist * 60 * 1.1515 if (unit=="K") { dist = dist * 1.609344 } if (unit=="N") { dist = dist * 0.8684 } return dist } var data = [{ "code": "0001", "lat": "1.28210155945393", "lng": "103.81722480263163", "location": "Stop 1" }, { "code": "0003", "lat": "1.2777380589964", "lng": "103.83749709165197", "location": "Stop 2" }, { "code": "0002", "lat": "1.27832046633393", "lng": "103.83762574759974", "location": "Stop 3" }]; var html = ""; var poslat = 1.28210155945393; var poslng = 103.81722480263163; for (var i = 0; i < data.length; i++) { // if this location is within 0.1KM of the user, add it to the list if (distance(poslat, poslng, data[i].lat, data[i].lng, "K") <= 0.1) { html += '<p>' + data[i].location + ' - ' + data[i].code + '</p>'; } } $('#nearbystops').append(html);

<script src="ajax.googleapis/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="nearbystops"></div>

更多推荐

如何从JSON数据中使用经度和纬度查找最近的位置

本文发布于:2023-10-30 08:54:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:经度   纬度   位置   数据   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!