Search code examples
asp.netgeolocationipip-address

How can I trace the exact location with lat/long and IP address of user using ASP.NET


When ever user visited to my site, or open my website site in browser, I want to store all the necessary information of the users like, location , city, county, latitude/longitude and including IP address as well.

Some of the answer I have found and implement them as well. Though they give the location but wasn't the exact location of the User.

Do not suggest me to use freegeoip, dotnetcurry,iplocationtools, etc. all are the useless and does not give exact location.


Solution

  • With the google map geolocation, Here I have fetch the latitude, longitude including the exact location of the user where ever they are. For google map API: "https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=false"

    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(success);
    } else {
    alert("Geo Location is not supported on your current browser!");
    }
    function success(position) {
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    var myLatlng = new google.maps.LatLng(lat, long);
    
    var geocoder, add, country;
    geocoder = new google.maps.Geocoder();
    
    geocoder.geocode({
        'latLng': myLatlng
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                add = results[1].formatted_address;
                country = results[9].formatted_address;
            } 
        }
    });
    }