目录

  • 前言
  • 前期准备
    • 注册成为开发者,获取Key
      • IP定位和逆地理编码API文档
  • 思路讲解
    • 1、IP地址获取
    • 2、IP定位获取经纬度
    • 3、逆地理编码获取地理位置
  • 代码实现
    • 本地运行版
      • 新版
      • 旧版本(已不适用)
    • web服务版
      • ip_pos.html
      • get_ip.c

前言

本文主要通过高德地图API进行一系列的分析。提供具体的讲解和源码。
获取的经纬度和地理位置精确度并不高,仅供参考
请勿非法使用,本人概不负责。
效果如下

前期准备

注册成为开发者,获取Key

高德开发平台:https://console.amap.com/dev/key/app
注册用户
填写相关信息成为开发者

创建key

需要先创建应用


然后点击添加

选择“web服务”,我们需要的就是 逆地理编码 和 IP定位API。ps:点击服务的蓝色字可以跳转到官方教程

创建完成后,就能获取到key了

IP定位和逆地理编码API文档

2022/07/22 发现v5已经改v3了,注意适配 https://restapi.amap.com/v3/ip?ip=114.247.50.2&output=xml&key=<用户的key>
https://lbs.amap.com/api/webservice/guide/api/ipconfig/

https://lbs.amap.com/api/webservice/guide/api/georegeo/

思路讲解

1、IP地址获取

采用2种方式,如果本地运行,选用搜狐接口 http://pv.sohu.com/cityjson?ie=utf-8,可以获取到访问者IP等信息。如果有web服务器,可以采用自建http请求获取访问者IP。

2、IP定位获取经纬度

调用高德地图 IP定位API https://restapi.amap.com/v5/ip?type=4&key=<用户的key>&ip=114.247.50.2
注意,上面的key就是我们刚才申请的应用里的key,将其替换于此,type是IP类型,ip就是你要查的IP。

我这用postman测试下,我们要的就是location,返回的json数据方便解析

3、逆地理编码获取地理位置

调用高德地图 逆地理API https://restapi.amap.com/v3/geocode/regeo?location=116.310003,39.991957&key=<用户的key>&radius=1000&extensions=all
其中location就是我们上一步获取的经纬度信息,key就是我们申请的key,其他参数都是调节用的,因为这些数据不确定,所以我实现时进行了简化。

测试,简化后,我们就获取formatted_address的内容

代码实现

本地运行版

创建XMLHttpRequest对象的实现并不严谨,可以优化,不过我懒了0.0
XMLHttpRequest - Web API 接口参考 | MDN
XMLHttpRequest 对象 W3C

文件名为 ip_pos.html,后缀是html就行

新版

<!DOCTYPE html>
<html lang="zh-CN"><head><title>我找到你了</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body><span id="ip"></span><br><span id="pos"></span><br><span id="address"></span>
</body>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript">// 存储IP地址var ip = returnCitySN["cip"];document.getElementById("ip").innerText = "你的IP是" + ip;get_pos(ip);function get_pos(ip){// 构建urlvar url = "https://restapi.amap.com/v3/ip?key=<你的key>&ip=" + ip;// 建立所需的对象var httpRequest = new XMLHttpRequest();// 打开连接  将请求参数写在url中 httpRequest.open('GET', url, true);// 发送请求  将请求参数写在URL中httpRequest.send();// 经纬度坐标var pos = "";// 获取数据后的处理程序httpRequest.onreadystatechange = function () {if (httpRequest.readyState == 4 && httpRequest.status == 200) {// 获取到json字符串var ret = httpRequest.responseText;//console.log(ret);// 转为JSON对象var json = JSON.parse(ret);console.log(json);pos = json["rectangle"];document.getElementById("pos").innerText = "你的经纬度是" + pos;pos_arr = pos.split(';');get_addr(pos_arr[0]);}};}function get_addr(pos){var httpRequest = new XMLHttpRequest();url = "https://restapi.amap.com/v3/geocode/regeo?key=<你的key>&radius=0&extensions=all&batch=false&location=" + pos;httpRequest.open('GET', url, true);httpRequest.send();httpRequest.onreadystatechange = function () {if (httpRequest.readyState == 4 && httpRequest.status == 200) {// 获取到json字符串var ret = httpRequest.responseText;//console.log(ret);// 转为JSON对象var json = JSON.parse(ret);console.log(json);var address = json["regeocode"]["formatted_address"];console.log(address);document.getElementById("address").innerText = "你的地址大概是" + address;}};}
</script></html>

旧版本(已不适用)

