Javascript 如何根据位置打印某些元素?

Javascript 如何根据位置打印某些元素?,javascript,geolocation,Javascript,Geolocation,我有一个获取用户当前位置的javascript函数,另一个函数在一些特定坐标之间的随机位置生成自行车 我想在控制台日志中打印出最近的5(或例如,所有长度小于0.05且距离较远的自行车)。我不确定从何处开始,有人有想法吗 这是我的javascript代码 底部的for each循环是我如何尝试的,但我不确定如何访问position const if(navigator.geolocation){ //Location is supported by the browser const lo

我有一个获取用户当前位置的javascript函数,另一个函数在一些特定坐标之间的随机位置生成自行车

我想在控制台日志中打印出最近的5(或例如,所有长度小于0.05且距离较远的自行车)。我不确定从何处开始,有人有想法吗

这是我的javascript代码

底部的for each循环是我如何尝试的,但我不确定如何访问position const

if(navigator.geolocation){
  //Location is supported by the browser
  const locationElement = document.getElementById("location")
  navigator.geolocation.getCurrentPosition((position) => {
    console.log(position)
    locationElement.innerText = `${position.coords.latitude},${position.coords.longitude}`
  })
}
  
  class Bike{
    constructor(name, location){
      this.name = name;
      this.location = location;
    }

  }
  
  class Location {
    constructor(lat, lon) {
      this.lat = lat;
      this.lon = lon
    }
  }

  const bikes = [];

  for(let i= 1; i<30; i++) {
    const name = `CityBike${i}`
    const location = new Location(Math.random()/10 + 55.5, Math.random()/10 + 12.5)
    
    const bike = new Bike(name, location)
    
    bikes.push(bike)
  }
  
  bikes.forEach((bike) => {
    
    if((bike.location - position) < 0.1) {
      console.log(bike)
    }
  })





if(navigator.geolocation){
//浏览器支持位置
const locationElement=document.getElementById(“位置”)
navigator.geolocation.getCurrentPosition((位置)=>{
控制台日志(位置)
locationElement.innerText=`${position.coords.latitude},${position.coords.latitude}`
})
}
职业自行车{
构造函数(名称、位置){
this.name=名称;
这个位置=位置;
}
}
类位置{
建造商(lat、lon){
this.lat=lat;
this.lon=lon
}
}
常数=[];
for(设i=1;i{
如果((自行车位置-位置)<0.1){
控制台日志(自行车)
}
})

由于获取位置是一个异步操作,您需要在getCurrentPosition的回调方法中检查位置是否在范围内。此外,我认为最好使用当前位置作为圆的中心坐标,根据您要走的距离选择半径,画一个圆,并检查另一辆自行车是否在范围内位置是否在圆内。 现在,我刚刚更改了代码,使其能够用于比较Longitude和latitude

if (navigator.geolocation) {
      //Location is supported by the browser
      //const locationElement = document.getElementById("location")
      navigator.geolocation.getCurrentPosition((position) => {
        console.log(`${position.coords.latitude},${position.coords.longitude}`);
        const myLocation = new Location(position.coords.latitude, position.coords.longitutde);
        bikes.forEach((bike) => {
    
          if ((myLocation.lat - bike.location.lat) < 0.1 && (myLocation.lon - bike.location.lon) < 0.1) {
            console.log(bike);
          }
        })
        //locationElement.innerText = `${position.coords.latitude},${position.coords.longitude}`
      })
    }
    
    class Bike {
      constructor(name, location) {
        this.name = name;
        this.location = location;
      }
    
    }
    var bikes = [];
    class Location {
      constructor(lat, lon) {
        this.lat = lat;
        this.lon = lon
      }
    }
    
    for (let i = 1; i < 3; i++) {
      const name = `CityBike${i}`
      const location = new Location(Math.random() / 10 + 55.5, Math.random() / 10 + 12.5)
      console.log(location);
      const bike = new Bike(name, location)
    
      bikes.push(bike)
    }
if(navigator.geolocation){
//浏览器支持位置
//const locationElement=document.getElementById(“位置”)
navigator.geolocation.getCurrentPosition((位置)=>{
log(${position.coords.latitude},${position.coords.longitude});
const myLocation=新位置(position.coords.lation,position.coords.longitude);
bikes.forEach((bike)=>{
如果((myLocation.lat-bike.location.lat)<0.1&(myLocation.lon-bike.location.lon)<0.1){
控制台。日志(自行车);
}
})
//locationElement.innerText=`${position.coords.latitude},${position.coords.latitude}`
})
}
职业自行车{
构造函数(名称、位置){
this.name=名称;
这个位置=位置;
}
}
var=[];
类位置{
建造商(lat、lon){
this.lat=lat;
this.lon=lon
}
}
for(设i=1;i<3;i++){
const name=`CityBike${i}`
常量位置=新位置(Math.random()/10+55.5,Math.random()/10+12.5)
控制台日志(位置);
const bike=新自行车(名称、位置)
推(自行车)
}