how to get the client/user location details

巧了我就是萌 提交于 2019-12-04 12:04:08

Yes, sure, Google Maps JS API v3 does provide an IP to location geocoding services. You can use this code to get the users location based on their IP address:

// If ClientLocation was filled in by the loader, use that info instead
if (google.loader.ClientLocation) {
  zoom = 13;
  latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
  location = "Showing IP-based location: <b>" + getFormattedLocation() + "</b>";
}

Beside Lat/Lng, google.loader.ClientLocation returns an address which include city, country, country_code, region etc...
Also, look at the example here.

Even if the address is null, you can always use the returned Lat/Lng with the Geocoding Service to get the address detail.

However, just as it is mentioned in the official site:

... we don't recommend using this approach (using IP addresses to detect a user's location) for geolocation. The W3C approach is the easiest and most fully-supported so it should be prioritized over other methods.

Also in the issue tracker:

... We stopped documenting it several years ago and have recommended the HTML-based solutions due to their improved accuracy, but the functionality itself has not been removed from the Loader...

The best way to do this is with the HTML5-based approach. However, if you are using the IP-based, be sure that you realize Google can drop support to it really any time.

You can use geoIP MaxMind web services to get location details in C#. Here is the following link which may be helpful for you.

http://dev.maxmind.com/geoip/geoip2/web-services/

Here is the Github repository available.

https://github.com/maxmind/GeoIP2-dotnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!