tomchentw / react-google-maps

React.js Google Maps integration component
https://tomchentw.github.io/react-google-maps/
MIT License
4.63k stars 937 forks source link

How can get current location (latitude, longitude)? #915

Open Dsantacruz opened 6 years ago

Dsantacruz commented 6 years ago

Getting Help

Before doing this, did you:

  1. Read the documentation
  2. Read the source code

You can get someone's help in three ways:

  1. Ask on StackOverflow with a google-maps tag or use react-google-maps as a keyword
  2. Ask in the chat room
  3. Create a Pull Request with your solutions to your problem

Please, be noted, no one, I mean, no one, is obligated to help you in ANY means. Your time is valuable, so does our contributors. Don't waste our time posting questions like “how do I do X with React-Google-Maps” and “my code doesn't work”. This is not the primary purpose of the issue tracker. Don't abuse.

shethmanan commented 5 years ago

You can simply use navigator object to do this, no need to use react-google-maps. Here is the code: navigator.geolocation.getCurrentPosition(callBackMethodName);

function callBackMethodName(location) console.log(location)

dig deeper: https://www.w3schools.com/html/html5_geolocation.asp

thangld8 commented 5 years ago

@shethmanan Hi mate, I have tried to get current location that use

if (navigator && navigator.geolocation) { navigator.geolocation.getCurrentPosition(pos => { console.log(443322, pos) const coords = pos.coords; this.setState({ currentLocation: { lat: coords.latitude, lng: coords.longitude } }); }); }

but it cannot get current location. I'm using macos with chrome