地址解析就是將地址(如:廣東省廣州市)轉換爲地理座標(如經度: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; } |