地址解析就是将地址(如:广东省广州市)转换为地理座标(如经度:113.26442,纬度:23.129074)的过程。google map api提供两种方法实现地址解析。
第一种,是通过使用 GClientGeocoder 对象来实现,大家可以参考google map api的相关文档,以下是摘自google的相关代码
var map = new GMap2(document.getElementById("map_canvas"));
var geocoder = new GClientGeocoder();
function showAddress(address) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert("无法解析:" + address);
} else {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(address);
}
}
);
}var map = new GMap2(document.getElementById("map_canvas"));
var geocoder = new GClientGeocoder();
function showAddress(address) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert("无法解析:" + address);
} else {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(address);
}
}
);
}
var map = new GMap2(document.getElementById("map_canvas")); var geocoder = new GClientGeocoder(); function showAddress(address) { geocoder.getLatLng( address, function(point) { if (!point) { alert("无法解析:" + address); } else { map.setCenter(point, 13); var marker = new GMarker(point); map.addOverlay(marker); marker.openInfoWindowHtml(address); } } ); } |
第二种方法,就是通过HTTP请求直接访问。调用参数等相关说明请参考http://code.google.com/intl/zh-CN/apis/maps/documentation/services.html
/**
* 利用googlemap api 通过 HTTP 进行地址解析
* @param address 地址
* @return HTTP状态代码,精确度(请参见精确度常数),纬度,经度
*/
private String getLatlng(String address){
String ret = "";
if(address != null && !address.equals("")){
try {
address = URLEncoder.encode(address,"UTF-8");//进行这一步是为了避免乱码
} catch (UnsupportedEncodingException e1) {
logger.error("转码失败", e1);
}
String[] arr = new String[4];
arr[0] = address;
arr[1] = OUTPUT;
arr[2] = SENSOR;
arr[3] = KEY;
String url = MessageFormat.format("http://maps.google.com/maps/geo?q={0}&output={1}&sensor={2}&key={3}",arr);
URL urlmy = null;
try {
urlmy = new URL(url);
HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
con.setFollowRedirects (true );
con.setInstanceFollowRedirects(false );
con.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
String s = "";
StringBuffer sb = new StringBuffer("");
while ((s = br.readLine()) != null ) {
sb.append(s+"/r/n");
}
ret = ""+sb;
} catch (MalformedURLException e) {
logger.error("通过http方式获取地址信息失败", e);
} catch (IOException e) {
logger.error("文件读取失败", e);
}
}
return ret;
}
而通过java实现的方法如下
/** * 利用googlemap api 通过 HTTP 进行地址解析 * @param address 地址 * @return HTTP状态代码,精确度(请参见精确度常数),纬度,经度 */ private String getLatlng(String address){ String ret = ""; if(address != null && !address.equals("")){ try { address = URLEncoder.encode(address,"UTF-8");//进行这一步是为了避免乱码 } catch (UnsupportedEncodingException e1) { logger.error("转码失败", e1); } String[] arr = new String[4]; arr[0] = address; arr[1] = OUTPUT; arr[2] = SENSOR; arr[3] = KEY; String url = MessageFormat.format("http://maps.google.com/maps/geo?q={0}&output={1}&sensor={2}&key={3}",arr); URL urlmy = null; try { urlmy = new URL(url); HttpURLConnection con = (HttpURLConnection) urlmy.openConnection(); con.setFollowRedirects (true ); con.setInstanceFollowRedirects(false ); con.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8")); String s = ""; StringBuffer sb = new StringBuffer(""); while ((s = br.readLine()) != null ) { sb.append(s+"/r/n"); } ret = ""+sb; } catch (MalformedURLException e) { logger.error("通过http方式获取地址信息失败", e); } catch (IOException e) { logger.error("文件读取失败", e); } } return ret; } |