A
A
Artem2018-01-22 23:54:14
C++ / C#
Artem, 2018-01-22 23:54:14

How to find the nearest marker and calculate the distance to it using Google Maps API?

There is such a code with an array that contains the coordinates of the marks and the script for adding a mark by clicking on the map / input:

js code itself
var coord = [
    {"id": 1, "coordlat": 47.2200, "coordlng": 39.6170},
    {"id": 2, "coordlat": 47.2882, "coordlng": 39.6643},
    {"id": 3, "coordlat": 47.2190, "coordlng": 39.7162},
    {"id": 4, "coordlat": 47.2502, "coordlng": 39.7930},
    {"id": 5, "coordlat": 47.2503, "coordlng": 39.5639},
    {"id": 6, "coordlat": 47.2612, "coordlng": 39.6854},
    {"id": 7, "coordlat": 47.2830, "coordlng": 39.6253},
    {"id": 8, "coordlat": 47.2799, "coordlng": 39.7486},
    {"id": 9, "coordlat": 47.1972, "coordlng": 39.6569},
];

var delAdress = $('input"]');
var markers = [];
var coords = [];
var map;

function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 11,
        center: {lat: 47.2381, lng: 39.7002}
    });
    var geocoder = new google.maps.Geocoder();
    for( i = 0; i < coord.length; i++ ) {
        var position = new google.maps.LatLng(coord[i].coordlat, coord[i].coordlng);
        marker = new google.maps.Marker({
            position: position,
            map: map,
        });
        coords.push(marker);
    };
    document.getElementById('btn').addEventListener('click', function() {
        if(delAdress.val()==''){
            return false;
        } else{
            geocodeAddress(geocoder, map);
        }
    });
    map.addListener('click', function(event) {
          addMarker(event.latLng);
    });
};

function addMarker(location){
    setMapOnAll(null);
    markers = [];
    var marker = new google.maps.Marker({
        position: location,
        map: map,
    });
    markers.push(marker);
};

function geocodeAddress(geocoder, resultsMap) {
    
    var address = document.getElementById('suggestions').value;
    
    geocoder.geocode({'address': address}, function(results, status) {
        if (status === 'OK') {
            setMapOnAll(null);
            markers = [];
            resultsMap.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
            });
            markers.push(marker);
        } else {
            console.log('Geocode was not successful for the following reason: ' + status);
        }
    });
};

function setMapOnAll(map) {
    for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(map);
    }
}
Duplicate on JSFiddle
And the question is, when adding a marker by the user, find the nearest added marker from the array and calculate the distance to it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NIKITF, 2021-08-04
@NIKITF

Your edited code:

#include<iostream> 
#include<fstream> 
#include<string>
using namespace std;
struct book
{
  string name;
  unsigned year;
  unsigned circulation;
};
void sort(book* arr, book* array1, const unsigned& N);
unsigned kolvo(book* arr, const unsigned&N);
int main()
{
  unsigned n, N; bool otkr;
  setlocale(LC_ALL, ""); book* arr = NULL;
  cout << "ВЫБЕРИТЕ СПОСОБ ВВОДА. 0 - С КЛАВИАТУРЫ, 1 - ИЗ ФАЙЛА  \n"; cin >> otkr;
  if (!otkr)
  {
    cout << "ВВЕДИТЕ РАЗМЕР МАССИВА:\n";
    cin >> N;
    arr = new book[N];
    for (int i = 0; i < N; i++)
    {
      cout << "ВВЕДИТЕ НАЗВАНИЕ КНИГИ:\n";
      cin >> arr[i].name;
      cout << "ВВЕДИТЕ ТИРАЖ:\n";
      cin >> arr[i].circulation;
      cout << "ВВЕДИТЕ ГОД \n";
      cin >> arr[i].year;
    };
    for (int i = 0; i < N; i++)
    {
      cout << arr[i].name << "  " << arr[i].circulation << "  " << arr[i].year << endl;
    };
  }
  else
  {
    ifstream knigi("TextFile1.txt");
    knigi >> N;
    arr = new book[N];
    for (int i = 0; i < N; i++)
    {
      knigi >> arr[i].name >> arr[i].circulation >> arr[i].year;
    }
  };
  if ((n = kolvo(arr, N)) == 0)
  {
    cout << "no matches";
    return 0;
  }
  book* array1 = new book[n]; cout<<endl;
  sort(arr, array1, N);
  for (int i = 0; i < n; i++)
  {
    cout << "КНИГИ НАПЕЧАТАННЫЕ С 2000 ПО 2010 :" << arr[i].name << "  " << arr[i].circulation << endl;
  }
  return 0;
}

void sort(book* arr, book* array1, const unsigned&N)
{
  unsigned counter = 0;
  for (auto i = 0; i < N; i++)
  {
    if ((arr[i].year > 1999) && (arr[i].year < 2011))
    {
      array1[counter].name = arr[i].name;
      array1[counter++].circulation = arr[i].circulation;
    }
  }
}

unsigned kolvo(book* arr, const unsigned &N)
{
  unsigned number = 0;
  for (int i = 0; i < N; i++)
  {
    if (arr[i].year > 1999 && arr[i].year < 2011)
    {
      number++;
    }
    return(number);
  }
}

F
freeExec, 2018-01-23
@thegreatestmafaka

https://stackoverflow.com/questions/4057665/google...
But that's all cool as long as you have a hundred other points. Wisely, you need to use spatial indexes and specially sharpened algorithms https://github.com/mourner/rbush-knn

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question