标签:blog http java 使用 os io for ar
做项目时,遇到对地点获取地图中对应的经纬度,作一下笔记,以备以后直接使用
package com.hpzx.data;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.hpzx.json.HTTPTokener;
import com.hpzx.json.JSONException;
/**
* 获取经纬度通过
*
* @author jueyue 返回格式:Map<String,Object> map map.put("status",
* reader.nextString());//状态 map.put("result", list);//查询结果
* list<map<String,String>> 密钥:f247cdb592eb43ebac6ccd27f796e2d2
*/
public class GetLatAndLngByBaidu {
/**
* @param addr
* 查询的地址
* @return
* @throws IOException
*/
public static String getCoordinate(String addr) throws IOException {
String address = null;
try {
address = java.net.URLEncoder.encode(addr, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String key = "f247cdb592eb43ebac6ccd27f796e2d2";
String url = String
.format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s",
address, key);
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
httpsConn = (URLConnection) myURL.openConnection();
// 不使用代理
if (httpsConn != null) {
InputStreamReader insr = null;
insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
StringBuffer buf = new StringBuffer();
String data = null;
while ((data = br.readLine()) != null) {
buf.append(data.replace(":", ""));
// System.out.println(data);
}
try {
return getLoc(buf.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public static String getLoc(String string) throws JSONException {
StringBuffer buf = new StringBuffer();
HTTPTokener x = new HTTPTokener(string);
String t;
t = x.nextToken();
while (x.more()) {
t = x.nextToken();
if (t.equalsIgnoreCase("lng")) {
break;
}
}
if (t.equalsIgnoreCase("lng")) {
buf.append(x.nextToken());
if (x.nextToken().equals("lat")) {
buf.append(x.nextToken());
}
return buf.toString();
}
return null;
}
public void getCoordinate(String longitude, String latitude) {
String url = String
.format("http://api.map.baidu.com/geocoder?output=json&"
+ "location=%s,%s%s&key=f247cdb592eb43ebac6ccd27f796e2d2",
latitude, "%20", longitude);
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
insr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
String str = "新疆维吾尔自治区哈密市南湖乡大南湖矿区";
GetLatAndLngByBaidu lotAndLng = new GetLatAndLngByBaidu();
String result = GetLatAndLngByBaidu.getCoordinate(str).replace(":", "");
System.out.println(result);
String[] loc = result.split(",");
lotAndLng.getCoordinate(loc[0], loc[1]);
}
}
根据百度地图API获取指定地点的经纬度,布布扣,bubuko.com
标签:blog http java 使用 os io for ar
原文地址:http://www.cnblogs.com/yuwenfeng/p/3927291.html