0

I am doing React application for getting me closest gas station, application should be able to show on map where is closest gas station depending on location, I got to a part where I can get latitude and longitude of someone but have no idea how to continue, Would love it if someone is able to help, Here is my code for getting latitude and longitude

     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;    `

Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30

1 Answers1

0

You'll need to use the input lat + lon from the user and then calculate the distance against each gas station lat + lon (assuming you have a database of this of some kind).

There will be something on SO for this, for example from a quick search:

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

Most upvoted answer on that particular one: https://stackoverflow.com/a/27943/1552160

Stu
  • 4,160
  • 24
  • 43