百度地圖運用體會

根據詳情地址訪問百度服務器獲得經緯度或者根據經緯度訪問百度服務器獲得詳情地址的代碼寫的不好的請大家指正一下:
package com.hzsc.smanage.bus.provider.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;

import net.minidev.json.parser.ParseException;

/**
*

  • 通過百度地圖獲取地址

*/
public class BaiduUtils {

/**
*

  • @param lat
  •        維度
    
  • @param lng
  •        精度
    
  • @return ak 自己申請的,密匙
  • @throws IOException
    */
    public static Map<String, String> testPost(Double lat, Double lng, String ak) throws IOException {
    URL url = new URL(“http://t.cn/Ai8PcGHK” + ak + “=” + ak + “&callback=renderReverse&location=”
    • lat + “,” + lng + “&output=json”);
      URLConnection connection = url.openConnection();

connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), “utf-8”);

out.flush();
out.close();

InputStream l_urlStream = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(l_urlStream, “UTF-8”));
StringBuilder sb = new StringBuilder("");
String res;
while ((res = in.readLine()) != null) {
// String res;
sb.append(res.trim());
}
String str = sb.toString();
System.out.println(str);
Map<String, String> map = null;
if (StringUtils.isNotEmpty(str)) {
int addStart = str.indexOf(“formatted_address”😊;
int addEnd = str.indexOf("",“business”);
if ((addStart > 0) && (addEnd > 0)) {
String address = str.substring(addStart + 20, addEnd);
map = new HashMap();
map.put(“address”, address);
return map;
}
}
return null;
}

/**

  • 查詢詳情地址
  • @param addr
  • @param ak
  • @return
  • @throws ParseException
    */
    public static Map<String, String> getCoordinate(String addr, String key) throws Exception {
    String address = null;
    Map<String, String> map = new HashMap<String, String>();
    try {
    address = java.net.URLEncoder.encode(addr, “UTF-8”);
    } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
    }

String url = String.format(“http://t.cn/zYuFrm1%s&output=json&key=%s”, address, key);
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
InputStreamReader insr = null;
BufferedReader br = null;
try {
httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
if (httpsConn != null) {
insr = new InputStreamReader(httpsConn.getInputStream(), “UTF-8”);
br = new BufferedReader(insr);
String data = null;
StringBuilder sb = new StringBuilder("");
while ((data = br.readLine()) != null) {
sb.append(data.trim());
}
String str = sb.toString();
JSONObject json = JSONObject.parseObject(str);
JSONObject result = (JSONObject) json.get(“result”);
JSONObject location = (JSONObject) result.get(“location”);
String lng = location.getString(“lng”);
String lat = location.getString(“lat”);
map.put(“lng”, lng);
map.put(“lat”, lat);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (insr != null) {
insr.close();
}
if (br != null) {
br.close();
}
}
return map;
}

// public static void main(String[] args) throws Exception {
// Map<String, String> map =
// BaiduUtils.getCoordinate(“廣東省廣州市荔灣區信聯街12號”,ConstantUtils.AK);
// System.out.println(map.get(“lat”));
// System.out.println(map.get(“lng”));
// //System.out.println(testPost(23.1, 113.25, ConstantUtils.AK));
//
//
// }
}

package com.hzsc.smanage.bus.provider.utils;

public class ConstantUtils {
/**

  • 百度地圖自己的祕鑰 xDbGLEyjGu2bOci0vqc5tZGb3bRFOL9q
    */
    public static final String AK=“LVHbkRuAuATKebFIQDVsvMGQUkkClEOe”;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章