●概要
携帯のGPS機能を使い位置情報を取得し、一日分の位置情報をGoogleマップへ表示するツールを作ってみた。現在位置を取得すると、位置情報データは随時サーバへ記録、日ごとに1つのKMLファイルとして保存される。釣りのポイントやハイキングでの絶景スポットなどの位置情報を、後にGoogleマップで管理することを想定して作成した。
●流れ
1.携帯電話のGPS機能を使い、位置情報取得。
2.位置情報をWebサーバのKML(XML)ファイルへ保存。
3.同じ日に取得した位置情報リストを1つのKMLファイルへ保存。
4.KMLファイルをGoogle Mapsへ取り込む。
1. 位置情報取得
Webサーバへ携帯サイトを作成。以下のコードを記述。
「lcs」がドコモ用のGPS位置情報取得属性で、他のキャリアもそれぞれ別の位置情報取得方法が用意されている。
2. 3. KMLファイル自動生成
PHPで以下のコードを作成してみた。
<? php
$lat = $_GET['lat']; // 緯度取得
$lon = $_GET['lon']; //経度取得
$acr = $_GET['x-acc']; //精度(測位レベル)取得
$geo = $_GET['geo']; //測地系取得
$lat_array = preg_split("/[.]/", $lat, 3);
$lon_array = preg_split("/[.]/", $lon, 3);
//緯度経度を60進法から10進法へ変換
if(count($lat_array)==3 && count($lon_array)==3){
if( $lat_array[0] >= 0){
$lat = ( ( $lat_array[2] / 60 ) + $lat_array[1] ) /60 + $lat_array[0];
}else{
$lat = ( ( $lat_array[2] / 60 ) + $lat_array[1] ) /60 + $lat_array[0] * -1;
$lat = -1 * $lat;
}
if($lon_array[0] >= 0){
$lon = ( ( $lon_array[2] / 60 ) + $lon_array[1] ) /60 + $lon_array[0];
}else{
$lon = ( ( $lon_array[2] / 60 ) + $lon_array[1] ) /60 + $lon_array[0] * -1;
$lon = -1 * $lon;
}
}
$longitude = str_replace('+', '', $lon);
$latitude = str_replace('+', '', $lat);
//日時をKMLファイル名にする
$kml_file = date('Ymd').'.kml';
//KMLファイル作成
$coorStr = $longitude . ',' . $latitude;
if(!file_exists($kml_file)){ //新規ファイル作成の場合
// DomDocumentを呼び出す
$dom = new DOMDocument('1.0', 'UTF-8');
// ルート要素作成
$node = $dom->createElementNS('http://earth.google.com/kml/2.2', 'kml');
$parNode = $dom->appendChild($node);
//新しい要素をルート要素の子要素のとして新規挿入する
$document = $parNode->appendChild(new domElement("Document"));
make_nod();
}else{ //追記の場合
//DomDocumentを呼び出す
$dom = new DomDocument('1.0','UTF-8');
$dom->preserveWhiteSpace = false;
//保存済みのxmlを読み込む
$dom->load($kml_file);
//読み込んだxmlのdocument要素をルートの子要素とする
$root = $dom->documentElement;
$document = $root->childNodes->item(0);
make_nod();
}
//共通部分
function make_nod(){
global $parNode,$document,$placemark,$dom,$kml_file,$coorStr,$node;//不要なものもあるかも
//placemark要素を新規作成
$placemark = $document->appendChild(new domElement("Placemark"));
//新しい要素をplacemark要素の子要素として新規挿入する
$placemark->appendChild(new domElement("name", date('H:i:s')));
$placemark->appendChild(new domElement("description", date('Y/m/d H:i:s')));
$placemark->appendChild(new domElement("styleUrl", "#"));
//GoogleMapsへ表示するアイコン設定
$Style=$placemark->appendChild(new domElement("Style"));
$IconStyle=$Style->appendChild(new domElement("IconStyle"));
$Icon=$IconStyle->appendChild(new domElement("Icon"));
$Icon->appendChild(new domElement("href","http://maps.google.com/mapfiles/kml/pal4/icon50.png"));
//新しい要素をPoint要素の子要素として新規挿入する
$Point=$placemark->appendChild(new domElement("Point"));
$Point->appendChild(new domElement("coordinates", $coorStr));
//XMLをきれいに整形
$dom->formatOutput = true;
//XMLを保存する
$dom->save($kml_file);
}
?>
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
<name>18:28:03</name>
<description>2010/08/22 18:28:03</description>
<styleUrl>#</styleUrl>
<Style>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/arrow.png</href>
</Icon>
</IconStyle>
</Style>
<Point>
<coordinates>139.43118083333,35.816845833333</coordinates>
</Point>
</Placemark>
<Placemark>
<name>18:28:19</name>
<description>2010/08/22 18:28:19</description>
<styleUrl>#</styleUrl>
<Style>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/arrow.png</href>
</Icon>
</IconStyle>
</Style>
<Point>
<coordinates>139.4327475,35.816566944444</coordinates>
</Point>
</Placemark>
<Placemark>
<name>18:28:30</name>
<description>2010/08/22 18:28:30</description>
<styleUrl>#</styleUrl>
<Style>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/arrow.png</href>
</Icon>
</IconStyle>
</Style>
<Point>
<coordinates>139.41197638889,35.782910555556</coordinates>
</Point>
</Placemark>
<Placemark>
<name>18:33:22</name>
<description>2010/08/22 18:33:22</description>
<styleUrl>#</styleUrl>
<Style>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pal4/icon50.png</href>
</Icon>
</IconStyle>
</Style>
<Point>
<coordinates>139.41982972222,35.780625277778</coordinates>
</Point>
</Placemark>
</Document>
</kml>
4. Google Mapsへ取り込む
以下のようなURLでとりあえずは表示可能。
http://maps.google.co.jp/maps?q=http://www.hogehoge.com/gpsdata/20100817.kml
http://maps.google.co.jp/maps?q=KMLファイルが保存されているURL
又は
google Mapsの検索欄へKMLファイルが保存されているURLを入力。
マイマップへは「編集」>>「インポート」でURLを入力するか、ローカルコンピュータへ保存したKMLファイルを選択。
こんな感じ ←場所は適当です