Измените CENTER const для массива CENTER для цикла / итерации? - PullRequest
0 голосов
/ 07 января 2019

У меня есть рабочая закуска с вспомогательной функцией, которая использует const CENTER.

Я пытаюсь изменить const CENTER на массив CENTER с множеством координат, чтобы вспомогательная функция могла выполнять итерацию / цикл? через все точки.

    const { latitude, longitude, speed } = position.coords;
    const center = { latitude: 37.600530, longitude: -122.482629 };  //this is to change
    const { radius, distance } = this.calculateMeasurements(latitude, longitude, center);


    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 };
  }

Я бы хотел разместить массив CENTER, как этот, может быть: он может работать?

    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 )];

1 Ответ

0 голосов
/ 07 января 2019

Это будет хорошо работать. Используйте map для цикла вашего массива, как показано ниже. И не забывайте использовать фигурные скобки при использовании элементов объекта в массиве.

const { latitude, longitude, speed } = position.coords;
const centers = [ // use curly brackets for js objects.
{ 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 = centers.map(({id, lati: latitude, longi: longitude) => {
  return this.calculateMeasurements(latitude, longitude, {lati, longi});
});

console.log(`results[0] : ${results[0].radius}, ${results[0].distance}`);

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 };
}
...