0

I am doing a project and i want to know how can I calculate the distance between 2 markers if it is between 20km? I want to know how to count the distance. Based on my code, I only can display the map and the marker.

Thank you!

        var TheFamilyPractice = new google.maps.Marker({
            position: { lat: 1.332419, lng: 103.867687},
            map: map
        });




        var contentTheFamilyPractice = '<div id="content">' +
            '<div id="siteNotice">' +
            '</div>' +
            '<h1 id="firstHeading" class="firstHeading">The Family Practice</h1>' +
            '<div id="bodyContent">' +
            '<p><b>Clinic Name: </b>The Family Practice </p> ' +
            '<p><b>Tel: </b>62816906 </p>' +
            '<p><b>Address: </b>148 Potong Pasir Ave 1, Singapore 350148</p>' +

            '</div>' +
            '</div>';
        var infowindow01 = new google.maps.InfoWindow({
            content: contentTheFamilyPractice
        });

        TheFamilyPractice.addListener('click', function () {
            infowindow01.open(map, TheFamilyPractice);
        });


        var Bankok = new google.maps.Marker({
            position: { lat: 1.332432, lng: 103.868321 },
            map: map
        });


        var contentBankok = '<div id="content">' +
            '<div id="siteNotice">' +
            '</div>' +
            '<h1 id="firstHeading" class="firstHeading">Ban Kok Clinic & Surgery</h1>' +
            '<div id="bodyContent">' +
            '<p><b>Clinic Name: </b>Ban Kok Clinic & Surgery </p> ' +
            '<p><b>Tel: </b>62869955 </p>' +
            '<p><b>Address: </b>146 Potong Pasir Ave 1, Singapore 350146</p>' +

            '</div>' +
            '</div>';
        var infowindow02 = new google.maps.InfoWindow({
            content: contentBankok
        });

        Bankok.addListener('click', function () {
            infowindow02.open(map, Bankok);
        });
  • You have two coordinates, `{ lat: 1.332419, lng: 103.867687}` and `{ lat: 1.332432, lng: 103.868321 }`, and you want to calculate the actual distance (which is about 20km) between them? Right? – kennyzx Jun 26 '18 at 08:47
  • Yes, I want to calculate the actual distance between. Do you have any solution to help? – Kareen Toh Jun 26 '18 at 10:00

1 Answers1

0

If you want the shortest distance, what is called the Great-circle distance you can check this answer.

If you want the travel distance (as displayed on traffic map), check Google Maps Distance Matrix API.

kennyzx
  • 12,845
  • 6
  • 39
  • 83
  • I am unsure of how can I use the code in the Google Maps Distance Matrix. I do not know which code to take to calculate the distance? – Kareen Toh Jun 27 '18 at 06:52
  • Construct a url using your coordinates and app key, and request the google service (sending a http GET request to the url), then you get the result from the response. It is explained well in that page. – kennyzx Jun 27 '18 at 06:56
  • Is it like the google map if I click on one of the markers and another marker it will display the distance between the 2 marker's distance on the map? – Kareen Toh Jun 27 '18 at 07:32
  • That seems to be the direct distance (Great circle distance). Which distance do you want? Direct distance or travel distance? – kennyzx Jun 27 '18 at 08:36
  • Direct Distance, hopefully can be like the google map measure the distance. But I think in the google map API do not have this kind of code. – Kareen Toh Jun 27 '18 at 10:23
  • Have you tried the method in the [linked answer](https://stackoverflow.com/a/6545031/815938)? – kennyzx Jun 27 '18 at 10:25
  • yes I have tried the method when I put the code there is an error Console.WriteLine(DistanceAlgorithm.DistanceBetweenPlaces(36.578581, -118.291994, 36.23998, -116.83171)); – Kareen Toh Jul 06 '18 at 10:03