cordova-plugin-googlemaps icon indicating copy to clipboard operation
cordova-plugin-googlemaps copied to clipboard

Can't update marker position with setPosition

Open artnicbr opened this issue 9 years ago • 13 comments

I am trying to make my App auto update position of markers loading from an API. Everything is fine except that the setPosition function doesn't move my Markers. Bellow is the last try I made, forcing the coordinates to update by 0.1 each loop:

let pos: ILatLng = {
                lat: this.me._x,
                lng: this.me._y
            }            
            let opt: MarkerOptions = {
                title: 'Testing',
                position: pos,
                icon: 'blue'
            }
            this.map.addMarker(opt).then((marker) => { 
                this.marker = marker;
            }, (error) => { 
                console.log(error); 
            });

            this.interval = setInterval(() => {
                pos.lat += 0.01;
                pos.lng += 0.01;
                if(this.marker !== undefined){                
                    this.marker.setPosition(pos);
                }                
            }, 3000);

artnicbr avatar Sep 01 '17 19:09 artnicbr

Please create a demo project, and share it on github.

wf9a5m75 avatar Sep 01 '17 19:09 wf9a5m75

Hi, this is the link for my Ionic testing project: https://github.com/artnicbr/first-one

artnicbr avatar Sep 03 '17 16:09 artnicbr

Where is the setPosition() in your code?

import { Component } from '@angular/core';

import {
 GoogleMaps,
 GoogleMap,
 GoogleMapOptions
} from '@ionic-native/google-maps';
import { Http, Headers, RequestOptions } from '@angular/http';
import { Device } from '@ionic-native/device';

@Component({
  selector: 'page-map',
  templateUrl: 'map.html'
})
export class MapPage {
  map: GoogleMap;
  mapElement: HTMLElement;
  me: any;
  users: any;
  interval: any;

  constructor(private googleMaps: GoogleMaps, private http: Http, private device : Device) {
   // Load the map when the platform is ready
   let opt: RequestOptions
   let hds: Headers = new Headers
   opt = new RequestOptions({headers: hds});

   this.http.get("http://artnic-earth.redirectme.net/gps/list/all", opt)
   .subscribe((data) => {
   this.users = data.json();        
    if(this.users.list !== undefined){
        this.users.list.forEach((item) => {  
            this.me = (item.uuid == this.device.uuid) ? item : this.me;
        });
        this.loadMap();
    }
   });   
   this.interval = setInterval(() => {
        this.map.clear();
        this.users.list.forEach((item) => {  
            if(this.map && this.users && this.me.uuid != item.uuid){
                this.map.addMarker({
                    title: item.name + "(" + item.uuid + ")",
                    icon: 'blue',
                    position: {
                        lat: item._x,
                        lng: item._y
                    }            
                });
            }
        });
    }, 5000);
  }

  ionViewWillLeave(){
      clearInterval(this.interval);
  }

  ionViewDidLoad() {
   this.loadMap();
  }

 loadMap() {
    this.mapElement = document.getElementById('map');
    

    let mapOptions: GoogleMapOptions = {
      camera: {
        target: {
          lat: this.me._x,
          lng: this.me._y
        },
        zoom: 5,
        tilt: 30
      }
    };
    

    this.map = this.googleMaps.create(this.mapElement, mapOptions);

    this.map.addMarker({
        title: this.me.name + "(" + this.me.uuid + ")",
        icon: 'red',
        animation: 'DROP',
        position: {
            lat: this.me._x,
            lng: this.me._y
        }            
    });
  }
}

wf9a5m75 avatar Sep 03 '17 17:09 wf9a5m75

Sorry, I was making some tests and the last version was without the setPosition. Now I commited the version with setPosition. My bad...

this.interval = setInterval(() => {                
                this.http.get("http://artnic-earth.redirectme.net/gps/list/all", requestOpt)
                .subscribe((data) => {
                    let user = data.json().list;
                    let posItem: ILatLng;
                    let optItem: MarkerOptions;
                    this.map.clear().then(() => {
                        let i: number = 1;
                        user.forEach((item) => {
                            if(item.marker === undefined || item.marker == null){
                                this.map.addMarker({
                                    position: {
                                        lat: item._x,
                                        lng: item._y
                                    },
                                    icon: (item.uuid != this.me.uuid) ? 'blue' : 'red',
                                    title: item.name+'('+item.uuid+')'
                                }).then((marker) => {
                                    item.marker = marker;
                                });
                            }else{
                                alert("setPosition");
                                let np: ILatLng = {
                                    lat: item._x + i,
                                    lng: item._y + i
                                }
                                item.marker.setPosition(np);
                            }
                        });
                    });                
                });
            }, 5000);

