javascript 从 IP 地址获取地理位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21306088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:37:26  来源:igfitidea点击:

Getting geolocation from IP address

javascriptphpjquerycodeignitergoogle-maps

提问by rb vishnu

I need to track user details, for that client's location and IP address is needed.
I got IP address from

我需要跟踪用户详细信息,因为需要该客户端的位置和 IP 地址。
我从

$this->input->ip_address();

in codiigniter.
Now the problem is how can i make their location in google map by using their respective IP address

在 codiigniter 中。
现在的问题是我如何使用他们各自的 IP 地址在谷歌地图中定位他们的位置

采纳答案by ReNiSh A R

just use ipinfo.ioat

只需使用ipinfo.io

http://ipinfo.io/

http://ipinfo.io/

it uses a location api that we can post ip address and it will returns the location details as json:

它使用一个位置 api,我们可以发布 ip 地址,它将以 json 形式返回位置详细信息:

we can able to display the loacation on map with the langitude and longitude details from json response to google Maps API.

我们可以使用来自 json 响应到 google Maps API 的纬度和经度详细信息在地图上显示位置。

Here is the code i used:

这是我使用的代码:

this script creates a Google Map instance with lattitude & longitude from json response:

此脚本从 json 响应创建一个具有纬度和经度的 Google 地图实例:

jQuery(document).ready(function(){
    jQuery.get("http://ipinfo.io/202.88.237.138", function (response)
               {
                   var lats = response.loc.split(',')[0]; 
                   var lngs = response.loc.split(',')[1];
                   map = new GMaps({
                       el: '#map',
                       lat: lats, //latitude
                       lng: lngs //longitude
                   });

               }, "jsonp");
});

and the map will displayed on:

地图将显示在:

<div style="border:1px solid red; height:745px;" id="map"></div>

Google Maps API gmaps.jsis needed to run this..

需要谷歌地图 API gmaps.js来运行这个..

回答by ChrisSwires

As a viable (although often less accurate) alternative, the HTML5 spec includes Geolocation. As HTML 5 becomes more and more prevalent I think we can expect to see this become standard in the future. It also does not require the use of external web services or libraries. Everything you need to geolocate is built right into the clients browser.

作为一种可行的(虽然通常不太准确)替代方案,HTML5 规范包括 Geolocation。随着 HTML 5 变得越来越流行,我认为我们可以期待它在未来成为标准。它还不需要使用外部 Web 服务或库。地理定位所需的一切都内置在客户端浏览器中。

I believe the primary method used is IP address as specified.

我相信使用的主要方法是指定的 IP 地址。

回答by Nick Sweeting

The Google Maps API will do the work of finding location using geoip for you. If the user is on a mobile device or has a more accurate way of locating themselves (like a GPS), it'll use that instead.

Google Maps API 将使用 geoip 为您完成查找位置的工作。如果用户在移动设备上或者有更准确的定位方式(如 GPS),它将使用它来代替。

Here's a good starting point:
https://google-developers.appspot.com/maps/documentation/javascript/examples/map-geolocation

这是一个很好的起点:https:
//google-developers.appspot.com/maps/documentation/javascript/examples/map-geolocation

If you absolutely need to fetch location from IP only, there are third-party toolsthat you can scrape to get this information. You should make sure you have permission beforehand if you're using this in a larger project.

如果您绝对需要仅从 IP 获取位置,则可以使用第三方工具来获取此信息。如果你在一个更大的项目中使用它,你应该确保事先获得许可。

回答by Effi Bar-She'an

freegeoip.netprovides a public HTTP API to search the geolocation of IP addresses. It uses a database of IP addresses that are associated to cities along with other relevant information like time zone, latitude and longitude.

freegeoip.net提供了一个公共 HTTP API 来搜索 IP 地址的地理位置。它使用与城市相关联的 IP 地址数据库以及时区、纬度和经度等其他相关信息。

You're allowed up to 15,000 queries per hour. If you need more than that, you can run the freegeoip as a web server on your own infrastructure. See: freegeoip on github

您每小时最多可以进行 15,000 次查询。如果您需要更多,您可以在您自己的基础设施上将 freegeoip 作为 Web 服务器运行。请参阅:github 上的 freegeoip