第三方免费开放API-查询用户IP及所在地理位置,天气查询

跨域相关文章: https://mp.csdn.net/postedit/85329628

 

 

 

(1)126api (速度较慢)

http://ip.ws.126.net/ipquery?ip=IP

https://ip.ws.126.net/ipquery?ip=IP (也支持https协议)

返回结果如下:

var lo="广东省", lc="广州市"; var localAddress={city:"广州市", province:"广东省"}

请求方式:

var url = "https://ip.ws.126.net/ipquery?ip=IP"; // 查询当前用户ip所在地理位置
// var url = "https://ip.ws.126.net/ipquery?ip=36.32.224.255"; // 查询某一ip所在地理位置
$.getScript(url, function(){

            var cityName = lc; // 城市名
            
            });

        });

 

(2)搜狐api

http://pv.sohu.com/cityjson?ie=utf-8 接口在4G网络或者其他情况返回的城市代码为CN,城市名为CHINA,不够精确,不建议使用

 

(3)太平洋:

http://whois.pconline.com.cn/ 

 

(4)百度:

http://lbsyun.baidu.com/index.php?title=webapi/ip-api

百度天气:

https://www.cnblogs.com/wangchengshen/p/3668946.html

https://blog.csdn.net/ixiaoyang/article/details/73556701

 

(5)淘宝

淘宝获取本机IP地址

接口地址:http://www.taobao.com/help/getip.php

传递参数:无

返回类型:jsonp

callback:ipCallback

返回值:

  • ip:本机IP地址

请求示例:

  1. Request URL:http://www.taobao.com/help/getip.php

返回示例:

  1. ipCallback({ip:"115.159.152.210"})

备注:本接口只有返回IP地址的功能

 

淘宝获取IP详细信息

接口地址:http://ip.taobao.com/service/getIpInfo.php

传递参数:

  • ip:要查询的IP地址

参数传递方式:GET/POST

返回类型:json

返回值:

  • code:错误码(为零代表请求成功)
  • country:国名
  • country_id:国名(英文缩写)
  • area:地域(如:华东)
  • area_id:地域ID
  • region:行政区
  • region_id:行政区ID
  • city:城市名
  • city_id:城市ID
  • isp:网络提供商
  • isp_id:网络提供商ID
  • ip:请求的IP地址

请求示例:

  1. Request URL:http://ip.taobao.com/service/getIpInfo.php?ip=115.159.152.210

返回示例:

  1. {
  2.     "code":0,
  3.     "data":{
  4.         "country":"中国",
  5.         "country_id":"CN",
  6.         "area":"华东",
  7.         "area_id":"300000",
  8.         "region":"上海市",
  9.         "region_id":"310000",
  10.         "city":"上海市",
  11.         "city_id":"310100",
  12.         "county":"",
  13.         "county_id":"-1",
  14.         "isp":"腾讯网络",
  15.         "isp_id":"1000153",
  16.         "ip":"115.159.152.210"
  17.     }
  18. }

备注:本接口来自淘宝IP地址库

 

查询天气: 以下三方api地址均可使用http或https

