java根據經度緯度獲取地理資訊?

根據經度緯度去獲取地理位置的資訊

工具/原料

Eclipse

方法/步驟

新建一個java專案,新增一個類,類裡面新增下面的方法:

public static String GetLocationMsg(double latitude,double longitude){

String message = "";

String url = String.format(

",%s&language=CN",

latitude,longitude);

URL myURL = null;

URLConnection httpsConn = null;

try {

myURL = new URL(url);

} catch (MalformedURLException e) {

e.printStackTrace();

}

try {

httpsConn = (URLConnection) myURL.openConnection();

if (httpsConn != null) {

InputStreamReader insr = new InputStreamReader(

httpsConn.getInputStream(), "UTF-8");

BufferedReader br = new BufferedReader(insr);

String data = null;

while ((data = br.readLine()) != null) {

message = message+data;

}

insr.close();

}

} catch (IOException e) {

e.printStackTrace();

}

return message;

}

在main函式呼叫步驟一中的方法:GetLocationMsg

public final static void main(String[] args) {

System.err.println(GetLocationMsg(32.7763644055,100.4338731743));

}

注意事項

經度和緯度不要隨便亂填,亂填可能沒有資料,可以用線上工具獲取到經度緯度資料再測試

相關問題答案