artnicbr avatar Sep 04 '17 17:09 artnicbr

The reason of setPostion() does not work is you execute map.clear() before marker.setPosition().

map.clear() means removing all overlays. It means execute marker.remove() for all markers(). You can not reuse the instances.

wf9a5m75 avatar Sep 04 '17 18:09 wf9a5m75

Awesome, thanks for the help! I corrected map.clear() and coded again the callback function to store the created marker in an Array. Everything is fine now. Thank you so much!

artnicbr avatar Sep 04 '17 19:09 artnicbr

import {
  GoogleMaps,
  GoogleMap,
  GoogleMapsEvent,
  GoogleMapOptions,
  CameraPosition,
  MarkerOptions,
  Marker, LatLng
} from '@ionic-native/google-maps';

devices: any[] = [];
markers: any[] = [];
locations: any[] = [];
duration = 4000;
speed: any = 0;


ioReceiveLocation() {
    this.socket.on('ioReceiveLocation', (data) => {
      console.log(data);
      if (this.devices.indexOf(data.uuid) === -1) {
        this.devices.push(data.uuid);
        const indice = this.devices.indexOf(data.uuid);
        this.createMarker(data.location, indice);
      } else {
        const indice = this.devices.indexOf(data.uuid);
        this.setPositionAnimate(data.location, indice);
      }
    });
  }

createMarker(location, indice) {
    let latlng = new LatLng(location.coords.latitude, location.coords.longitude);
    let markerOptions = {
      title: 'Ionic',
      icon: 'red',
      animation: 'BOUNCE',
      position: latlng
    };
    this.markers[indice] = this.map.addMarker(markerOptions);
  }

  setPositionAnimate(location, indice) {
    let latlng = new LatLng(location.coords.latitude, location.coords.longitude);
    this.speed = location.coords.speed * 3.6;
    this.markers[indice].setPosition(latlng);
  }

this.markers[indice].setPosition(latlng) not work, not move, not animate Help-me!!!

manuvalentim avatar Oct 30 '17 19:10 manuvalentim

Hi @manuvalentim

Did you solve the problem?

darthxhester avatar Dec 29 '17 19:12 darthxhester

Awesome, thanks for the help! I corrected map.clear() and coded again the callback function to store the created marker in an Array. Everything is fine now. Thank you so much!

I am facing the same issue can u post ur code here

shivakumar116 avatar Jul 23 '19 02:07 shivakumar116

@artnicbr

Here is my project https://github.com/shivakumar116/Track-In Once check homepage.ts

shivakumar116 avatar Jul 23 '19 02:07 shivakumar116

Sorry, not clear - the above examples are in pure Javascript? I see some thing that I don't recognise, for example

import { GoogleMaps, GoogleMap, GoogleMapsEvent, GoogleMapOptions, CameraPosition, MarkerOptions, Marker, LatLng } from '@ionic-native/google-maps';

ghost avatar Nov 03 '20 23:11 ghost

@alex4buba The code is Typescript, and the code represents @ionic-native/google-maps which is the wrapper plugin of this plugin.

wf9a5m75 avatar Nov 03 '20 23:11 wf9a5m75

import {
  GoogleMaps,
  GoogleMap,
  GoogleMapsEvent,
  GoogleMapOptions,
  CameraPosition,
  MarkerOptions,
  Marker, LatLng
} from '@ionic-native/google-maps';

devices: any[] = [];
markers: any[] = [];
locations: any[] = [];
duration = 4000;
speed: any = 0;


ioReceiveLocation() {
    this.socket.on('ioReceiveLocation', (data) => {
      console.log(data);
      if (this.devices.indexOf(data.uuid) === -1) {
        this.devices.push(data.uuid);
        const indice = this.devices.indexOf(data.uuid);
        this.createMarker(data.location, indice);
      } else {
        const indice = this.devices.indexOf(data.uuid);
        this.setPositionAnimate(data.location, indice);
      }
    });
  }

createMarker(location, indice) {
    let latlng = new LatLng(location.coords.latitude, location.coords.longitude);
    let markerOptions = {
      title: 'Ionic',
      icon: 'red',
      animation: 'BOUNCE',
      position: latlng
    };
    this.markers[indice] = this.map.addMarker(markerOptions);
  }

  setPositionAnimate(location, indice) {
    let latlng = new LatLng(location.coords.latitude, location.coords.longitude);
    this.speed = location.coords.speed * 3.6;
    this.markers[indice].setPosition(latlng);
  }

this.markers[indice].setPosition(latlng) not work, not move, not animate Help-me!!!

@manuvalentim Your issue is resolved ?

piyushysoft avatar Sep 09 '21 10:09 piyushysoft