java 对接高德地图,通过经纬度获取当前定位的代码
时间: 2023-09-30 09:08:49 浏览: 429
以下是 Java 对接高德地图,通过经纬度获取当前定位的代码示例:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class Location {
public static void main(String[] args) {
try {
String url = "http://restapi.amap.com/v3/geocode/regeo?key=您的key&location=经度,纬度&poitype=&radius=1000&extensions=all&batch=false&roadlevel=0";
// 经度和纬度需要替换成实际的值
url = url.replace("经度", "116.481488").replace("纬度", "39.990464");
String result = sendGet(url);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
}
```
其中,key 是高德地图开放平台申请的开发者 Key,location 是经纬度信息,poitype 是搜索类型,radius 是搜索半径,extensions 是返回结果的扩展信息,batch 是批量查询,roadlevel 是道路等级。具体参数含义可以参考高德地图开放平台的文档。
阅读全文