Coder Social home page Coder Social logo

Comments (6)

eregnier avatar eregnier commented on July 19, 2024

Hey polsola.
I saw your issue just now, sorry.
I don't know a lot nuxt, but indeed it seems there is issues related to nuxt due to the underlaying gmap library of this component. there is a small trick in the link on the README that gives a hint how to solve it. I cannot do any thing more for you on this topic.

image

Please let me know if this solves your issue.

from vue2-gmap-custom-marker.

eregnier avatar eregnier commented on July 19, 2024

I also thing you issue may be due to SSR that doesn't compute marker on gmap properly.
You may try to render them on client side for a test and if it is related to SSR, you may share me any technical detail that would help to solve your issue.

from vue2-gmap-custom-marker.

polsola avatar polsola commented on July 19, 2024

Hello Eric,
Do not worry, thanks for the help!
I've tried now to import it without plugin and with the workaround, with this there's no error, but the position still doesn't refresh, here's how it looks now:

<template>
<div>
  <GmapMap
    :center="center"
    :zoom="8"
    style="width: 100%; height: 600px"
    >
    <gmap-custom-marker 
        :key="vehicle.id"
        v-for="vehicle in vehicles"
        :marker="vehicle.position"
    >
        <div class="vehicle-marker">
            <img :src="vehicle.avatar" />
        </div>
    </gmap-custom-marker> 
</GmapMap>
</div>
</template>

<style lang="scss">

.vehicle-marker {
    width: 100px;
    height: 100px;
    background-color: red;
    img {
        width: 64px;
        display: block;
    }
}
    
</style>

<script>

import * as VueGoogleMaps from 'vue2-google-maps'
import GmapCustomMarker from 'vue2-gmap-custom-marker'
import Vue from 'vue'

Vue.use(VueGoogleMaps, {
  load: {
    key: '#'
  }
})

export default {
    layout: 'ridereact',
    components: {
      'gmap-custom-marker': GmapCustomMarker
    },
    head() {
        return {
        title: `${this.$t('menu.map')}`,
        }
    },
    data() {
        return {
            center: {lat:41.385063, lng:2.173404},
            vehicles: [
                {
                    id: 1,
                    avatar: '#',
                    position: {
                        lat: 41.385063,
                        lng: 2.173404
                    }
                },{
                    id: 2,
                    avatar: '#',
                    position: {
                        lat: 41.930290,
                        lng: 2.254350
                    }
                }
            ]
        }
    },
    mounted() {
        setInterval(function () {
            this.vehicles.forEach(vehicle => {
                var newLat = vehicle.position.lat += (Math.floor(Math.random() * 11) >= 6) ? 0.005 : -0.005
                var newLng = vehicle.position.lng += (Math.floor(Math.random() * 11) >= 6) ? 0.005 : -0.005
                
                vehicle.position.lat = newLat
                vehicle.position.lng = newLng
                console.log('Vehicle', vehicle.id, vehicle.position.lat, vehicle.position.lng)
            });
            }.bind(this), 2000)
    }
}
</script>

I'm gonna try to create the same without Nuxt.js to see if this works as expected

from vue2-gmap-custom-marker.

polsola avatar polsola commented on July 19, 2024

Hello again Eric,

Weird thing, if I set both of the markers (normal & custom) the position updates correctly on both, here's the template now:

  <GmapMap
    :center="center"
    :zoom="8"
    style="width: 100%; height: 600px"
    >
    <GmapMarker 
        :key="`vehicle-${vehicle.id}`"
        v-for="vehicle in vehicles"
        :position="vehicle.position"
     />
    <gmap-custom-marker 
        :key="vehicle.id"
        v-for="vehicle in vehicles"
        :marker="vehicle.position"
        :offsetY="-10"
    >
        <div class="vehicle-marker">
            <img :src="vehicle.avatar" />
        </div>
    </gmap-custom-marker> 
</GmapMap>

from vue2-gmap-custom-marker.

eregnier avatar eregnier commented on July 19, 2024

I am back from holiday, I'll have a look into your issue soon.

from vue2-gmap-custom-marker.

eregnier avatar eregnier commented on July 19, 2024

Hey @polsola ,

Here is a working code

<template>
    <div>
        {{vehicles}}
        <GmapMap :center="center" :zoom="8" style="width: 100%; height: 600px">
            <gmap-custom-marker
                :key="vehicle.id"
                v-for="vehicle in vehicles"
                :marker="vehicle.position"
            >
                <div class="vehicle-marker">
                    <img :src="vehicle.avatar" />
                </div>
            </gmap-custom-marker>
        </GmapMap>
    </div>
</template>

<style>
.vehicle-marker {
    width: 100px;
    height: 100px;
    background-color: red;
}
.vehicle-marker img {
    width: 64px;
    display: block;
}
</style>

<script>
import * as VueGoogleMaps from "vue2-google-maps";
import GmapCustomMarker from "vue2-gmap-custom-marker";
import Vue from "vue";

Vue.use(VueGoogleMaps, {
    load: {
        key: '#'
    }
});

export default {
    layout: "ridereact",
    components: {
        "gmap-custom-marker": GmapCustomMarker
    },
    head() {
        return {
            title: `${this.$t("menu.map")}`
        };
    },
    data() {
        return {
            center: { lat: 41.385063, lng: 2.173404 },
            vehicles: [
                {
                    id: 1,
                    avatar: "#",
                    position: {
                        lat: 41.385063,
                        lng: 2.173404
                    }
                },
                {
                    id: 2,
                    avatar: "#",
                    position: {
                        lat: 41.93029,
                        lng: 2.25435
                    }
                }
            ]
        };
    },
    mounted() {
        setInterval(
            function() {
                this.vehicles.forEach(vehicle => {
                    var newLat = vehicle.position.lat +(Math.floor(Math.random() * 11) >= 6 ? 0.05 : -0.05)
                    var newLng = vehicle.position.lng + (Math.floor(Math.random() * 11) >= 6 ? 0.05 : -0.05)

                    vehicle.position = {
                        lat: newLat,
                        lng: newLng
                    }
                    console.log(
                        "Vehicle",
                        vehicle.id,
                        vehicle.position.lat,
                        vehicle.position.lng
                    );
                });
            }.bind(this),
            2000
        );
    }
};
</script>

I just assigned an object reference to the marker position instead of setting individual raw position number values. This let vue reactive system continue to work properly.

Also, don't forget to clean your set interval in a vue destroy method for exemple if you want to avoid strange behaviors and memory leaks.

Hope this helps

from vue2-gmap-custom-marker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.