<!DOCTYPE html>
<html lang="zh-CN"><head><title>我找到你了</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body><span id="ip"></span><br><span id="pos"></span><br><span id="address"></span>
</body>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript">// 存储IP地址var ip = returnCitySN["cip"];document.getElementById("ip").innerText = "你的IP是" + ip;get_pos(ip);function get_pos(ip){// 构建urlvar url = "https://restapi.amap.com/v5/ip?key=<你的key>&type=4&ip=" + ip;// 建立所需的对象var httpRequest = new XMLHttpRequest();// 打开连接  将请求参数写在url中 httpRequest.open('GET', url, true);// 发送请求  将请求参数写在URL中httpRequest.send();// 经纬度坐标var pos = "";// 获取数据后的处理程序httpRequest.onreadystatechange = function () {if (httpRequest.readyState == 4 && httpRequest.status == 200) {// 获取到json字符串var ret = httpRequest.responseText;//console.log(ret);// 转为JSON对象var json = JSON.parse(ret);console.log(json);pos = json["location"];document.getElementById("pos").innerText = "你的经纬度是" + pos;get_addr(pos);}};}function get_addr(pos){var httpRequest = new XMLHttpRequest();url = "https://restapi.amap.com/v3/geocode/regeo?key=<你的key>&radius=0&extensions=all&batch=false&location=" + pos;httpRequest.open('GET', url, true);httpRequest.send();httpRequest.onreadystatechange = function () {if (httpRequest.readyState == 4 && httpRequest.status == 200) {// 获取到json字符串var ret = httpRequest.responseText;//console.log(ret);// 转为JSON对象var json = JSON.parse(ret);console.log(json);var address = json["regeocode"]["formatted_address"];console.log(address);document.getElementById("address").innerText = "你的地址大概是" + address;}};}
</script></html>

web服务版

我这里用的CGI,web服务为boa或apache。

ip_pos.html

<!DOCTYPE html>
<html lang="zh-CN"><head><title>我找到你了</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body><span id="ip"></span><br><span id="pos"></span><br><span id="address"></span>
</body><script type="text/javascript">// 存储IP地址var ip = "";var xmlhttp;if(window.XMLHttpRequest){//code for IE7+,Firefox,Chrome,Opera,Safarixmlhttp = new XMLHttpRequest();}else{//code for IE6,IE5xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}xmlhttp.onreadystatechange = function(){if(xmlhttp.readyState == 4){if(xmlhttp.status == 200){//将接收到的字符串存入strvar str = xmlhttp.responseText;if(str.length == 0){return;}// 解决XML, 解析错误:语法错误try //Internet Explorer{xmlDoc = new ActiveXObject("Microsoft.XMLDOM");xmlDoc.async = "false";xmlDoc.loadXML(text);} catch (e) {try //Firefox, Mozilla, Opera, etc.{parser = new DOMParser();xmlDoc = parser.parseFromString(str, "text/xml");} catch (e) { alert(e.message) }}var ret = xmlDoc.getElementsByTagName('p')[0].textContent;ip = ret;console.log(ip);document.getElementById("ip").innerText = "你的IP是" + ip;get_pos(ip);}else{//alert(xmlhttp.status);}}else{//alert(xmlhttp.readyState);}}xmlhttp.open("GET","cgi-bin/get_ip.cgi?get",true);xmlhttp.send();function get_pos(ip){// 构建urlvar url = "https://restapi.amap.com/v5/ip?key=<你的key>&type=4&ip=" + ip;// 建立所需的对象var httpRequest = new XMLHttpRequest();// 打开连接  将请求参数写在url中 httpRequest.open('GET', url, true);// 发送请求  将请求参数写在URL中httpRequest.send();// 经纬度坐标var pos = "";// 获取数据后的处理程序httpRequest.onreadystatechange = function () {if (httpRequest.readyState == 4 && httpRequest.status == 200) {// 获取到json字符串var ret = httpRequest.responseText;//console.log(ret);// 转为JSON对象var json = JSON.parse(ret);console.log(json);pos = json["location"];document.getElementById("pos").innerText = "你的经纬度是" + pos;get_addr(pos);}};}function get_addr(pos){var httpRequest = new XMLHttpRequest();url = "https://restapi.amap.com/v3/geocode/regeo?key=<你的key>&radius=0&extensions=all&batch=false&location=" + pos;httpRequest.open('GET', url, true);httpRequest.send();httpRequest.onreadystatechange = function () {if (httpRequest.readyState == 4 && httpRequest.status == 200) {// 获取到json字符串var ret = httpRequest.responseText;//console.log(ret);// 转为JSON对象var json = JSON.parse(ret);console.log(json);var address = json["regeocode"]["formatted_address"];console.log(address);document.getElementById("address").innerText = "你的地址大概是" + address;}};}
</script></html>

get_ip.c

