获取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('获取定位失败'));
            }
          });
        });
      });
    },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中获取用户公网IP内网IP需要进行不同的操作: 1. 获取公网IP 获取公网IP可以通过访问外部网站来获取,可以通过以下代码实现: ```java import java.net.InetAddress; import java.net.UnknownHostException; import java.io.IOException; public class PublicIP { public static void main(String[] args) { try { InetAddress addr = InetAddress.getByName("www.baidu.com"); System.out.println("Public IP: " + addr.getHostAddress()); } catch (UnknownHostException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们通过访问百度网站来获取公网IP。 2. 获取内网IP 获取内网IP需要通过访问本地网络接口来获取,可以通过以下代码实现: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class LocalIP { public static void main(String[] args) { try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()){ NetworkInterface ni = interfaces.nextElement(); Enumeration<InetAddress> addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()){ InetAddress addr = addresses.nextElement(); if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && addr.isSiteLocalAddress()){ System.out.println("Local IP: " + addr.getHostAddress()); } } } } catch (SocketException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们通过遍历本地网络接口来获取内网IP。在获取到的IP地址中,排除了本地链路地址和回环地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值