Flex中如何使用Yahoo! Maps的例子
By Minidxer | August 5, 2008
接下来的例子演示了Flex中如何使用Yahoo! Maps创建自己的地图应用。
首先你需要做的是从这里获取Yahoo! Maps API key
然后从这里下载Yahoo! Maps ActionScript 3.0 component
接下去将zip压缩包内的东西复制到工程的/libs/中就可以了。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white"
- creationComplete="init();">
- <mx:Script>
- <![CDATA[
- import mx.events.ResizeEvent;
- import mx.controls.Alert;
- import com.yahoo.maps.api.YahooMap;
- import com.yahoo.maps.api.YahooMapEvent;
- import com.yahoo.maps.api.core.location.Address;
- import com.yahoo.maps.webservices.geocoder.GeocoderResult;
- import com.yahoo.maps.webservices.geocoder.events.GeocoderEvent;
- private var yahooMap:YahooMap;
- private var address:Address;
- private function init():void {
- yahooMap = new YahooMap();
- yahooMap.init(APP_ID, mapContainer.width, mapContainer.height);
- yahooMap.addPanControl();
- yahooMap.addZoomWidget();
- yahooMap.addTypeWidget();
- mapContainer.addChild(yahooMap);
- geocodeAddress(textInput.text);
- }
- private function geocodeAddress(value:String):void {
- address = new Address(value);
- address.addEventListener(GeocoderEvent.GEOCODER_SUCCESS, address_geocoderSuccess);
- address.addEventListener(GeocoderEvent.GEOCODER_FAILURE, address_geocoderFailure);
- address.geocode();
- }
- private function address_geocoderSuccess(evt:GeocoderEvent):void {
- var result:GeocoderResult = Address(evt.target).geocoderResultSet.firstResult;
- yahooMap.centerLatLon = result.latlon;
- yahooMap.zoomLevel = result.zoomLevel;
- }
- private function address_geocoderFailure(evt:GeocoderEvent):void {
- Alert.show("Unable to geocode address: " + address.address);
- }
- private function mapContainer_resize(evt:ResizeEvent):void {
- if (yahooMap) {
- yahooMap.setSize(mapContainer.width, mapContainer.height);
- }
- }
- private function button_click(evt:MouseEvent):void {
- geocodeAddress(textInput.text);
- }
- ]]>
- </mx:Script>
- <mx:String id="APP_ID" source="appid.txt" />
- <mx:ApplicationControlBar dock="true">
- <mx:Form styleName="plain">
- <mx:FormItem label="Address:"
- direction="horizontal">
- <mx:TextInput id="textInput"
- text="601 Townsend St, San Francisco, CA 94103" />
- <mx:Button id="button"
- label="Submit"
- click="button_click(event);" />
- </mx:FormItem>
- </mx:Form>
- </mx:ApplicationControlBar>
- <mx:UIComponent id="mapContainer"
- width="100%"
- height="100%"
- resize="mapContainer_resize(event);"/>
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Flex |
Tags: ActionScript, API, Yahoo Maps, 地图应用
Trackbacks