// 方式一: 太平洋API:查询当前用户位置信息 (推荐)
        $.ajax({
            type: 'get',
            url: "https://whois.pconline.com.cn/ipJson.jsp",
            dataType: 'jsonp',
            data: {
            },
            success: function (ret) {
                var cityName = ret.city;
                // 百度API:通过城市名查询天气
                $.ajax({
                    type: 'get',
                    url: 'https://api.map.baidu.com/telematics/v3/weather',
                    dataType: 'jsonp',
                    data: {
                        location: cityName,
                        output: 'json',
                        ak: constant.WEATHER.AK
                    },
                    success: function (res) {
                        if (res.status === 'success') {
                            var data = res.results[0];
                            var weather_data = data.weather_data[0];
                            var temperature = data.weather_data[0].date.split(':')[1].split(')')[0];
                            $('#position').text(data.currentCity);
                            $('#temperature').text(temperature);
                            $('#weather_now').text(weather_data.weather);
                            $('#weather_temperature').text(weather_data.temperature.replace('~', '/'));
                        } else {
                            console.log('天气接口报错' + res.error);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                    }
                });

            }
        });


// 方式二:百度API:查询当前用户位置信息 (推荐)
        $.ajax({
            type: 'get',
            url: "https://api.map.baidu.com/location/ip",
            dataType: 'jsonp',
            data: {
                ak: constant.WEATHER.AK
            },
            success: function (ret) {
                var cityName = ret.content.address;
                // 百度API:通过城市名查询天气
                $.ajax({
                    type: 'get',
                    url: 'https://api.map.baidu.com/telematics/v3/weather',
                    dataType: 'jsonp',
                    data: {
                        location: cityName,
                        output: 'json',
                        ak: constant.WEATHER.AK
                    },
                    success: function (res) {
                        if (res.status === 'success') {
                            var data = res.results[0];
                            var weather_data = data.weather_data[0];
                            var temperature = data.weather_data[0].date.split(':')[1].split(')')[0];
                            $('#position').text(data.currentCity);
                            $('#temperature').text(temperature);
                            $('#weather_now').text(weather_data.weather);
                            $('#weather_temperature').text(weather_data.temperature.replace('~', '/'));
                        } else {
                            console.log('天气接口报错' + res.error);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                    }
                });
            }
        });


 // 方式三:126API:查询当前用户位置信息 (速度较慢)
        $.getScript("https://ip.ws.126.net/ipquery?ip=IP", function(){
            var cityName = lc;
            // 百度API:通过城市名查询天气
            $.ajax({
                type: 'get',
                url: 'https://api.map.baidu.com/telematics/v3/weather',
                dataType: 'jsonp',
                data: {
                    location: cityName,
                    output: 'json',
                    ak: constant.WEATHER.AK
                },
                success: function (res) {
                    if (res.status === 'success') {
                        var data = res.results[0];
                        var weather_data = data.weather_data[0];
                        var temperature = data.weather_data[0].date.split(':')[1].split(')')[0];

                        $('#position').text(data.currentCity);
                        $('#temperature').text(temperature);
                        $('#weather_now').text(weather_data.weather);
                        $('#weather_temperature').text(weather_data.temperature.replace('~', '/'));
                    } else {
                        console.log('天气接口报错' + res.error);
                    }
                },
                error: function (error) {
                    console.log(error);
                }
            });

        });


 

曾经有过的-百度高精度IP定位服务

https://www.jianshu.com/p/ae46bc471ac2

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
通常情况下,我们可以使用第三方IP地址解析库来获取IP地址地理位置信息。以下是使用GeoIP2 Java API进行IP地址解析的示例代码: 1. 首先,将GeoIP2 Java API添加到项目的依赖中。可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.maxmind.geoip2</groupId> <artifactId>geoip2</artifactId> <version>2.14.3</version> </dependency> ``` 2. 然后,使用以下代码来解析IP地址: ```java import java.net.InetAddress; import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.exception.GeoIp2Exception; import com.maxmind.geoip2.model.CityResponse; public class GeoIpResolver { private final DatabaseReader reader; public GeoIpResolver() throws IOException { reader = new DatabaseReader.Builder(new File("path/to/GeoLite2-City.mmdb")).build(); } public String resolve(String ipAddress) throws IOException, GeoIp2Exception { InetAddress inetAddress = InetAddress.getByName(ipAddress); CityResponse response = reader.city(inetAddress); return response.getCity().getName() + ", " + response.getCountry().getName(); } } ``` 在上面的代码中,我们使用了`DatabaseReader`类来读取MMDB格式的IP地址库文件。然后,我们使用`reader.city(inetAddress)`方法来解析IP地址获取城市和国家信息。最后,我们将城市和国家信息组合成一个字符串返回。 请注意,上述代码中的`path/to/GeoLite2-City.mmdb`应该替换为实际的IP地址库文件路径。你可以从MaxMind网站上下载免费的GeoLite2 IP地址库文件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值