Get user location by IP address

前端 未结 14 1243
深忆病人
深忆病人 2020-11-28 02:23

I have an ASP.NET website written in C#.

On this site I need to automatically show a start page based on the user\'s location.

Can I get name of user\'s city

14条回答
  •  清酒与你
    2020-11-28 02:56

    An Alternative to using an API is to use HTML 5 location Navigator to query the browser about the User location. I was looking for a similar approach as in the subject question but I found that HTML 5 Navigator works better and cheaper for my situation. Please consider that your scinario might be different. To get the User position using Html5 is very easy:

    function getLocation()
    {
        if (navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
        else
        {
            console.log("Geolocation is not supported by this browser.");
         }
    }
    
    function showPosition(position)
    {
          console.log("Latitude: " + position.coords.latitude + 
      "
    Longitude: " + position.coords.longitude); }

    Try it yourself on W3Schools Geolocation Tutorial

提交回复
热议问题