使用Javascript将IP映射到位置 [英] IP to Location using Javascript

查看:146
本文介绍了使用Javascript将IP映射到位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="application/javascript">
    function getip(json){
      alert(json.ip); // alerts the ip address
    }
</script>

<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>

我可以通过此代码获得用户IP ...

I can get User IP by this code...

我想找到这个IP的位置。
我该怎么办?

I want to find location of this IP. How can I?

推荐答案

您可以将收到的IP提交给在线地理定位服务,例如 http://www.geoplugin.net/json.gp?ip=<your ip here>& jsoncallback =<适用于你的来源> 的javascript函数,然后包括返回的源代码将运行您在 jsoncallback 中指定的函数以及地理位置信息。

You can submit the IP you receive to an online geolocation service, such as http://www.geoplugin.net/json.gp?ip=<your ip here>&jsoncallback=<suitable javascript function in your source>, then including the source it returns which will run the function you specify in jsoncallback with the geolocation information.

或者,您可能需要查看HTML5的地理定位功能 - 您可以在此处查看其实际演示。这样做的好处是您不需要向外部服务器发出请求,但它可能不适用于不支持HTML5的浏览器。

Alternatively, you may want to look into HTML5's geolocation features -- you can see a demo of it in action here. The advantage of this is that you do not need to make requests to foreign servers, but it may not work on browsers that do not support HTML5.

这篇关于使用Javascript将IP映射到位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