Vue2leaflet: How can i get current location

Created on 26 Nov 2019  路  2Comments  路  Source: vue-leaflet/Vue2Leaflet

leaflet version : 2.2.1

Hi

How can i get current location ? sorry i didn't find an example for this.

question

Most helpful comment

@Mateo111001 You can use the leaflet location function, keep in mind you can always access any leaflet function trough the mapObject property on LMap or any other component. about locate you can do as follows:

<l-map @ready="onReady" ...  @locationfound="onLocationFound" .../>
methods: {
onReady (mapObject) {
  mapObject.locate();
},
onLocationFound(location){
  console.log(location)
}
}

for all the options and return format please follow: https://leafletjs.com/reference-1.6.0.html#map-locate

This would be an awesome addition to our faq? would you like to make a PR with it? :)

All 2 comments

@Mateo111001 You can use the leaflet location function, keep in mind you can always access any leaflet function trough the mapObject property on LMap or any other component. about locate you can do as follows:

<l-map @ready="onReady" ...  @locationfound="onLocationFound" .../>
methods: {
onReady (mapObject) {
  mapObject.locate();
},
onLocationFound(location){
  console.log(location)
}
}

for all the options and return format please follow: https://leafletjs.com/reference-1.6.0.html#map-locate

This would be an awesome addition to our faq? would you like to make a PR with it? :)

I wanted to show a full example of what worked for me, showing a circle with my location onLocationFound and the radius with a semi-transparent background (simulating google maps)

<template>
    <l-map @ready="onReady" ref="map" @locationfound="onLocationFound">
        <template v-if="location">
             <l-circle-marker :lat-lng="location.latlng" :fillOpacity="1" :radius="0.1" />
             <l-circle-marker :lat-lng="location.latlng" :radius="location.accuracy/2" :stroke="false" />
         </template>
    </l-map>
<script>
export default {
   data() {
       map: null,
       location: null
    },
    methods: {
        onReady() {
            this.map = this.$refs["map"].mapObject;
            this.map.locate();
        },
        onLocationFound(l) {
            console.log(l);
            this.location = l;
        },
    }
</script>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

shtw picture shtw  路  5Comments

mnboos picture mnboos  路  3Comments

rhoseno98 picture rhoseno98  路  4Comments

mpallante picture mpallante  路  4Comments

srtonz picture srtonz  路  4Comments