简体   繁体   English

如何使用经纬度获取用户当前位置

[英]How to get user current location with lat and long

I followed geocoder for a while to get the user's current location and info, and alone with lat long value.我关注了一段时间的地理编码器以获取用户的当前位置和信息,并且单独使用 lat long 值。

The code I am tried.我试过的代码。

import geocoder
myloc = geocoder.ip('me')
print(myloc.latlng)
print(geocoder.ipinfo())

Getting the info value is okay.获取info值是可以的。 But lat long value seems different.但经纬度值似乎不同。 It's showing an almost 8~9km distance location.它显示了近8~9km的距离位置。 Seems runtime is also a bit lengthy.似乎运行时间也有点长。 Is there any other way to get close enough lat long value and runtime also faster?有没有其他方法可以更快地获得足够接近的经纬度值和运行时间?

You need to understand how IP geolocation works to know why you don't get a precise result.您需要了解 IP 地理定位的工作原理,才能知道为什么您没有得到精确的结果。

1- IP Geolocation use your public IP address to locate you. 1- IP 地理位置使用您的公共IP 地址来定位您。 Most of the times this address is allocated to you by your internet provider.大多数情况下,此地址是由您的互联网提供商分配给您的。

It's not your computer's or your home's IP, but the IP of one of your Internet provider's equipment (their exit-point to the Internet, the IP of a router somewhere inside their infrastructure, or anything else).这不是您的计算机或您家的 IP,而是您的 Internet 提供商设备之一的 IP(它们到 Internet 的出口点,ZA12A3079E14CED46E69BA52B8A90B2 的路由器内部的某处)

2- There is no location information attached to an IP address, and depending on the Geolocation API you are using, the result can be more or less precise. 2- IP 地址没有附加位置信息,根据您使用的地理位置 API,结果可能或多或少精确。 I use Abstract Geolocation because it's free, accurate and works in real-time: https://www.abstractapi.com/ip-geolocation-api you need to create an account to get your API key, which takes only a few seconds.我使用 Abstract Geolocation 因为它是免费的、准确的并且可以实时运行: https://www.abstractapi.com/ip-geolocation-api您需要创建一个帐户来获取您的 API 密钥,这只需几秒钟。

3- Geocoder may be slow sometimes. 3-地理编码器有时可能很慢。 Calling an API is as simple as going through Geocoder, so you could do without it and get a faster response:调用 API 就像通过地理编码器一样简单,因此您可以不用它并获得更快的响应:

import requests
import json

response = requests.get("https://ipgeolocation.abstractapi.com/v1/?api_key=YOUR_API_KEY")
data = json.loads(response.content)
print(data)

This will show lots of information about your IP address, and you can optionally add an ip_address parameter to get info about another IP.这将显示有关您的 IP 地址的大量信息,您可以选择添加ip_address参数以获取有关另一个 IP 的信息。

geocoder uses your ip to get the location (rather than using more advanced techniques like WIFI SSID mapping and GPS) which isn't that accurate. geocoder器使用您的 ip 来获取位置(而不是使用更先进的技术,如 WIFI SSID 映射和 GPS),这并不准确。

From iplocation.net来自iplocation.net

  1. How accurate is IP-based Geolocation?基于 IP 的地理定位有多准确?

Accuracy of geolocation database varies depending on which database you use.地理位置数据库的准确性取决于您使用的数据库。 For IP-to-country database, some vendors claim to offer 98% to 99% accuracy although typical Ip2Country database accuracy is more like 95%.对于 IP 到国家/地区的数据库,一些供应商声称可以提供 98% 到 99% 的准确度,尽管典型的 Ip2Country 数据库准确度更像是 95%。 For IP-to-Region (or City), accuracy range anywhere from 50% to 75% if neighboring cities are treated as correct.对于 IP 到区域(或城市),如果相邻城市被视为正确,则准确度范围为 50% 到 75%。 Considering that there is no official source of IP-to-Region information, 50+% accuracy is pretty good.考虑到没有 IP-to-Region 信息的官方来源,50+% 的准确率是相当不错的。

You can try something like what is mentioned here: https://github.com/joeljogy/Getting-GeoLocation to get more accurate results.您可以尝试此处提到的内容: https://github.com/joeljogy/Getting-GeoLocation以获得更准确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM