<h1>Detecting the physical location by IP Address <b>OR</b> HTML5 geolocation</h1>
<p>This example uses Vue, Axios, and my new cloudflare workers API to get your location. To get your physical location using your device's GPS or its IP address, click one of the two buttons, below.</p>
<div id="app">
<p><button v-on:click="geolocation()">Use GPS to update position</button></p>
<p><button v-on:click="showPositionByIP()">Use IP to update position</button></p>
It looks like you are currently at:<br /><br />
{{details.mqStreet}}<br />
{{details.mqCity}}, {{details.mqState}} {{details.mqZip}}<br />
</div>
new Vue({
el: "#app",
data() {
return {
details: {
latitude: null,
longitude: null,
cfCountry: null,
cfCity: null,
cfLatitude: null,
cfLongitude: null,
cfPostalcode: null,
cfRegion: null,
cfRegioncode: null,
cfTimezone: null,
mqStreet: null,
mqCity: null,
mqState: null,
mqZip: null
}
};
},
methods: {
geolocation: function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.showPosition);
} else {
console.log("Geolocation is not supported by this browser.");
}
},
showPosition: function(position) {
// Use Axios to make the API Call
axios
.get("https://geolocation-json.joe.workers.dev/?latitude="+position.coords.latitude+"&longitude="+position.coords.longitude)
.then(response=> (this.details = response.data));
},
showPositionByIP: function() {
// Use Axios to make the API Call
axios
.get("https://geolocation-json.joe.workers.dev/")
.then(response=> (this.details = response.data));
}
}
});
This Pen doesn't use any external CSS resources.