У меня есть угловое приложение, использующее библиотеку листовок, в которой я хочу щелкнуть карту и сохранить координаты в BD.
Для этого у меня есть map.component.ts, где координатывзят и передан в service.ts, но в service.ts я получаю пустой объект.
import { Component, OnInit } from '@angular/core';
import { FeaturesService } from '../../services/feature.service'
import { Vertex } from '../../models/vertex'
declare let L;
var lat;
var lng;
var coordinatesObj: Vertex;
var map;
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
constructor(private _vertexService: FeaturesService) {}
ngOnInit() {
map = L.map('map').setView([40.4893538, -3.6827461], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
}
getCoordinates() {
map.addEventListener('click', function(ev) {
lat = ev.latlng.lat;
lng = ev.latlng.lng;
coordinatesObj = {
"lat": lat,
"lng": lng
};
})
this.sendCoords(coordinatesObj);
}
sendCoords(coordinatesObj) {
this._vertexService.registerVertex(coordinatesObj).subscribe(
response => {
if (!response) {
console.log('there is no response')
} else {
console.log(response)
}
},
error => {
let errorMessage = < any > error;
if (errorMessage !== null) {
let body = JSON.parse(error._body);
console.log('error:' + body);
}
}
);
}
}
Что я делаю не так?Переменные lat и long объявляются вне map.addEventListenerEvent, поэтому они могут быть доступны любым методом.