1205/0910
Google Maps API for Flash Demo
Google Maps API的ActionScript3版本出来已经有一段时间了,今天在官方文档(里边有很详细的介绍)那看了一下,做了2个Demo,关于中文版的地图…恩,,是没出?还是SDK里可以设置语言?知道的告诉我一下,我是农民,不知道怎么设置。
先是一般的地图,带标记,拖动,和位置查询。
来来,代码:
import com.google.maps.LatLng;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapMouseEvent;
import com.google.maps.MapType;
import com.google.maps.InfoWindowOptions;
import com.google.maps.controls.ZoomControl;
import com.google.maps.controls.PositionControl;
import com.google.maps.overlays.Marker;
import com.google.maps.overlays.MarkerOptions;
import com.google.maps.services.ClientGeocoder;
import com.google.maps.services.GeocodingEvent;
var map:Map = new Map();
map.key = "ABQIAAAAo5RLwGEOxmVA88OUDH7fDRQCv6C133r1XzmkPp8P2SPfybuhnxRC1L3ALirem35g_tUs6uhkINg6xg";//这里输入你的googlemap api key
map.setSize(new Point(stage.stageWidth, stage.stageHeight));//设置map的宽度和高度
map.addEventListener(MapEvent.MAP_READY, onMapReady);
this.addChild(map);
var mc_search=new Search_mc();
mc_search.x=280;
mc_search.y=10;
this.addChild(mc_search);
function onMapReady(_evt:Event):void {
mc_search.btn_search.addEventListener(MouseEvent.CLICK,doGeocode);
map.setCenter(new LatLng(30, 115), 5, MapType.NORMAL_MAP_TYPE);
//map.enableScrollWheelZoom();
map.addControl(new ZoomControl());
map.addControl(new PositionControl());
//map.addControl(new MapTypeControl());
map.addEventListener(MapMouseEvent.CLICK, onMapClick);
var marker:Marker = new Marker(map.getCenter(), new MarkerOptions({draggable: true}));
marker.addEventListener(MapMouseEvent.DRAG_START,dragMarker);
marker.addEventListener(MapMouseEvent.DRAG_END,dropMarker);
map.addOverlay(marker);
}
function onMapClick(_evt:MapMouseEvent){
//map.openInfoWindow(_evt.latLng,new InfoWindowOptions({title: "Click Event", content: "You clicked the map!"}));
}
function dragMarker(_evt:MapMouseEvent):void {
map.closeInfoWindow();
};
function dropMarker(_evt:MapMouseEvent):void {
map.openInfoWindow(_evt.latLng,new InfoWindowOptions({content: "Marker was droped here!"}));
};
function doGeocode(event:Event):void {
var geocoder:ClientGeocoder = new ClientGeocoder();
geocoder.addEventListener(GeocodingEvent.GEOCODING_SUCCESS,geoSuccess);
geocoder.addEventListener(GeocodingEvent.GEOCODING_FAILURE,geoFailed);
geocoder.geocode(mc_search.address.text);
}
function geoSuccess(_evt:GeocodingEvent){
var placemarks:Array = _evt.response.placemarks;
if (placemarks.length > 0) {
map.setCenter(placemarks[0].point);
var marker:Marker = new Marker(placemarks[0].point);
marker.addEventListener(MapMouseEvent.CLICK, function (event:MapMouseEvent):void {
marker.openInfoWindow(new InfoWindowOptions({content: placemarks[0].address}));
});
map.addOverlay(marker);
}
}
function geoFailed(_evt:GeocodingEvent){
trace("Geocoding failed");
}
接着是地图导航的部分:
来来,又是代码部分:
import com.google.maps.*;
import com.google.maps.overlays.*;
import com.google.maps.services.*;
var dir:Directions;
var turnCounter:uint = 0;
var map:Map = new Map();
map.key = "ABQIAAAAo5RLwGEOxmVA88OUDH7fDRQCv6C133r1XzmkPp8P2SPfybuhnxRC1L3ALirem35g_tUs6uhkINg6xg";//这里输入你的googlemap api key
map.setSize(new Point(stage.stageWidth, stage.stageHeight));//设置map的宽度和高度
map.addEventListener(MapEvent.MAP_READY, onMapReady);
this.addChild(map);
var mc_input=new MC_input();
this.addChild(mc_input);
mc_input.getTurnByTurnDirections.enabled = false;
function onMapReady(_evt:MapEvent):void {
map.setCenter(new LatLng(30,115), 5, MapType.NORMAL_MAP_TYPE);
dir = new Directions();
dir.addEventListener(DirectionsEvent.DIRECTIONS_SUCCESS, onDirLoad);
dir.addEventListener(DirectionsEvent.DIRECTIONS_FAILURE, onDirFail);
mc_input.getDirections.addEventListener(MouseEvent.CLICK, processDirections);
mc_input.getTurnByTurnDirections.addEventListener(MouseEvent.CLICK, processTurnByTurn);
}
function processDirections(_evt:Event):void {
dir.load("from: " + mc_input.from.text + " to: " + mc_input.to.text);
mc_input.getTurnByTurnDirections.enabled = true;
// Reset turnCounter to zero for new directions
turnCounter = 0;
mc_input.step.htmlText = "Start at " + mc_input.from.text;
}
function onDirFail(_evt:DirectionsEvent):void {
mc_input.step.htmlText = "onDirFail";
}
function onDirLoad(_evt:DirectionsEvent):void {
var dir:Directions = _evt.directions;
var startMarker:Marker;
var endMarker:Marker;
map.clearOverlays();
map.addOverlay(dir.createPolyline());
map.setZoom(map.getBoundsZoomLevel(dir.bounds));
map.setCenter(dir.bounds.getCenter());
startMarker = new Marker(dir.getRoute(0).startGeocode.point, new MarkerOptions({fillStyle: {color:Color.GREEN}}));
endMarker = new Marker(dir.getRoute(0).endGeocode.point, new MarkerOptions({fillStyle: {color:Color.BLUE}}));
map.addOverlay(startMarker);
map.addOverlay(endMarker);
}
function processTurnByTurn(_evt:Event):void {
var stepText:String;
var stepMarker:Marker;
turnCounter++;
if (turnCounter <= dir.getRoute(0).numSteps) {
stepText = dir.getRoute(0).getStep(turnCounter-1).descriptionHtml;
stepMarker = new Marker(dir.getRoute(0).getStep(turnCounter-1).latLng, new MarkerOptions({label: turnCounter.toString()}));
map.addOverlay(stepMarker);
mc_input.step.htmlText = "Step " + turnCounter + ": " + stepText;
} else {
mc_input.getTurnByTurnDirections.enabled = false;
mc_input.step.htmlText = "Arrive at " + mc_input.to.text + " : " + dir.getRoute(0).summaryHtml;
}
}
用Google Maps API可以做的东西应该还是挺多的,比如用户的地理定位,导航,地图游戏等。。。等有需要再看吧,哈。。
May 13th, 2009 - 15:10
哈哈。这个东西确实很强大,顶一个先。!~
[Reply]
DFdou Reply:
2009-5-13 at 3:42 pm
@ibio, google挺多的应用都挺好的
[Reply]
May 15th, 2009 - 00:02
Hi, Super post, Need to mark it on Digg
Thank you
[Reply]
DFdou Reply:
2009-5-15 at 9:36 am
@AnnaHopn, go ahead as you like,
[Reply]
January 13th, 2010 - 09:33
不错,学习了,请问DFdou ,如果在鼠标移动到地图上某一位置,出来相应的风景图片或相应的风景信息,这个该怎么做呢,要用到什么类,谢谢了
[Reply]
DFdou Reply:
2010-1-13 at 11:58 am
@ashes_li, Google Map官方类里边没有这个东西吧,那个应该可以自己做hover事件,貌似有看到类似的应用哈。
[Reply]
January 13th, 2010 - 16:23
我看了官网InfoWindowOptions感觉这里可以加点类似html内容,可是例子里是contentHTML: null,我试了,等标签好像显示不了内容,另外比如我当前点击的是杭州,怎么获取这个标签内容啊,谢谢了
[Reply]
DFdou Reply:
2010-1-13 at 4:57 pm
@ashes_li, 这个。。。我还真没去注意过。。。你可以参考下getDefaultOptions()获得默认的看看
[Reply]
February 21st, 2010 - 10:35
可以相应鼠标事件吗?类似
http://www.fjlj.gov.cn/flash/indexmap.swf这种效果可以吗?
[Reply]
DFdou Reply:
2010-2-21 at 10:28 pm
@step, 可以的,点击事件之类的都有~
[Reply]