Google Maps API for Flash ジオコーディング
カテゴリー: Google Maps API for Flash
投稿日: 2009/11/13 00:12
Google Maps API for Flashには、ジオコーディングというサービスがある。
ジオコーディングとは、住所 (「1600 Amphitheatre Parkway, Mountain View, CA」など) を地理座標 (緯度 37.423021、経度 -122.083739 など) に変換する処理のこと。
今回は、ユーザが入力した住所をジオコーディングを利用して地理座標に変換しマーカーを配置する。
ジオコーディングとは、住所 (「1600 Amphitheatre Parkway, Mountain View, CA」など) を地理座標 (緯度 37.423021、経度 -122.083739 など) に変換する処理のこと。
今回は、ユーザが入力した住所をジオコーディングを利用して地理座標に変換しマーカーを配置する。
import com.google.maps.LatLng;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapType;
import com.google.maps.controls.ZoomControl;
import com.google.maps.MapMouseEvent;
import com.google.maps.InfoWindowOptions;
import com.google.maps.overlays.*;
import com.google.maps.services.*;
var map:Map = new Map();
map.addControl(new ZoomControl());
map.key = "YOUR Google Maps API Key";
map.setSize(new Point(stage.stageWidth, stage.stageHeight-50));
map.y=50;
map.addEventListener(MapEvent.MAP_READY, onMapReady);
this.addChild(map);
var geocoder:ClientGeocoder; // ジオコーディング
geoSearchButton.addEventListener(MouseEvent.CLICK,geoSearch);
function geoSearch(e:MouseEvent):void{
var geoSearch:String = geoSearchText.text;
if(geoSearch){
geocoder.geocode(geoSearch); // ジオコードする住所
}
}
// ジオコーディングの正常終了イベント
function handleGeocodingSuccess(e:GeocodingEvent):void{
var placemarks:Array = e.response.placemarks; // ジオコーディング処理の結果
if(placemarks.length > 0){
map.setCenter(placemarks[0].point); // 住所のLatLngを設定
trace(placemarks[0].address); // 詳細な住所
trace(placemarks[0].addressDetails); // 住所詳細オブジェクト
var marker:Marker = new Marker(placemarks[0].point);
marker.addEventListener(MapMouseEvent.CLICK, function(event:Event):void {
marker.openInfoWindow(new InfoWindowOptions(
{title:placemarks[0].address,content:placemarks[0].point.toString()}));
});
map.addOverlay(marker); // マーカーを追加
}
}
// ジオコーディングのエラーイベント
function handleGeocodingFailure(e:GeocodingEvent):void{
}
function onMapReady(event:Event):void {
map.setCenter(new LatLng(39.701683, 141.136369), 16, MapType.NORMAL_MAP_TYPE);
geocoder = new ClientGeocoder();
// ジオコーディングの正常終了イベントの登録
geocoder.addEventListener(GeocodingEvent.GEOCODING_SUCCESS, handleGeocodingSuccess);
// ジオコーディングのエラーイベントの登録
geocoder.addEventListener(GeocodingEvent.GEOCODING_FAILURE, handleGeocodingFailure);
}
Adobe Flash Playerが必要です
Google Maps API for Flash