1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
| public class MainComplet extends Sprite {
// -----------------------------------------------------------------------------------
// DECLARATIONS
// -----------------------------------------------------------------------------------
private var _map:Map = null;
private var _positionRoissy:LatLng = new LatLng(48.790367,2.656885);
private var _positionGarePontault:LatLng = new LatLng(48.805843, 2.617828);
private var _positionCarrefourPontault:LatLng = new LatLng(48.77617, 2.608611);
private var _positionCentre:LatLng = new LatLng(48.793764,2.632756);
private var _mapZoomPointDebut:Point = null;
private var _mapZoomLatDebut:LatLng = null;
private var _mapZoomLatFin:LatLng = null;
private var _spZone:Sprite = null;
// -----------------------------------------------------------------------------------
// CONSTRUCTEUR
// -----------------------------------------------------------------------------------
public function MainComplet():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// ---------
stage.scaleMode = "noScale";
stage.align = "LT";
stage.quality = "medium";
// définition de la carte, de ces options et évenements
_map = new Map();
_map.language = "fr_FR";
_map.countryCode = "FR";
_map.key = "ABQIAAAAvwdr5PrLsm228aByyJiTFRQ5NISJ_vEU2QZeuqsHywIYKJUzMhS0KqAO5rT_XJ07lAzKRjs3aZG6eQ";
_map.setSize(new Point(stage.stageWidth, stage.stageHeight));
_map.addEventListener(MapEvent.MAP_READY, evtMapReady);
_map.addEventListener(MapEvent.MAP_PREINITIALIZE, evtMapPreinitialize);
_map.addEventListener(MouseEvent.MOUSE_WHEEL, evtMapMouseWheel);
_map.addEventListener(MapMouseEvent.MOUSE_DOWN, evtMapMouseDown);
_map.addEventListener(MapMouseEvent.MOUSE_UP, evtMapMouseUp);
_map.addEventListener(MapMouseEvent.MOUSE_MOVE, evtMapMouseMove);
_map.x = 0;
_map.y = 0;
this.addChild(_map);
// ---------
_spZone = new Sprite();
_spZone.x = 0;
_spZone.y = 0;
this.addChild(_spZone);
// ---------
stage.addEventListener(Event.RESIZE, evtStageResize);
}
// -----------------------------------------------------------------------------------
// EVENEMENTS -> DEFINITION ZONE ET ZOOM SUR ZONE
// -----------------------------------------------------------------------------------
private function evtMapMouseDown(ev:MapMouseEvent):void {
// si la touche controle est enfoncé au moment du clique : enregistrer le point de départ et bloquer le déplacement de la carte
if ( ev.ctrlKey ) {
_map.disableDragging();
_mapZoomLatDebut = ev.latLng;
_mapZoomPointDebut = new Point(stage.mouseX, stage.mouseY);
}
}
private function evtMapMouseUp(ev:MapMouseEvent):void {
// Zoomer sur la zone sélectionné
if ( ev.ctrlKey && _map.draggingEnabled() == false ) {
_spZone.graphics.clear();
_mapZoomLatFin = ev.latLng;
var llBound:LatLngBounds = new LatLngBounds(_mapZoomLatDebut, _mapZoomLatFin);
var c:LatLng = llBound.getCenter();
var z:Number = _map.getBoundsZoomLevel(llBound);
_map.setCenter(c, z);
}
_map.enableDragging();
}
private function evtMapMouseMove(ev:MapMouseEvent):void {
// si la touche controle est gardé enfoncé pendant le déplacement : dessiner la zone de zoom
if ( ev.ctrlKey && _map.draggingEnabled() == false ) {
with (_spZone.graphics) {
clear();
lineStyle(2, 0xFF0000, 1);
beginFill(0x006699, 0.2);
drawRect(_mapZoomPointDebut.x, _mapZoomPointDebut.y, stage.mouseX - _mapZoomPointDebut.x, stage.mouseY - _mapZoomPointDebut.y);
endFill();
}
}
}
// -----------------------------------------------------------------------------------
// EVENEMENT -> REDIMENSIONNEMENT STAGE (redéfinir la taille de la map)
// -----------------------------------------------------------------------------------
private function evtStageResize(ev:Event):void {
_map.setSize(new Point(stage.stageWidth, stage.stageHeight));
}
// -----------------------------------------------------------------------------------
// EVENEMENT -> INITIALISATION MAP (définition des options)
// -----------------------------------------------------------------------------------
private function evtMapPreinitialize(ev:MapEvent):void {
var mapO:MapOptions = new MapOptions();
mapO.zoom = 13;
mapO.center = _positionCentre;
mapO.mapType = MapType.HYBRID_MAP_TYPE;
_map.setInitOptions(mapO);
}
// -----------------------------------------------------------------------------------
// EVENEMENT -> CHARGEMENT MAP (ajout des marqueurs, des lignes et des controles)
// -----------------------------------------------------------------------------------
private function evtMapReady(ev:MapEvent):void {
// ajout de 3 marqueurs a 3 positions différentes
var m:MarkerOptions = new MarkerOptions(
{
fillStyle: { color:0xFF0000 },
strokeStyle: { color:0, thickness:1 },
tooltip:"Roissy en brie",
radius:20,
hasShadow:true,
draggable:false
}
);
_map.addOverlay(new Marker(_positionRoissy, m));
m = new MarkerOptions(
{
fillStyle: { color:0x00FF00 },
strokeStyle: { color:0, thickness:1 },
tooltip:"la gare de pontault",
radius:20,
hasShadow:true,
draggable:false
}
);
_map.addOverlay(new Marker(_positionGarePontault, m));
m = new MarkerOptions(
{
fillStyle: { color:0x0000FF },
strokeStyle: { color:0, thickness:1 },
tooltip:"carrefour de pontault",
radius:20,
hasShadow:true,
draggable:false
}
);
_map.addOverlay(new Marker(_positionCarrefourPontault, m));
// dessiner des lignes entre les 3 positions
var po:PolylineOptions = new PolylineOptions({ strokeStyle: new StrokeStyle({
color: 0xFF0000,
thickness: 4,
alpha: 0.7})
});
var pl:Polyline = new Polyline([_positionRoissy, _positionGarePontault, _positionCarrefourPontault, _positionRoissy], po);
_map.addOverlay(pl);
// ajout des controles
_map.addControl(new MapTypeControl());
_map.addControl(new OverviewMapControl());
_map.addControl(new NavigationControl());
var sco:ScaleControlOptions = new ScaleControlOptions();
sco.labelFormat = new TextFormat('Arial', 12, 0xFFFFFF, true);
sco.units = ScaleControlOptions.UNITS_METRIC_ONLY;
_map.addControl(new ScaleControl(sco));
}
// -----------------------------------------------------------------------------------
// EVENEMENT -> ZOOM SUR LA CARTE AVEC LA ROULETTE DE LA SOURIS
// -----------------------------------------------------------------------------------
private function evtMapMouseWheel(ev:MouseEvent):void {
// recuperer le zoom actuel et l'incrémenter ou le décrementer suivant la direction de la roulette de la souris
var z:Number = _map.getZoom() + (ev.delta > 1 ? 1 : -1);
z = (z > 20 ? 20 : z);
// Zoomer sur la carte en utilisant son centre
//_map.setCenter(_map.getCenter(), z);
// Zoomer sur la carte en la centrant sur la position de la souris
var pos:LatLng = _map.fromViewportToLatLng(new Point(_map.mouseX, _map.mouseY));
_map.setCenter(pos, z);
}
}
} |
Partager