Hello i try to update the user positon in my google map using the geolocation plugin.
i used the example in the ionic docs and tried to adapt to my need.
Unfortunately, i didnt see any change on the marker if im moving the position of my device.
private selfmarker: Marker;
this.map.one(GoogleMapsEvent.MAP_READY).then(
() => {
this.geoLocation.getCurrentPosition().then((resp) => {
let userPosition: LatLng = new LatLng(resp.coords.latitude, resp.coords.longitude);
let position: CameraPosition = {
target: userPosition,
zoom: 15,
tilt: 0
};
this.map.moveCamera(position);
this.addSelfMarkerOnMap(userPosition);
const subscription = this.geoLocation.watchPosition()
.filter((p) => p.coords !== undefined) //Filter Out Errors
.subscribe(position => {
this.selfmarker.remove();
let markerOptions: MarkerOptions = {
// after console.log(position.coords.latitude,position.coords.longitude) it show every x seconds
//updated position
position: new LatLng(position.coords.longitude, position.coords.latitude),
};
this.map.addMarker(markerOptions).then((marker) => { this.selfmarker = marker; });
// I Tried to change the position of the marker directly in the map
// this.selfmarker.setPosition(new LatLng(position.coords.longitude, position.coords.latitude));
});
}).catch((error) => {
console.log('Error getting location', error);
});
}
);
/* The marker at the start of the application is working */
private addSelfMarkerOnMap(userPosition: any) {
let markerOptions: MarkerOptions = {
position: markerPosition,
icon: markerIcon
};
this.map.addMarker(markerOptions).then((marker) => { this.selfmarker = marker; });
}