首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用react获得最近的加油站

使用react获得最近的加油站
EN

Stack Overflow用户
提问于 2021-04-30 21:31:16
回答 1查看 31关注 0票数 0

我正在做React应用程序来获得最近的加油站,应用程序应该能够在地图上显示最近的加油站在哪里取决于位置,我得到了一个部分,我可以获得某人的纬度和经度,但不知道如何继续,如果有人能够帮助它,我会很高兴,这是我的代码获取经度和纬度

代码语言:javascript
复制
     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;    `
EN

回答 1

Stack Overflow用户

发布于 2021-04-30 21:38:43

您需要使用来自用户的输入经度+经度,然后计算每个加油站的距离(假设您有一个这样的数据库)。

例如,在快速搜索中,会有一些关于这方面的内容:

Calculate distance between two latitude-longitude points? (Haversine formula)

在这个问题上最受欢迎的答案是:https://stackoverflow.com/a/27943/1552160

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67334816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档