知道经纬度获取地理位置
public static void main(String[] args) throws Exception {
String latitude = "34.22259";
String longitude = "108.94878";
String url = "https://apis.map.qq.com/ws/geocoder/v1/?location="+latitude+","+longitude+"&key=个人key值"+"&get_poi=1";
JSONObject httpJson = getHttpJson(url);
System.out.println(httpJson);
}
public static JSONObject getHttpJson(String url) throws Exception {
try {
URL realUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
//请求成功
if (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//10KB的缓存
byte[] buffer = new byte[10240];
int len = 0;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String jsonString = baos.toString();
baos.close();
is.close();
JSONObject jsonArray = JSONObject.parseObject(jsonString);
return jsonArray;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
微信小程序获取经纬度
偏差问题
备注:真机模拟的获取到的地址也存在着偏差,不能精准定位到当前位置,如果需要精确地址,需要客户人工手动调整;