歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 如何把IP轉換成經緯度(Java版)

如何把IP轉換成經緯度(Java版)

日期:2017/3/1 9:09:12   编辑:Linux編程

經常有這種需求,擁有用戶的IP地址,想要在地圖上顯示用戶的訪問量。這個時候就需要用到經緯度...應為一般的地圖插件都是基於經緯度的。
那麼問題來了,如何把IP轉換成經緯度?

百度API

最國產的方式,就是使用百度API了,百度提供了兩種服務:

普通的IP服務:http://lbsyun.baidu.com/index.php?title=webapi/ip-api

https://api.map.baidu.com/location/ip?ak=請輸入您的AK&coor=bd09ll

返回值:

{  
    address: "CN|吉林|長春|None|CERNET|1|None",  
    content: 
    {  
        address: "吉林省長春市",  
        address_detail: 
        {  
            city: "長春市",  
            city_code: 53,  
            district: "",  
            province: "吉林省",  
            street: "",  
            street_number: ""  
        },  
        point: 
        {       
            x: "125.31364243",      
            y: "43.89833761"  
        }  
    },  
    status: 0  
}

精准的服務:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip

https://api.map.baidu.com/highacciploc/v1?qcip=220.181.38.113&qterm=pc&ak=請輸入您的AK&coord=bd09ll

返回值:

{
    content: {
        location: {
            lat: 40.047726,#緯度
            lng: 116.313304 #經度  
        },
        locid: "8b1bf81d208bc2ce657fb6e6c270de66",#定位結果唯一ID
        radius: 30, #定位結果半徑
        confidence: 1 #定位結果可信度
    },
    result: {
        error: 161,#定位結果狀態碼
        loc_time: "2016-08-23 15:14:12"#定位時間
    }
}

這個API也不是隨便問的,首先就需要注冊;每個時間段的訪問量還有限...因此不適合做數據分析使用。因為數據分析往往是大批量的數據同時去進行經緯度的轉換。

Logstash進行轉換

Logstash本身提供了IP地址轉換成經緯度的功能:

input{
    file{
        path => "D:\access.json"
        start_position => "beginning"
    }   
}
filter{
    json{
        source => "message"
    }
    date{
        match => ["time","yyyy-MM-dd HH:mm:ss"]
        timezone => "Asia/Shanghai"
    }
    geoip {
                source => "ip"
                target => "geoip"
    }
}
output{
    stdout{
            codec => rubydebug
    }
}

MaxMind提供的GeoIp服務

這個公司提供了GeoIp的轉換服務,當然如果想要精確的匹配也是收費的。

這裡有一個體驗的網址:https://www.maxmind.com/en/geoip-demo

東拼西湊山寨方案

這個山寨的方案靈感來源於Logstash,Logstash本身提供了IP轉換經緯度的功能。原理就是它自己有一個IP數據庫,可以通過它執行查詢。其實這個數據庫時老版的MaxMind提供的數據文件,湊合用吧!新的需要花錢呀!

廢話不多說,在Java中想要使用這個數據文件需要下載相應的Jar包和dat文件:

  • GeoIP jar包:geoip-api-1.3.1.jar
  • Geo city dat文件:GeoLiteCity-2013-01-18.dat

把dat文件放在自己的本地目錄,然後項目中導入geoip.jar即可:

import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import java.io.IOException;

public class TestMain {
    public static void main(String[] args) {
        try {
            LookupService cl = new LookupService("D:/lib/geoip/GeoLiteCity-2013-01-18.dat", LookupService.GEOIP_MEMORY_CACHE);
            Location l2 = cl.getLocation("144.0.9.29");
            System.out.println(
                    "countryCode: " + l2.countryCode +"\n"+
                    "countryName: " + l2.countryName +"\n"+
                    "region: " + l2.region +"\n"+
                    "city: " + l2.city +"\n"+
                    "latitude: " + l2.latitude +"\n"+
                    "longitude: " + l2.longitude);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

輸出內容:

countryCode: CN
countryName: China
region: 25
city: Jinan
latitude: 36.668304
longitude: 116.99719

最後曬一個圖,興奮一下

參考

1 國外Geoip服務 MaxMind:https://www.maxmind.com/en/geoip-demo
2 國內Geoip服務 百度開放API: http://lbsyun.baidu.com/index.php?title=webapi/ip-api

Copyright © Linux教程網 All Rights Reserved