1. Reverse Geocoding
(反向地理編碼說明 http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/services.html#ReverseGeocoding)
以上此做法是在Client 去執行的,所以前端要做大量住址反查時,不是一個最佳解法
以下為官網的Sample
var map;
var geocoder;
var address;
function initialize() {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(40.730885,-73.997383), 15);
map.addControl(new GLargeMapControl);
GEvent.addListener(map, "click", getAddress);
geocoder = new GClientGeocoder();
}
function getAddress(overlay, latlng) {
if (latlng != null) {
address = latlng;
geocoder.getLocations(latlng, showAddress);
}
}
function showAddress(response) {
map.clearOverlays();
if (!response || response.Status.code != 200) {
alert("Status Code:" + response.Status.code);
} else {
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(
'orig latlng:' + response.name + '
' +
'latlng:' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '
' +
'Status Code:' + response.Status.code + '
' +
'Status Request:' + response.Status.request + '
' +
'Address:' + place.address + '
' +
'Accuracy:' + place.AddressDetails.Accuracy + '
' +
'Country code: ' + place.AddressDetails.Country.CountryNameCode);
}
}
2. Google Maps API Web Services
參考資料:http://code.google.com/intl/zh-TW/apis/maps/documentation/geocoding/
說明:
Google Maps API Web Services
1. http://maps.google.com/maps/api/geocode/json?latlng=22.67744,120.34423&sensor=false®ion=tw&language=zh-TW
2. 可透過HttpClient 將url 送出,可以收到一個 JSON
3. 再透過 JSON去拆解 Google 吐回來的 JSON字串
/**
*
* @author 熊爸天下
*/
public class GmapUtil {
private static final String gmapUrl = "http://maps.google.com/maps/api/geocode/json?";
/**
* 經緯度反查住址
* @param Latitude 東經
* @param longitude 北緯
* @return 回傳住址
* @throws UnsupportedEncodingException
* @throws IOException
*/
public String LatlonToAddress(String Latitude, String longitude) throws UnsupportedEncodingException, IOException {
//ex:http://maps.google.com/maps/api/geocode/json?latlng=22.67744,120.34423&sensor=false®ion=tw&language=zh-TW
StringBuffer url = new StringBuffer();
url.append(gmapUrl);
url.append("latlng=").append(Latitude).append(",").append(longitude);
url.append("&sensor=false®ion=tw&language=zh-TW");
byte[] addr = getJsonObject(url.toString());
return this.getMapAddress(SplitArrayObject(addr));
}
private byte[] getJsonObject(String urlStr) throws UnsupportedEncodingException, IOException {
HttpClient httpClient = new HttpClient();
HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
// UTF-8
managerParams.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
//設定connect時間(單位毫秒)
managerParams.setConnectionTimeout(10000);
//設定connect Timeout時間(單位毫秒)
managerParams.setSoTimeout(20000);
GetMethod method = new GetMethod(urlStr);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
int statusCode = 0;
try {
statusCode = httpClient.executeMethod(method);
} catch (IOException ex) {
ex.printStackTrace();
}
byte[] responseBody = null;
if (statusCode != HttpStatus.SC_OK) {
} else {
try {
responseBody = method.getResponseBody();
} catch (IOException e) {
}
method.releaseConnection();
}
return responseBody;
}
public String SplitArrayObject(byte[] responseBody) throws UnsupportedEncodingException {
String json = "";
String address = "";
if (responseBody.length == 0) {
return "";
}
json = new String(responseBody, "UTF-8").trim();
return json;
}
public String getMapAddress(String json) throws UnsupportedEncodingException {
String address = "";
if (!json.equals("") && !json.equals("{}")) {
JSONArray obj = null;
try {
obj = new JSONArray();
obj.add(json);
String rs = obj.getJSONObject(0).getString("results");
JSONArray obj_detail = new JSONArray();
obj_detail.add(rs);
address = obj_detail.getJSONArray(0).getJSONObject(0).getString("formatted_address");
} catch (JSONException e) {
}
}
return address;
}
public static void main(String[] arg) {
GmapUtil gu = new GmapUtil();
try {
System.out.println(gu.LatlonToAddress("22.63434", "120.33406"));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(GmapUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(GmapUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
沒有留言:
張貼留言