当前位置: 首页 > news >正文

获取ip(公网和内网) 前端通过高德api获取位置信息

获取ip(公网和内网) 前端通过高德api获取位置信息

获取ip

//获取公网ip
getIp() {this.$axios.get('http://api.ipify.org').then((res) => {if (res) {console.log(res, '公网ip');}}).catch((e) => {console.log('e', e);});
},//获取内网ip
this.getIP((ip) => {console.log('内网ip', ip);
});getIP(onNewIP) {// 获取用户IP地址let MyPeerConnection =window.RTCPeerConnection ||window.mozRTCPeerConnection ||window.webkitRTCPeerConnection;let pc = new MyPeerConnection({iceServers: [],});let noop = () => {};let localIPs = {};let ipRegex =/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;let iterateIP = (ip) => {if (!localIPs[ip]) onNewIP(ip);localIPs[ip] = true;};pc.createDataChannel('');pc.createOffer().then((sdp) => {sdp.sdp.split('\n').forEach(function (line) {if (line.indexOf('candidate') < 0) return;line.match(ipRegex).forEach(iterateIP);});pc.setLocalDescription(sdp, noop, noop);}).catch((reason) => {});pc.onicecandidate = (ice) => {if (!ice ||!ice.candidate ||!ice.candidate.candidate ||!ice.candidate.candidate.match(ipRegex))return;ice.candidate.candidate.match(ipRegex).forEach(iterateIP);};},

判断手机类型

const u = navigator.userAgent,
app = navigator.appVersion;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // 安卓
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
//isAndroid和 isIOS是boolean类型

与安卓交互

created() {window.appToLocation = this.appToLocation; // 获取定位信息回调
}
methods: {
appToLocation(paramOne,paramTwo,paramThree,paramFour,paramFive,paramSix,paramSeven,paramEight) {console.log(paramOne,paramTwo,paramThree)},}

判断浏览器(移动端)

isWeixin() {//判断是否是微信内置浏览器return navigator.userAgent.indexOf('MicroMessenger') > 0;},//钉钉window.navigator.userAgent.includes('DingTalk')

前端通过高德api获取位置信息

1.引用(index.html)

<script type="text/javascript" src="https://webapi.amap.com/maps?
v=2.0&key=你自己的key&plugin=AMap.Geolocation"></script>

2.配置vue.config.js

chainWebpack: (config) => {config.resolve.alias.set('@', resolve('src')).set('@c', resolve('src/components')).set('@u', resolve('src/utils'))//高德config.externals({AMap: 'AMap',AMapUI: 'AMapUI',})},

3.获取位置信息

this.getMap().then((result) => {console.log('定位', result);
})getMap() {//前端获取位置信息return new Promise((resolve, reject) => {AMap.plugin('AMap.Geolocation', () => {var geolocation = new AMap.Geolocation({enableHighAccuracy: true,useNative: true,timeout: 10000,needAddress: true,});geolocation.getCityInfo((status, result) => {if (status === 'complete') {resolve(result);} else {reject(new Error('获取定位失败'));}});});});},
http://www.lryc.cn/news/149457.html

相关文章:

  • linux打开端口命令是什么
  • 从《孤注一掷》出发,聊聊 SSL 证书的重要性
  • 专题:曲面的切平面、法线
  • 数据结构:排序解析
  • Revit SDK:AutoJoin 自动合并体量
  • MYSQL(索引、事务)
  • 部署问题集合(二十三)设置Docker容器内的中文字符集,解决某些情况下中文乱码的问题
  • Web AP—PC端网页特效
  • Spring线程池ThreadPoolTaskExecutor使用
  • spring mvc的执行流程
  • docker作业
  • java实现本地文件转文件流发送到前端
  • 2020ICPC南京站
  • Linux 中的 chsh 命令及示例
  • JavaScript 数组如何实现冒泡排序?
  • ZooKeeper集群环境搭建
  • 【跟小嘉学 Rust 编程】二十、进阶扩展
  • pytorch学习过程中一些基础语法
  • 判断聚类 n_clusters
  • 基于深度学习的网络异常检测方法研究
  • SSM 基于注解的整合实现
  • 工具类APP如何解决黏性差、停留短、打开率低等痛点?
  • 使用Java MVC开发高效、可扩展的Web应用
  • wandb安装方法及本地部署教程
  • stable diffusion实践操作-提示词插件安装与使用
  • 【SpringBoot】详细介绍SpringBoot中的bean
  • 【Nuxt实战】在Nuxt3项目中如何按需引入Element-plus
  • 专业制造一体化ERP系统,专注于制造工厂生产管理信息化,可定制-亿发
  • Linux工具
  • Java项目-苍穹外卖-Day07-redis缓存应用-SpringCache/购物车功能