Oct 24, 2007
[WebService] PlaceEngine API にチャレンジ
PlaceEngine とは
PlaceEngine は、Wi-Fi 機器を使って簡単に現在位置を推定してくれるソフトウェア。 以前 Mac OS X にインストールして放置してあったのだけれど、API があるということを知ったので早速試してみた。
- PlaceEngine
- http://www.placeengine.com/
- PlaceEngine 技術資料
- http://www.placeengine.com/doc
- PlaceEngine API ローカルアプリケーションプログラミング
- http://www.placeengine.com/doc/tutl
サンプルコード
サンプルコードは、PlaceEngine API ローカルアプリケーションプログラミングとほとんど同じ。
Mac OS X 版では LocalDB 機能(ローカルに位置情報を記録する機能) は使用できないということなので、PlaceEngine サーバに位置情報を問い合わせるサンプルだけ試してみた。
サンプルアプリを作成するためには、
- ここ から PlaceEngine クライアントをダウンロードし、インストールする。ここでは 2007/10/24 現在最新の o070531 を使用するものとする。
![[PlaceEngine version]](/blog/entries/WebService/20071024_01/PlaceEngine_version.png)
- PlaceEngine を起動する。
- ここで PlaceEngine API アプリケーションキーを取得する。
サンプルを動作させると PlaceEngine がセキュリティ警告ダイアログを表示する。 実行しても安全だと思う場合は YES をクリックする。
package jp.in_vitro.codelets;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Codelet {
private static final String KEY = "aNR3HYXx1jzYqw24...";
private static final String URL_RTAGJS = "http://localhost:5448/rtagjs";
private static final String URL_LOC = "http://www.placeengine.com/api/loc";
private final long t;
public Codelet() {
super();
System.setProperty("http.nonProxyHosts", "localhost");
this.t = System.currentTimeMillis() / 1000;
}
public Loc getLoc() throws IOException {
Rtag rtag = this.getRtag(t);
if (rtag.getNumap() > 0) {
String locRequest = URL_LOC + "?t=" + this.t + "&rtag="
+ rtag.getRtag() + "&appk=" + KEY + "&fmt=json";
StringBuilder locResponse = sendHttpRequest(new URL(locRequest));
return parseLocResponse(locResponse);
}
return null;
}
protected Loc parseLocResponse(final StringBuilder response) {
Loc location = new Loc();
response.deleteCharAt(0);
String responseAsString = new String(response);
String param[] = responseAsString.split(",");
String param2[] = responseAsString.split("\\{");
if (param.length >= 4 && param2.length >= 2) {
location.setLon(param[0]);
location.setLat(param[1]);
String info[] = param2[1].substring(0, param2[1].length() - 1)
.split(",");
for (int i = 0; i < info.length; i++) {
String tk[] = info[i].split(":");
String name = tk[0].substring(1, tk[0].length() - 1);
String value = tk[1];
if ("addr".equals(name)) {
location.setAddr(value);
} else if ("msg".equals(name)) {
location.setMsg(value);
} else if ("floor".equals(name)) {
location.setFloor(value);
} else if ("t".equals(name)) {
location.setT(value);
}
}
}
return location;
}
protected void printLoc(final Loc loc) {
System.out.println("Longitude(経度) : " + loc.getLon());
System.out.println("Latitude(緯度) : " + loc.getLat());
System.out.println("Address(住所) : " + loc.getAddr());
System.out.println("Floor : " + loc.getFloor());
System.out.println("Message : " + loc.getMsg());
}
public Rtag getRtag(final long t) throws IOException {
String rtagRequest = URL_RTAGJS + "?t=" + this.t + "&appk=" + KEY;
StringBuilder rtagResponse = sendHttpRequest(new URL(rtagRequest));
Rtag rtag = this.parseRtagResponse(rtagResponse);
return rtag;
}
protected Rtag parseRtagResponse(final StringBuilder response) {
Rtag rtag = new Rtag();
String splitRtagjsResponse[] = response.substring(9,
response.length() - 2).split(", *");
String r = splitRtagjsResponse[0].substring(1, splitRtagjsResponse[0]
.length() - 1);
int numap = Integer.parseInt(splitRtagjsResponse[1]);
String t = splitRtagjsResponse[2];
rtag.setRtag(r);
rtag.setNumap(numap);
rtag.setT(t);
return rtag;
}
protected StringBuilder sendHttpRequest(final URL url) throws IOException {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection
.getInputStream(), "UTF-8"));
StringBuilder response = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response;
} finally {
try {
if (reader != null) {
reader.close();
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
public static void main(final String[] args) throws Exception {
Codelet me = new Codelet();
Loc loc = me.getLoc();
me.printLoc(loc);
}
public static class Rtag {
private String rtag;
private int numap;
private String t;
public Rtag() {
super();
}
public String getRtag() {
return this.rtag;
}
public void setRtag(final String rtag) {
this.rtag = rtag;
}
public int getNumap() {
return this.numap;
}
public void setNumap(final int numap) {
this.numap = numap;
}
public String getT() {
return this.t;
}
public void setT(final String t) {
this.t = t;
}
}
public static class Loc {
private String lat;
private String lon;
private String addr;
private String msg;
private String floor;
private String t;
public Loc() {
super();
}
public String getLat() {
return this.lat;
}
public void setLat(final String lat) {
this.lat = lat;
}
public String getLon() {
return this.lon;
}
public void setLon(final String lon) {
this.lon = lon;
}
public String getAddr() {
return this.addr;
}
public void setAddr(final String addr) {
this.addr = addr;
}
public String getMsg() {
return this.msg;
}
public void setMsg(final String msg) {
this.msg = msg;
}
public String getFloor() {
return this.floor;
}
public void setFloor(final String floor) {
this.floor = floor;
}
public String getT() {
return this.t;
}
public void setT(final String t) {
this.t = t;
}
}
}
サンプルコードの実行結果
Longitude(経度) : 13*.****** Latitude(緯度) : 3*.****** Address(住所) : '東京都************' Floor : null Message : '位置が取得できました'



