Я использовал Open layer 3 в angular 2 project для отслеживания текущего местоположения пользователя. Он работает нормально в первый раз и после маршрутизации на некоторые другие страницы и возвращается на страницу карты, хотя текущее местоположение установлено, карта не загружает текущую позицию. как справиться с этим вопросом ?? Вот мой код, который я использовал.
initializeMap() {
this.markerSource = new ol.source.Vector({ wrapX: false });
this.layer = new ol.layer.Vector({
source: this.markerSource,
style: (f, r) => this.styleFunction(f, r),
});
let view = new ol.View({
center: ol.proj.fromLonLat([5.73271, 58.96564]),
zoom: 5
});
this.map = new ol.Map({
layers: [this.layer],
view: view
});
/// add geo location features to show the location if user give permissions start ///
let geolocation = new ol.Geolocation({
tracking: true,
projection: view.getProjection()
});
var positionFeature = new ol.Feature();
positionFeature.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({
color: '#3399CC'
}),
})
}));
geolocation.on('change:position', function () {
var coordinates = geolocation.getPosition();
positionFeature.setGeometry(coordinates ?
new ol.geom.Point(coordinates) : null);
view.setCenter(coordinates);
});
let track = geolocation.getTracking();
if (track) {
console.log("test");
let coordinates = geolocation.getPosition();
console.log("test: ", coordinates);
positionFeature.setGeometry(coordinates ?
new ol.geom.Point(coordinates) : null);
view.setCenter(coordinates);
}
this.markerSource.addFeature(positionFeature);
/// add geo location features to show the location if user give permissions end ///
// add tile layer to support map switching
this.toggleMapLayers(this.map, this.siteSetting.mapType.getValue());
// add select interaction for single-click
this.map.addInteraction(this.getOnClickInteraction());
// add select interaction for on hover
this.map.addInteraction(this.getOnHoverInteracion());
// overlay/popup that will show on mouse hover
this.popup = new ol.Overlay({
element: this.popupElement.nativeElement,
offset: [10, 10]
});
this.map.addOverlay(this.popup);
this.map.setTarget(this.mapElement.nativeElement);
}
Эта функция вызывается внутри ngOnInit (), как показано ниже.
ngOnInit() {
this.initializeMap();
}
Заранее спасибо.
Я использовал открытый слой 3, который я загрузил из CDN для вашей информации, и мой проект следует за углом 2.