艺虎动画 > 使用Google Map API(FLASH)在地图中显示你的位置

使用Google Map API(FLASH)在地图中显示你的位置

翼虎动漫   2010-7-20

 

 

 

流程大概这样
1.先获取访客IP,然后通过IP获取用户地理位置信息..(这一步我直接调用了http://www.webxml.com.cn/提供的接口)
2.调用ClientGeocoder.geocode获取该地理位置于google map上的集合..
3.获取第一个位置..用Map.setCenter定位地图..
4.用Map.addOverlay标注位置..
5.用Map.openInfoWindow弹出Tip提示..

code~~

01.<?xml version="1.0" encoding="utf-8"?>
02.<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontFamily="Verdana" fontSize="12">
03.    <mx:Script>
04.        <![CDATA[
05.            import com.google.maps.InfoWindowOptions;
06.            import com.google.maps.Map;
07.            import com.google.maps.MapType;
08.            import com.google.maps.overlays.Marker;
09.            import com.google.maps.services.ClientGeocoder;
10.            import com.google.maps.services.GeocodingEvent;
11.              
12.            import mx.controls.Alert;
13.            import mx.rpc.events.FaultEvent;
14.            import mx.rpc.events.ResultEvent;
15.              
16.            private var geocoder:ClientGeocoder;
17.            //by l4cd.net 
18.            private function onMapReady(event:Event):void 
19.            {
20.                map.enableContinuousZoom();
21.                map.enableScrollWheelZoom();
22.                geocoder = new ClientGeocoder();
23.                geocoder.addEventListener(GeocodingEvent.GEOCODING_SUCCESS,
24.                    function(event:GeocodingEvent):void {
25.                        var placemarks:Array = event.response.placemarks;
26.                        if (placemarks.length > 0) {
27.                            map.setCenter(placemarks[0].point, 5, MapType.NORMAL_MAP_TYPE);
28.                            var marker:Marker = new Marker(placemarks[0].point);
29.                            map.addOverlay(marker);
30.                            map.openInfoWindow(placemarks[0].point, new InfoWindowOptions({title:"欢迎访问 L4cd.Net 简单工作",content: "来自<"+here+">的访客"}));
31.                        }
32.                    });
33.                geocoder.addEventListener(GeocodingEvent.GEOCODING_FAILURE,
34.                    function(event:GeocodingEvent):void {
35.                        trace("Geocoding failed");
36.                        Alert.show("获取地理位置失败","L4cd.Net 简单工作");
37.                    });
38.                  
39.                  
40.                ip.getGeoIPContext();
41.            }
42.            private var here:String;
43.            protected function ip_resultHandler(event:ResultEvent):void
44.            {
45.                here = event.result[1];
46.                geocoder.geocode(here);
47.            }
48.  
49.            protected function ip_faultHandler(event:FaultEvent):void
50.            {
51.                Alert.show("获取地理位置失败","L4cd.Net 简单工作");
52.            }
53.  
54.        ]]>
55.    </mx:Script>
56.    <mx:WebService result="ip_resultHandler(event)" fault="ip_faultHandler(event)" wsdl="http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl" id="ip">    
57.    </mx:WebService>
58.    <maps:Map xmlns:maps="com.google.maps.*" language="zh-CN" id="map" mapevent_mapready="onMapReady(event)" 
59.              width="100%" height="100%" key="{api_key}"/>
60.</mx:Application>


其中api_key需要你自己到http://code.google.com/intl/zh-CN/apis/maps/signup.html申请一个开发用的key..
只要用google账号登陆..填写你需要使用地图服务的域名即可
当然你可以破解我的flash获取我现在用的key..不过每个key是有域名限制..so..你拿了也没用..

另外此地址为获取访客ip与地理位置的webservice..
http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl

PS~其实把上面的flash稍稍改改..
即可改成显示当前在线用户在地图上的分布..各位兴趣可以尝试一下..