2010年8月11日 星期三

Gmap 入門 (一)

1. Gmap API 的申請
http://code.google.com/intl/zh-TW/apis/maps/signup.html
你必須要申請API,Google才知道你是誰,當然 Google 也可由此知道誰在偷偷用他的地圖服務,聽 起來很可怕吧,沒有做什麼壞事情就不用想太多。

例如:我申請一組 www.xxx.com
GMap會給我 ABQIAAAAb37BxmszgAN90DlQF2kuXxQK7D9hMb7A52EddnovcMgPuF8behQgpef_nt5fmn

我當然也不可能貼一組可以用的,自己去申請一下吧

2. Gmap提供幾種使用Map的方式
2.1 JavaScript Maps
2.2 Maps API for Flash
2.3 HTTP Service

我自己沒使用過 Flash版的,所以以下我就先不介紹


Google API Reference
http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/reference.html

Gmap Reverse Geocoding

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);
}
}


}

2010流年不順

距離上寫網誌也快2年了,最近發生了很多事,今年上半年不管做什麼事情都很不順心。
今天心血來潮,決定回來維護一下自己網誌。