新聞中心
Java根據(jù)地址獲取城市
1、使用第三方API:通過調(diào)用高德地圖、百度地圖等第三方API,根據(jù)地址獲取城市信息。

專業(yè)從事成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司,高端網(wǎng)站制作設(shè)計(jì),微信平臺(tái)小程序開發(fā),網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團(tuán)隊(duì)竭力真誠服務(wù),采用H5技術(shù)+CSS3前端渲染技術(shù),響應(yīng)式網(wǎng)站建設(shè),讓網(wǎng)站在手機(jī)、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項(xiàng)小組,與您實(shí)時(shí)在線互動(dòng),隨時(shí)提供解決方案,暢聊想法和感受。
2、解析方法:將地址字符串按照一定的規(guī)則進(jìn)行解析,提取出城市信息。
Java根據(jù)IP獲取坐標(biāo)
1、使用第三方API:通過調(diào)用騰訊地圖、百度地圖等第三方API,根據(jù)IP獲取坐標(biāo)信息。
2、解析方法:將返回的JSON數(shù)據(jù)解析成Java對象,提取出坐標(biāo)信息。
Java根據(jù)IP獲取城市
1、使用第三方API:通過調(diào)用騰訊地圖、百度地圖等第三方API,根據(jù)IP獲取城市信息。
2、解析方法:將返回的JSON數(shù)據(jù)解析成Java對象,提取出城市信息。
示例代碼
1、Java根據(jù)地址獲取城市(以高德地圖API為例)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetCityByAddress {
public static void main(String[] args) {
String address = "北京市朝陽區(qū)阜通東大街6號";
String key = "你的高德地圖API密鑰";
String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&address=" + address;
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
if (connection.getResponseCode() == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF8"));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
System.out.println("城市信息:" + result.toString());
} else {
System.out.println("請求失敗,錯(cuò)誤碼:" + connection.getResponseCode());
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、Java根據(jù)IP獲取坐標(biāo)(以騰訊地圖API為例)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSON;
public class GetCoordinateByIP {
public static void main(String[] args) {
String ip = "8.8.8.8"; // 目標(biāo)IP地址
String key = "你的騰訊地圖API密鑰"; // 你的騰訊地圖API密鑰
String url = "http://apis.map.qq.com/ws/location/v1/ip?ip=" + ip + "&key=" + key; // 騰訊地圖IP查詢接口地址
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
if (connection.getResponseCode() == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF8"));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
// 解析返回的JSON數(shù)據(jù),提取坐標(biāo)信息
JSONObject jsonObject = JSONObject.parseObject(result.toString());
JSONArray locationInfoList = jsonObject.getJSONArray("location_info");
for (int i = 0; i < locationInfoList.size(); i++) {
JSONObject locationInfo = locationInfoList.getJSONObject(i);
double lng = locationInfo.getDoubleValue("lng"); // 經(jīng)度
double lat = locationInfo.getDoubleValue("lat"); // 緯度
System.out.println("坐標(biāo)信息:" + lng + ", " + lat);
}
} else {
System.out.println("請求失敗,錯(cuò)誤碼:" + connection.getResponseCode());
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文章名稱:java根據(jù)地址過去市,java根據(jù)ip獲取坐標(biāo)(java根據(jù)ip獲取到城市)
轉(zhuǎn)載源于:http://fisionsoft.com.cn/article/dhhjisc.html


咨詢
建站咨詢
