+ // ------------------------------------------------------------------
+ // Attribution/terms management
+ // (moved from Imagery.as)
+
+ private var _selected:Object={};
+ public function get selected():Object { return _selected; }
+
+ public function setAttribution():void {
+ var tf:TextField=TextField(_overlay.getChildAt(0));
+ tf.text='';
+ if (!_selected.attribution) return;
+ var attr:Array=[];
+ if (_selected.attribution.providers) {
+ // Bing attribution scheme
+ for (var provider:String in _selected.attribution.providers) {
+ for each (var bounds:Array in _selected.attribution.providers[provider]) {
+ if (_map.scale>=bounds[0] && _map.scale<=bounds[1] &&
+ ((_map.edge_l>bounds[3] && _map.edge_l<bounds[5]) ||
+ (_map.edge_r>bounds[3] && _map.edge_r<bounds[5]) ||
+ (_map.edge_l<bounds[3] && _map.edge_r>bounds[5])) &&
+ ((_map.edge_b>bounds[2] && _map.edge_b<bounds[4]) ||
+ (_map.edge_t>bounds[2] && _map.edge_t<bounds[4]) ||
+ (_map.edge_b<bounds[2] && _map.edge_t>bounds[4]))) {
+ attr.push(provider);
+ }
+ }
+ }
+ }
+ if (attr.length==0) return;
+ tf.text="Background "+attr.join(", ");
+ positionAttribution();
+ dispatchEvent(new MapEvent(MapEvent.BUMP, { y: tf.textHeight })); // don't let the toolbox obscure it
+ }
+ public function positionAttribution():void {
+ if (!_selected.attribution) return;
+ var tf:TextField=TextField(_overlay.getChildAt(0));
+ tf.x=_map.mapwidth - 5 - tf.textWidth;
+ tf.y=_map.mapheight - 5 - tf.textHeight;
+ }
+
+ public function setLogo():void {
+ while (_overlay.numChildren>2) { _overlay.removeChildAt(2); }
+ if (!_selected.logoData) return;
+ var logo:Sprite=new Sprite();
+ logo.addChild(new Bitmap(_selected.logoData));
+ if (_selected.attribution.url) { logo.buttonMode=true; logo.addEventListener(MouseEvent.CLICK, launchLogoLink, false, 0, true); }
+ _overlay.addChild(logo);
+ positionLogo();
+ }
+ public function positionLogo():void {
+ if (_overlay.numChildren<3) return;
+ _overlay.getChildAt(2).x=5;
+ _overlay.getChildAt(2).y=_map.mapheight - 5 - _selected.logoHeight - (_selected.terms_url ? 10 : 0);
+ }
+ private function launchLogoLink(e:Event):void {
+ if (!_selected.attribution.url) return;
+ navigateToURL(new URLRequest(_selected.attribution.url), '_blank');
+ }
+ public function setTerms():void {
+ var terms:TextField=TextField(_overlay.getChildAt(1));
+ if (!_selected.attribution) { terms.text=''; return; }
+ if (_selected.attribution && _selected.attribution.text) { terms.text=_selected.attribution.text; }
+ else { terms.text="Background terms of use"; }
+ positionTerms();
+ terms.addEventListener(MouseEvent.CLICK, launchTermsLink, false, 0, true);
+ }
+ private function positionTerms():void {
+ _overlay.getChildAt(1).x=5;
+ _overlay.getChildAt(1).y=_map.mapheight - 15;
+ }
+ private function launchTermsLink(e:Event):void {
+ if (!_selected.attribution.url) return;
+ navigateToURL(new URLRequest(_selected.attribution.url), '_blank');
+ }
+
+ public function resizeHandler(event:MapEvent):void {
+ positionLogo();
+ positionTerms();
+ positionAttribution();
+ }
+ private function moveHandler(event:MapEvent):void {
+ setAttribution();
+ // strictly speaking we should review the collection on every move, but slow
+ // dispatchEvent(new Event("collection_changed"));
+ }
+
+ // Create overlay sprite
+ public static function overlaySprite():Sprite {
+ var overlay:Sprite=new Sprite();
+ var attribution:TextField=new TextField();
+ attribution.width=220; attribution.height=300;
+ attribution.multiline=true;
+ attribution.wordWrap=true;
+ attribution.selectable=false;
+ attribution.defaultTextFormat=new TextFormat("_sans", 9, 0, false, false, false);
+ overlay.addChild(attribution);
+ var terms:TextField=new TextField();
+ terms.width=200; terms.height=15;
+ terms.selectable=false;
+ terms.defaultTextFormat=new TextFormat("_sans", 9, 0, false, false, true);
+ overlay.addChild(terms);
+ return overlay;
+ }
+
+ // ------------------------------------------------------------------
+ // Choose a new background
+ // (moved from setBackground in Imagery.as)
+
+ public function setBackgroundFromImagery(bg:Object,remember:Boolean):void {
+ // set background
+ _selected=bg;
+// dispatchEvent(new CollectionEvent(CollectionEvent.SELECT, bg));
+ _map.tileset.init(bg, bg!='');
+ // update attribution and logo
+ _overlay.visible=bg.hasOwnProperty('attribution');
+ setLogo(); setAttribution(); setTerms();
+ // save as SharedObject for next time
+ if (remember) {
+ var obj:SharedObject = SharedObject.getLocal("user_state","/");
+ obj.setProperty('background_url' ,String(bg.url));
+ obj.setProperty('background_name',String(bg.name));
+ try { obj.flush(); } catch (e:Error) {}
+ }
+ }
+