とあるセクシーなデータ分析官

を目指す渋谷で働くソーシャルゲーム分析者の卵

Google Static Maps APIを使って、携帯から使えそうな簡単な地図サイト作ってみた

Google Stati Maps APIで地図を画像として取得できますが、
最近そのAPIの機能が拡張されて、フラッシュで生成されるGoogle Maps API
同じようなオプションが使えるようです!


下記のソースで作ったものはこちら
http://keywood.ryuquo.com/gmap/gmap.php

$mapcenter = array(35.658517, 139.745493);
$mapzoom = 4;
$lines = array(34.567891, 134.567891, 35.678911, 136.789111)
$lines = array(35.567891, 138.567891, 38.678911, 140.789111)

$imgpath = getGMap($mapcenter, $mapzoom, $lines);


function getGMap($mapcenter, $mapzoom, $lines)
{
	$host =   "http://maps.google.com/staticmap";
	$center = "?center={$mapcenter[0]},{$mapcenter[1]}";
	$format = "&format=png";
	$size =   "&size=240x240";
	$zoom =   "&zoom={$mapzoom}";
	$maptype= "&maptype=mobile";

	$path = "";
	foreach ($lines as $line) {
		$path .= "&path=rgba:0x0000ffff,weight:2|{$line[0]},{$line[1]}|{$line[2]},{$line[3]}";
	}
	$key =    "googleのID";

	$imgpath = $host.$center.$format.$size.$zoom.$maptype.$path.$key;
	return $imgpath;
}


とりわけ判りにくい部分はpathで複数の線を引く場合。
参考にしたサイトの通りに行うとパスをパイプで複数引くと全部くっついてしまいます


くっついたパスが引きたくなかったのでどうにかできないものかとpathのパラメータを複数別に投げてみたら
くっついてない線を複数引くことに成功しました。


これで、簡単に携帯サイト&GPSに対応した地図を作れます。
イージー!


参考
http://wordpressgogo.com/customize/static_maps_api2.html