Reputation: 5551
I need to implement the prompt dialog for device geographical location in web browsers and I'm kinda struggling on how to do it. I have this JavaScript code:
if (navigator.geolocation) {
// Prompt for permision
navigator.geolocation.getCurrentPosition()
}
else {
alert('Please allow your location in order for app to work
properly.');
}
but getCurrentPosition
requires at least 1 argument and I can't seem to get it working.
All the JavaScript code that I've found online worked just fine, but all of them were writing in the console Your latitude is:
and Your longitude is:
but I don't need that.
I need for web browser to just remember the geographical location so I can use the coordinates later in Foursquare API. I don't need for the browser to write anything in the web browser's console.
Upvotes: 1
Views: 2828
Reputation: 943108
but getCurrentPosition requires at least 1 argument and I can't seem to get it working.
So you need an example of how to pass a suitable argument.
All the JavaScript code that I've found online worked just fine, but all of them were writing in the console Your latitude is: and Your longitude is: but I don't need that.
So you have examples of how to pass an argument. That argument being a function which does something with the position. You just don't like what it does with the position.
Use that example.
Edit the function so that the lines which log the information do whatever you want to do with the data instead.
Here is the version from MDN:
var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }; function success(pos) { var crd = pos.coords; console.log('Your current position is:'); console.log(`Latitude : ${crd.latitude}`); console.log(`Longitude: ${crd.longitude}`); console.log(`More or less ${crd.accuracy} meters.`); }; function error(err) { console.warn(`ERROR(${err.code}): ${err.message}`); }; navigator.geolocation.getCurrentPosition(success, error, options);
Upvotes: 5