How to calculate distance between two geolocation?


I am calculating distance between two Latitude and long
one is the current location of the user and another from the database, I want here if the lat-long distance is between 4km then show data

how can I do it using C#?


Asked by:- vikas_jk
0
: 316 At:- 7/21/2017 9:56:30 AM
C# asp-net Geolocation






1 Answers
profileImage Answered by:- vikas_jk

Answering to my question, as it  was  originally asked by RameshwarLate

So here are the steps, which you need to achieve:

  1. Get Current user location using javascript, for this step I assume you have enabled Google API to get current user location if you haven't, follow these steps

    1. Create new project for "getting user location" in Google Console
    2. Create an API key for the new project.
    3. Enable Google Geocoding API and google maps javascript API, add this javascript code
        
    <script async defer
            src="https://maps.googleapis.com/maps/api/js?key=YourApiKeyHere&callback=initMap">
    </script>?

    4. Run the project in the browser, allow the browser to access your location.
  2. Get user Location While loading browser using this javascript code(you don't need to create Map, just add a div with id)

    var map, infoWindow,CurrentLat,CurrentLong;
          function initMap() {
           //Just to intialize maps api
            map = new google.maps.Map(document.getElementById('map'), {
              center: {lat: -34.397, lng: 150.644},
              zoom: 6
            });
            infoWindow = new google.maps.InfoWindow;
    
            // Try HTML5 geolocation.
            if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(function(position) {
                var pos = {
                  lat: position.coords.latitude,
                  lng: position.coords.longitude
                };
                CurrentLat = position.coords.latitude;
                CurrentLong = position.coords.longitude;
                
                 //Save CurrentLat, CurrentLong here in Javascript or pass to C# using ajax
              }, function() {
                handleLocationError(true, infoWindow, map.getCenter());
              });
            } else {
              // Browser doesn't support Geolocation
              handleLocationError(false, infoWindow, map.getCenter());
            }
    
    
    
          }
    
          function handleLocationError(browserHasGeolocation, infoWindow, pos) {
            infoWindow.setPosition(pos);
            infoWindow.setContent(browserHasGeolocation ?
                                  'Error: The Geolocation service failed.' :
                                  'Error: Your browser doesn\'t support geolocation.');
            infoWindow.open(map);
          }?
  3. Pass the CurrentLat, CurrentLong variables to C#, using Ajax
  4. In C#, use this code to get Lat/Long from address(If you don't have other location lat/long already in database, but you have addresses in database, I insist you save Lat/Long in database)

    //pass address here like 123,Street,City , it will give you lat/long
     public string ConvertAddressIntoLatLong(string address)
            {
              
                var locationService = new GoogleLocationService();
                var point = locationService.GetLatLongFromAddress(address);
    
                var latitude = point.Latitude;
                var longitude = point.Longitude;
                return (latitude+","+longitude);
            }?
    Note: You may need to install NuGet package to GeoLocation
  5. Now as you have both Current user Lat/Long and lat/long from database, calculate distance between two location using C#

     public static double DistanceTo(double lat1, double lon1, double lat2, double lon2, char unit = 'K')
            {
                double rlat1 = Math.PI * lat1 / 180;
                double rlat2 = Math.PI * lat2 / 180;
                double theta = lon1 - lon2;
                double rtheta = Math.PI * theta / 180;
                double dist =
                    Math.Sin(rlat1) * Math.Sin(rlat2) + Math.Cos(rlat1) *
                    Math.Cos(rlat2) * Math.Cos(rtheta);
                dist = Math.Acos(dist);
                dist = dist * 180 / Math.PI;
                dist = dist * 60 * 1.1515;
    
                switch (unit)
                {
                    case 'K': //Kilometers -> default
                        return dist * 1.609344;
                    case 'N': //Nautical Miles 
                        return dist * 0.8684;
                    case 'M': //Miles
                        return dist;
                }
    
                return dist;
            }?
  6. Based on results of Step 5, you can show other data, to show it on front end, use jQuery Ajax 

Hope this helps, please upvote and share your thoughts by commenting on this answer.

0
At:- 7/21/2017 10:35:49 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use
Amazon Deals

Jobs from Indeed