配合 cgic.h 和 cgic.c编译 gcc get_ip.c cgic.c -o get_ip.cgi

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cgic.h"int cgiMain(void)
{// 用于接收环境变量QUERY_STRINGchar *lenstr = NULL;// 搜索环境变量 QUERY_STRING,并返回相关的值给字符串lenster。lenstr = getenv("QUERY_STRING");// 若没有找到环境变量 QUERY_STRING,直接returnif (lenstr == NULL){return -1;}// 使用MIME头信息″Content-type: text/html\n\n″来输出HTML源代码给Web服务器。// 请注意任何MIME头信息后必须有一个空行。一旦发送这个MIME头信息给Web服务器后,Web浏览器将认为随后的文本输出为HTML源代码printf("Content-type: text/html\n\n");printf("<p>%s</p>", cgiRemoteAddr);return 0;
}

前端页面获取访问者的IP地址、经纬度和地理位置相关推荐

  1. Java 获取访问者的IP地址

    获取访问者的ip地址 这一次,依然是一个工具类,主要是用来获取当前用户访问服务器的ip地址,可以用于更快的查找出是在那一台电脑上进行操作的那些东西. package com.fashion.fox.c ...

  2. springboot 获取访问者的ip地址、访问设备信息、城市地址信息

    1.获取访问者的ip地址: 不多说直接上代码,详解见注释 package com.xr.util;import lombok.extern.slf4j.Slf4j;import javax.servl ...

  3. Java | IP工具类(获取访问者的ip地址 查询IP地址)

    一.IpUtil package com.java.util;import com.fasterxml.jackson.annotation.JsonInclude; import com.faste ...

  4. 获取访问者的IP地址

    2019独角兽企业重金招聘Python工程师标准>>> HTTP_X_FORWARDED_FOR:浏览当前页面的用户计算机的网关 如果用户是通过代理服务器浏览的 那么使用REMOTE ...

  5. oracle实例查询IP,Oracle数据库中获取访问者的IP地址或主机名的方法

    有时我们需要在Oracle数据库中获取访问者的局域网或Internet网的主机名或IP地址.在Oracle中可以使用下面的方法来实现: Oracle包utl_inaddr 作用:用于取得局域网或Int ...

  6. 纯前端js获取电脑本地IP地址(必用)

    注意 启动本地服务才能获取到: function getUserIP(onNewIP) { // onNewIp - your listener function for new IPs //comp ...

  7. 获取网页访问者的IP地址

    描述:获取网页访问者的IP地址,并显示出访问时间 原理:使用JSP内置对象request的getRemoteAddr方法获取访问者地址:使用SimpleDateFormat将当前时间转换为指定的时间格 ...

  8. 前端Vue中获取本机ip地址

    前端Vue中获取本机ip地址 1.打开谷歌浏览器,地址栏输入chrome://flags,进入. 2.搜索Anonymize local IPs exposed by WebRTC,将其设置为Disa ...

  9. PHP获取IP地址所在的地理位置

    PHP获取IP地址所在的地理位置 时间:2011-04-02 16:19来源:三知开发网 作者:许亮 点击: 787 次 http://www.sunchis.com/html/php/phpsour ...

最新文章

  1. wxpython 安装教程
  2. fetch 自动加cookie_WEBUI自动化开发(第五章)
  3. CASIO 5800P计算器游戏--猜数字游戏
  4. 大厂必问的分布式究竟是什么鬼?
  5. 图解 hexo + github 从零快速 搭建个人博客 windowss
  6. 今天辞去了联盟的版主职务
  7. cyyz: Day 6 平衡树整理
  8. 通过sql server的作业调度+存储过程来实现系统定时任务的方法
  9. Servlet总结1---基础东西的介绍
  10. 【MATLAB统计分析与应用100例】案例017:matlab读取Excel数据,进行变量系统聚类分析
  11. 超级好看的windows终端美化教程
  12. 人工智能产生式系统实验—动物识别系统(txt文件导入数据库)python代码实现以及关系图
  13. plotplayer s/w hevc(h265)解码 问题
  14. 自定义结构体及初始化
  15. python中import用法sys_python sys模块的常见用法汇总
  16. SpringBoot与Shiro整合-权限管理实战
  17. 计算机基础知识第一章测试题,计算机基础知识测试题第一章
  18. cfg80211 subsystem中的wiphy
  19. App测试中有哪些常见的性能测试指标?出具App测试报告的软件测试机构推荐
  20. HTAP 能够取代 OLAP 吗?

热门文章

  1. 酷我音乐PC客户端歌词解密 - nodejs
  2. 关于Tomcat启动闪退的问题
  3. MayaAPI官方案例asciiToBinary的修改,实现ma和mb格式互转
  4. 第一本Python神经网络编程译著图书终于来啦
  5. 【小伟哥AI之路】Jetson Nano之4针PWM风扇转速控制
  6. Java线程池详细介绍——原理及详细使用
  7. 初入职场必读:那些关于你职业生涯和收入的规则和建议
  8. html动感相册怎么转成视频,如何导入要制作成视频的照片和视频短片【详细介绍】...
  9. 阿里如何做好双11技术保障?大队长霜波分享4点经验(转自阿里技术)
  10. 电动汽车充电站(桩)控制系统解决方案