今天網(wǎng)上亂逛的時(shí)候..
在一個(gè)測(cè)試站點(diǎn)速度(http://www.speedtest.cn/)的網(wǎng)站下面~
用Google地圖顯示了訪問(wèn)者所在的位置...
覺(jué)得挺有意思的..
看了一下google map的flash api..
實(shí)現(xiàn)起來(lái)非常方便..感嘆一下google map的強(qiáng)大..
swf效果地址:http://blog.l4cd.net/google/googlemap.swf
流程大概這樣
1.先獲取訪客IP,然后通過(guò)IP獲取用戶地理位置信息..(這一步我直接調(diào)用了http://www.webxml.com.cn/提供的接口)
2.調(diào)用ClientGeocoder.geocode獲取該地理位置于google map上的集合..
3.獲取第一個(gè)位置..用Map.setCenter定位地圖..
4.用Map.addOverlay標(biāo)注位置..
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:"歡迎訪問(wèn) L4cd.Net 簡(jiǎn)單工作",content: "來(lái)自<"+here+">的訪客"}));
31. }
32. });
33. geocoder.addEventListener(GeocodingEvent.GEOCODING_FAILURE,
34. function(event:GeocodingEvent):void {
35. trace("Geocoding failed");
36. Alert.show("獲取地理位置失敗","L4cd.Net 簡(jiǎn)單工作");
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 簡(jiǎn)單工作");
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申請(qǐng)一個(gè)開(kāi)發(fā)用的key..
只要用google賬號(hào)登陸..填寫(xiě)你需要使用地圖服務(wù)的域名即可
當(dāng)然你可以破解我的flash獲取我現(xiàn)在用的key..不過(guò)每個(gè)key是有域名限制..so..你拿了也沒(méi)用..
另外此地址為獲取訪客ip與地理位置的webservice..
http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl
PS~其實(shí)把上面的flash稍稍改改..
即可改成顯示當(dāng)前在線用戶在地圖上的分布..各位興趣可以嘗試一下..