Get coords and api data

mail@pastecode.io avatar
unknown
javascript
2 years ago
980 B
1
Indexable
Never
<template>
  <div>
    <p>Latitude: {{ latitude }}</p>
    <p>Longitude: {{ longitude }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      latitude: null,
      longitude: null
    }
  },
  created() {
    this.getLocation()
  },
  methods: {
    getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position => {
          this.latitude = position.coords.latitude
          this.longitude = position.coords.longitude
          this.callApi()
        })
      } else {
        // Geolocation is not supported by the browser
      }
    },
    callApi() {
      const apiUrl = `https://example.com/api?lat=${this.latitude}&lon=${this.longitude}`
      // Make an API call using the fetch function or another HTTP library
      // For example:
      fetch(apiUrl)
        .then(response => response.json())
        .then(data => {
          // Use the data from the API
        })
    }
  }
}
</script>