例-51:Google Maps API for Flash でルート検索

Google Maps API for Flash SDK 1.18から、日本国内でもルート検索が行なえるようになりました。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="absolute" xmlns:local="local.*" xmlns:maps="com.google.maps.*" viewSourceURL="srcview/index.html">
	<maps:Map width="100%" height="100%" id="map_canvas"
		 key="[MAPS_API_KEY]" sensor="false" language="ja" countryCode="JP"
		 mapevent_mapready="{onMapReady();}" />
	
	<mx:Script>
		<![CDATA[
			import com.google.maps.InfoWindowOptions;
			import com.google.maps.overlays.Marker;
			import com.google.maps.interfaces.IMarker;
			import com.google.maps.services.Route;
			import com.google.maps.LatLngBounds;
			import com.google.maps.overlays.Polyline;
			import com.google.maps.services.DirectionsEvent;
			import com.google.maps.services.Directions;
			import com.google.maps.LatLng;
			import com.google.maps.Map;
			import com.google.maps.MapEvent;
			import com.google.maps.MapMouseEvent;
			import mx.controls.Alert;
			
			private function onMapReady() : void {
				//初期化
				var initPos : LatLng = new LatLng(35.684, 139.750);
				var initZoom : Number = 7;
				map_canvas.setCenter(initPos, initZoom);
				
				//ディレクションの作成
				var directions : Directions = new Directions();
				directions.addEventListener(DirectionsEvent.DIRECTIONS_SUCCESS, directionSuccess);
				directions.addEventListener(DirectionsEvent.DIRECTIONS_FAILURE, directionFail);
				directions.load("from: 東京駅 to: 大阪駅");
			}
			private function directionFail(event : DirectionsEvent) : void {
				//ディレクションの検索結果が失敗した
				Alert.show(event.directions.status.toString());
			}
			private function directionSuccess(event : DirectionsEvent) : void {
				//ディレクションの検索結果が成功した
				var directions : Directions = event.directions;
				this.map_canvas.addOverlay(directions.createPolyline());
				
				//ズームレベルと中心位置を設定
				var bounds : LatLngBounds = directions.bounds;
				var zoom : Number = map_canvas.getBoundsZoomLevel(bounds);
				map_canvas.setCenter(bounds.getCenter(), zoom);
				
				//開始地点にマーカー
				var route : Route = directions.getRoute(0);
				var sPos : LatLng = route.getStep(0).latLng;
				var sMakrer : IMarker = createMarker(sPos, "from : 東京駅");
				map_canvas.addOverlay(sMakrer);
				
				//終了地点にマーカー
				var ePos : LatLng = route.endLatLng;
				var eMarker : IMarker = createMarker(ePos, "to : 大阪駅");
				map_canvas.addOverlay(eMarker); 
			}
			private function createMarker(latlng : LatLng, txt : String) : IMarker {
				var marker : Marker = new Marker(latlng);
				var infoOpts : InfoWindowOptions = new InfoWindowOptions();
				infoOpts.contentHTML = txt;
				marker.addEventListener(MapMouseEvent.CLICK, function(e : MapMouseEvent) : void {
					marker.openInfoWindow(infoOpts);
				});
				return marker;
			}
		]]>
	</mx:Script>
</mx:Application>

ソースコードのダウンロード map_example_51.zip



サイト内検索
Google Maps APIプログラミングガイド

Google Maps API Expert 4人が共著で執筆。中級者向けに実用に役立つサンプルを中心に紹介! スマートフォン時代に合わせたGoogle Maps APIの使い方も掲載。
詳しくはこちら
Google Maps APIプログラミング入門
Google Maps APIプログラミング入門
全480ページ。Google Maps API ver.2, ver.3, for Flash, Static Maps API v2, Google Maps API Primery, ライセンス…など、Google Maps APIに関連する幅広い内容の本を書きました。
このサイトで公開しているサンプルはもちろん、本のために作ったサンプルも満載です。
詳しくはこちら