如何根據普通ip地址獲取當前地理位置

前言:

我們現在需要做的功能是根據ip地址獲取當前地理位置,是Web 服務API,獲取後的位置信息一般是城市級別的,不是很精確的那種獲取。

(1)開發者需要做的準備工作:

需要登錄百度地圖的api,點擊web 服務API.

 

開發文檔的地址:http://lbsyun.baidu.com/index.php?title=webapi/ip-api

請求的URL 有倆個,

http://api.map.baidu.com/location/ip?ak=您的AK&ip=您的IP&coor=bd09ll //HTTP協議 

https://api.map.baidu.com/location/ip?ak=您的AK&ip=您的IP&coor=bd09ll //HTTPS協議

請求的參數

其中需要注意的是: ak 是必填的,其他幾個參數可選。

(2)如何申請AK

地址:http://lbsyun.baidu.com/apiconsole/key#/home

首先需要點到我的應用裏面,新建應用,我這下面是已經建好了。

 

 

點擊確定即可。

需要注意的是:ip白名單不建議使用 0.0.0.0/0  可以使用具體的ip名稱。

(3)代碼調用

獲取到的json 我把他複製出來,看一下怎麼解析這個json.  我這裏用的是阿里巴巴下的jsonObject ,所以需要引用jar 包。

{
	"address": "CN|上海|上海|None|CHINANET|0|0",
	"content": {
		"address_detail": {
			"province": "上海市",
			"city": "上海市",
			"street": "",
			"district": "",
			"street_number": "",
			"city_code": 289
		},
		"address": "上海市",
		"point": {
			"x": "13524118.26",
			"y": "3642780.37"
		}
	},
	"status": 0
}

(4)代碼

package com.bos.test;

import com.alibaba.fastjson.JSONObject;
import com.bos.util.SendRequest;


public class MenuTest {

    public static void main(String[] args) {
        String url = "http://api.map.baidu.com/location/ip?&ak=lGhonqkkoUGZurajdlpL1QqciA215kkq";
        JSONObject jsonObject = SendRequest.sendGet2(url);
        System.out.println(jsonObject);
        //獲取content中的值
        JSONObject contentJsonObject = jsonObject.getJSONObject("content");
        System.out.println("contentJsonObject-------"+contentJsonObject);
        //獲取詳細地址數據
        JSONObject addressDetailJsonObject = contentJsonObject.getJSONObject("address_detail");
        String address = addressDetailJsonObject.getString("province");
        String city = addressDetailJsonObject.getString("city");
        System.out.println("address_detail-------"+addressDetailJsonObject);
        System.out.println("address-------"+address);
        System.out.println("city-------"+city);

        String address2 = contentJsonObject.getString("address");
        System.out.println("address2-----"+address2);

        JSONObject pointJsonObject = contentJsonObject.getJSONObject("point");
        String x = pointJsonObject.getString("x");
        String y = pointJsonObject.getString("y");
        System.out.println("x----"+x);
        System.out.println("y----"+y);
    }


}
 /**
     * 發送get 請求,比如定時任務調用的就是請求
     * @param url
     * @return
     */
    public static JSONObject sendGet2(String url) {
        JSONObject jsonObject = null;
        StringBuffer sb = new StringBuffer();
        BufferedReader in = null;
        try {
            String urlName = url;
            URL realUrl = new URL(urlName);
            // 打開和URL之間的連接
            URLConnection conn = realUrl.openConnection();
            // 設置通用的請求屬性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setConnectTimeout(10000);
            // 建立實際的連接
            conn.connect();
            // 定義BufferedReader輸入流來讀取URL的響應
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            jsonObject = JSON.parseObject(sb.toString());
        } catch (Exception e) {
            System.out.println("發送GET請求出現異常!" + e);
            // 使用finally塊來關閉輸入流
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                System.out.println("關閉流異常");
            }
        }
        return jsonObject;
    }

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章