У меня есть функция перекуса и помощника для сопоставления всех точек CENTER в массиве, расчета, нахожусь ли я в радиусе и расстоянии до них.
Это не работает, и я не могу понять, почему. Я всегда получаю журнал «Радиус не был рассчитан».
Он работал без массива, но теперь что-то падает.
componentDidMount () {
this.watchId = navigator.geolocation.watchPosition(
(position) => {
const { latitude, longitude, speed } = position.coords;
const center = [
{ id: 1, latitude: 34.3434656, longitude: -56.876456 },
{ id: 2, latitude: 35.3434656, longitude: -57.876456 },
{ id: 3, latitude: 36.3434656, longitude: -58.876456 },
{ id: 4, latitude: 37.3434656, longitude: -59.876456 }];
const { radius, distance } = this.calculateMeasurements(latitude, longitude, center);
const results = center.map((id, lati: latitude, longi: longitude) => {
return this.calculateMeasurements(latitude, longitude, {lati, longi});
});
console.log(`results[0] : ${results[0].radius}, ${results[0].distance}`);
this.setState({
latitude: latitude,
longitude: longitude,
speed: speed,
radius: radius,
distance: distance,
error: null
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 0, distanceFilter: 1 }
);
}
componentWillUnmount () {
navigator.geolocation.clearWatch(this.watchId);
}
calculateMeasurements = (latitude, longitude, center) => {
const radius = geolib.isPointInCircle(
{ latitude: latitude, longitude: longitude },
{ latitude: center.latitude, longitude: center.longitude },
1000
);
const distance = geolib.getDistance(
{ latitude: latitude, longitude: longitude },
{ latitude: center.latitude, longitude: center.longitude }
);
console.log(radius, distance);
return { radius, distance };
}
render () {
const { radius, distance } = this.state;
if (radius === true) {
console.log('I am in Radius.');
} else if (radius === null) {
console.log('Radius has not been calculated');
} else if (radius === false) {
console.log('I am NOT in Radius.');
}