注意事项:
1. 使用 百度地图API 需要先申请 ak(API 密钥)。
事例 java 代码如下:
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import .MalformedURLException;
import .URL;
import .URLConnection;
import net.sf.json.JSONObject;
public class TestBaiduMapAPI {
/*** description: This method was used to get longitude and latitude. * @param address*/public static void test(String address){// address: 地址output:输出格式ak:是申请的百度 api 密钥String path = "http://api./geocoder/v2/?address="+address+"&output=json&ak=####################";String lineData = null;StringBuilder sb = new StringBuilder();try {URL url = new URL(path);URLConnection conn = url.openConnection();BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));while((lineData = br.readLine()) != null){sb.append(lineData);}JSONObject json = JSONObject.fromObject(sb.toString());JSONObject result = json.getJSONObject("result").getJSONObject("location");String lng = result.getString("lng");String lat = result.getString("lat");System.out.println("coordinate:("+lng+", "+ lat+");");} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {test("中国江苏省连云港市");}
}