Reputation: 707
I am implementing a geolocation/position tracking functionality with openlayers5 and angular 6. Even though I dont get any errors and this seems to work fine, I dont get the coords right away. Here is my geolocation-related code.
ngOnInit() {
//initialize everything in the ngOnInit
//set it
this.geolocation = new Geolocation({
trackingOptions: {
enableHighAccuracy: true
},
projection: 'EPSG:3857'
});
//handle errors
this.geolocation.on('error', (error) => {
this.geolocationerror=true;
this.geolocationmsg = error;
});
//accuracy
this.accuracyFeature = new Feature();
this.geolocation.on('change:accuracyGeometry', ()=> {
this.accuracyFeature.setGeometry(this.geolocation.getAccuracyGeometry());
});
//position point
this.positionFeature = new Feature();
this.positionFeature.setStyle(new Style({
image: new CircleStyle({
radius: 6,
fill: new Fill({
color: '#3399CC'
}),
stroke: new Stroke({
color: '#fff',
width: 2
})
})
}));
//track position
this.geolocation.on('change:position', ()=> {
let coordinates = this.geolocation.getPosition();
this.positionFeature.setGeometry(coordinates ?
new Point(coordinates) : null);
});
//on smaller screens toggle the geolocation on automatically
if(window.innerWidth < 600){
this.isChecked = true;
}
//geolocation has its own vector layer
this.geolocsource = new VectorSource({});
this.geoloclayer = new VectorLayer({
source: this.geolocsource,
title:'location'
});
}//ngOnInit closes here
//toggle geolocation on/off
toggleGeolocation(checked){
//erase any previous errors
this.geolocationmsg='';
this.geolocationerror=false;
////toggled on
if(checked){
this.geolocation.setTracking(checked); //set on
this.geolocsource.addFeatures([this.accuracyFeature, this.positionFeature]) ;
this.geolocOn = true; // to show info in html
let center = this.geolocation.getPosition();
//zoom there
if (center){
this.olmap.getView().animate({
center: center,
duration: 2000,
zoom:16
});
}
//show the geolocation coords in html all the time
this.geolocLiveCoords = this.geolocation.getPosition();
}
else{ //geolocation off
this.geolocation.setTracking(checked);
this.geolocOn = false;
this.geolocsource.clear();
this.geolocLiveCoords='';
}
}
and this is the HTML part
<label for="track">track position<input id="trackpos" type="checkbox" [(ngModel)]="isChecked" (change)="toggleGeolocation(isChecked)"/></label>
<div *ngIf='geolocationerror'>{{geolocationmsg}}</div>
<div *ngIf='geolocOn'>{{geolocLiveCoords}}</div>
The checkbox is automatically checked for smaller screens.
In any case, even though I get no errors, I have to manually check-unckeck the checkbox myself a couple of times in order to see the geolocation coords and for the whole code to work. It does not work the first time I turn it on, it gets no coords, so I turn it off and on again. After that it works fine, as it should.
What is the problem here? How can I fix it? Please advice.
Thanks
Upvotes: 0
Views: 706
Reputation: 2857
You're only turning on the tracking when you call toggleGeolocation
with true
which, at this stage, only happens then you check the check-box. If you want it to start straight away, you should call it at the end of your ngOnInit
method:
this.toggleGeolocation(this.isChecked);
Upvotes: 1