I am developing a website using ASP.NET MVC, how can I get the current location (Country, city, district, lat-lng geolocation) of each visitor browses my website depending on IP address?
-
Welcome to Stack Overflow. Please read How to Ask and share your research. You're not the first person asking this question.– CodeCasterApr 20, 2016 at 11:18
-
1Possible duplicate of How to get visitor's location (i.e. country) using javascript geolocation– CodeCasterApr 20, 2016 at 11:22
3 Answers
It's possible to get location by using IP address of visitor. Use web-service provided by ipinfodb.com :
string url = string.Format("https://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
Location location = new JavaScriptSerializer().Deserialize<Location>(json);
return location;
}
There are several services out there that can do this. Try, for example, this one: http://geoip.nekudo.com. An example: http://geoip.nekudo.com/api/8.8.8.8 Look for "ip geolocation rest api".
-
Why the downvote? My answer has nothing to do with JavaScript! Apr 25, 2016 at 11:30
There are many geo-localization service you can use. For example, I do this way.
Put this tag inside your main .html page:
<script type="text/javascript" src="https://l2.io/ip.js?var=userIp"></script>
Than you will find a global variable called userIp
with the calling ip address.
Than you can use any geolocation tool to get all the information you need.