我正在做React应用程序来获得最近的加油站,应用程序应该能够在地图上显示最近的加油站在哪里取决于位置,我得到了一个部分,我可以获得某人的纬度和经度,但不知道如何继续,如果有人能够帮助它,我会很高兴,这是我的代码获取经度和纬度
import React from 'react';
import './App.css';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
};
this.getLocation = this.getLocation.bind(this);
this.getCoordinates=this.getCoordinates.bind(this)
}
getLocation(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.getCoordinates);
} else {
alert("Geolocation is not supported by this browser.");
}
}
getCoordinates(position){
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude
})
}
render() {
return (
<div classname="App">
<h2>
Location</h2>
<button onClick={this.getLocation}>Show coordinates</button>
<h4>Coordinates</h4>
<p>Latitude:{this.state.latitude}</p>
<p>longitude:{this.state.longitude}</p>
</div>
)
}
}
export default App; `发布于 2021-04-30 21:38:43
您需要使用来自用户的输入经度+经度,然后计算每个加油站的距离(假设您有一个这样的数据库)。
例如,在快速搜索中,会有一些关于这方面的内容:
Calculate distance between two latitude-longitude points? (Haversine formula)
在这个问题上最受欢迎的答案是:https://stackoverflow.com/a/27943/1552160
https://stackoverflow.com/questions/67334816
复制相似问题