简体   繁体   English

如何根据用户纬度和经度显示最近位置列表

[英]How to display list of closest locations based on user latitude and longitude

How can I display the closest three locations in ascending order of distance?如何按距离升序显示最近的三个位置?

I'm able to display the single closest location with the code below, but am running into a roadblock when it comes to displaying the second the third closest locations.我可以使用下面的代码显示单个最近的位置,但是在显示第二个第三个最近的位置时遇到了障碍。

<div class="container p-3">
<button onclick="getLocation()" class="mb-2">Find city nearest you</button>
<p id="closest">
</p>
</div>

var x = document.getElementById("closest");

var storeLocations = {
    "Atlanta": {latitude: 33.7489954, longitude: -84.3879824},
  "Chicago": {latitude: 41.8781136, longitude: -87.6297982},
    "Dallas": {latitude: 32.7766642, longitude: -96.7969879},
  "Houston": {latitude: 29.7604267, longitude: -95.3698028},
    "Los Angeles": {latitude: 34.0522342, longitude: -118.2436849},
    "New York": {latitude: 40.7127837, longitude: -74.0059413},
    "Philadelphia": {latitude: 39.9525839, longitude: -75.1652215},
    "Phoenix": {latitude: 33.4483771, longitude: -112.0740373},
    "Seattle": {latitude: 47.6062095, longitude: -122.3320708}
}

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(findNearestStore);
    } else {
        x.innerHTML = "Location: Washington, D.C.";
    }
}

function findNearestStore(position) {
        var nearestStore = geolib.findNearest({latitude: position.coords.latitude, longitude: position.coords.longitude}, storeLocations);
        x.innerHTML = "Location: " + nearestStore.key;
}

Codepen: https://codepen.io/Codewalker/pen/LwWqrE代码笔: https ://codepen.io/Codewalker/pen/LwWqrE

Use the orderByDistance function, combined with Array.slice as below:使用orderByDistance函数,结合Array.slice如下:

var nearest3Stores = 
geolib
  .orderByDistance({
    latitude: position.coords.latitude, 
    longitude: position.coords.longitude
  }, storeLocations)
  .slice(0, 3);

Source 来源

Edit: You will get the 3 nearest locations using the above method,but to print them you need to loop over them since you get an array.编辑:您将使用上述方法获得 3 个最近的位置,但要打印它们,您需要循环遍历它们,因为您获得了一个数组。 No need to use .slice() again.无需再次使用.slice() Like this:像这样:

y.innerHTML = "Locations: ";
nearest3Stores.forEach(function(store){
  y.innerHTML+= store.key + " ";
})

Here is a codepen showing the results.这是一个显示结果的代码笔

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

相关问题 如何根据两个位置的经度和纬度获得距离? - How to get the distance between two locations based on their latitude and longitude? 如何从基于经纬度的位置列表中显示Google地图上最近的位置 - How to display closest location on google map from a list of locations based on lat and lng 如何基于xml格式的纬度和经度在Google地图中显示 - How to display in google maps based on our latitude and longitude in xml format 如何在 Node.js 中根据纬度和经度设置自定义位置? - How do I set custom locations based on latitude and longitude in Node.js? 选择最接近经度和纬度的记录 - Select records with closest Longitude and Latitude 我如何选择经度和纬度以返回特定位置 javascript - how do i select the longitude and latitude to return specific locations javascript 如何在Google地图上显示多个经度和纬度 - How to display multiple Latitude and Longitude on Google Map 如何在具有经度和纬度的iframe中显示地图 - how to display map in iframe having latitude and longitude 如何基于“文本框”值获取纬度和经度 - How to get latitude and Longitude based in Textbox value 如何在模型中获取用户的经纬度 - How to get the user latitude and longitude in model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM