---
title: "Openscales 入门"
author: "wangpj"
date: "2011-05-29T22:39:52Z"
tags: ["Adobe", "CSS", "Flex", "OS", "PHP"]
categories: ["非技术"]
slug: "openscales"
draft: false
lastmod: "2011-05-29T22:43:08Z"
id: 1063993
url: "https://wangpj.iteye.com/blog/1063993"
---

对于开源WebGIS ：我首选GeoServer+PostGIS+Openscales

 

 

下面开始Openscales1.2应用的简单入门

 

FlexApp.mxml

 

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;s:Application xmlns:fx="[http://ns.adobe.com/mxml/2009](http://ns.adobe.com/mxml/2009)"  
      xmlns:s="library://ns.adobe.com/flex/spark"  
      xmlns:mx="library://ns.adobe.com/flex/mx"  
      width="100%" height="100%"  
      xmlns:os="[http://openscales.org](http://openscales.org)"  
      creationComplete="initMap();" resize="resizeMap()"&gt;  
 &lt;fx:Style source="FlexApp.css"/&gt;

 &lt;fx:Declarations&gt;  
 &lt;/fx:Declarations&gt;  
   
 &lt;os:Map id="fxmap" width="100%" height="100%" zoom="5" center="4.833,45.767"&gt;  
  &lt;os:WMSC name="Nasa" url="[http://openscales.org/geoserver/gwc/service/wms](http://openscales.org/geoserver/gwc/service/wms)" layers="bluemarble"  
   format="image/jpeg" maxExtent="-180,-90,180,90"/&gt;  
    
  &lt;os:WheelHandler/&gt;  
  &lt;!--图层管理器 --&gt;  
  &lt;os:ControlPanel title="图层管理器" x="{width - 225}" y="10" width="215"&gt;  
   &lt;os:LayerManager /&gt;  
  &lt;/os:ControlPanel&gt;  
  &lt;!-- 鹰眼--&gt;  
  &lt;s:Panel id="miniMapPanel" title="Overview map" x="{width-miniMapPanel.width-10}" y="{height-miniMapPanel.height-10}"&gt;  
   &lt;s:layout&gt;  
    &lt;s:HorizontalLayout/&gt;  
   &lt;/s:layout&gt;  
   &lt;os:OverviewMap id="miniMap" width="100%" height="100%" extentColor="0xFF3300" newExtentColor="0x00FF33"&gt;  
    &lt;os:CycleMap name="cyclemap" proxy="[http://openscales.org/proxy.php?url="/](http://openscales.org/proxy.php?url=%22%2F)&gt;  
   &lt;/os:OverviewMap&gt;  
  &lt;/s:Panel&gt;  
  &lt;!-- 比例尺 --&gt;  
  &lt;os:ScaleLine x="{10}" y="{height-80}" /&gt;

  &lt;!-- 显示鼠标坐标 --&gt;  
  &lt;os:MousePosition x="{fxmap.width / 2}"  
        y="{fxmap.height-20}"  
        displayProjection="EPSG:4326"/&gt;  
  &lt;!-- 导航控件1 --&gt;  
  &lt;os:PanZoom map="{map}"  
     x="{fxmap.x+10}"  
     y="{fxmap.y+10}" width="112" height="126"/&gt;  
 &lt;/os:Map&gt;  
   
 &lt;!--导航控件2  
 &lt;os:ControlPanel x="10" y="200" width="140" title="Navigation2"&gt;  
  &lt;os:Pan map="{map}"/&gt;  
  &lt;mx:HBox width="100%" paddingLeft="5" paddingRight="5"&gt;  
   &lt;os:Zoom map="{map}"/&gt;  
   &lt;mx:Spacer width="100%" /&gt;  
   &lt;os:ZoomBox map="{map}" width="32" height="32"/&gt;  
  &lt;/mx:HBox&gt;  
 &lt;/os:ControlPanel&gt;  
 --&gt;  
   
 &lt;!-- 工具栏 --&gt;  
 &lt;os:ControlPanel  x="100" height="57" y="10" title="工具栏"&gt;  
  &lt;os:layout&gt;  
   &lt;s:HorizontalLayout/&gt;  
  &lt;/os:layout&gt;  
  &lt;mx:Button id="zoomInBtn" click="doMap('zoomin')" label="放大"/&gt;  
  &lt;mx:Button id="zoomOutBtn" click="doMap('zoomout')" label="缩小"/&gt;  
  &lt;mx:Button id="fullExtentBtn" click="{map.zoomToMaxExtent()}" label="全屏"/&gt;  
  &lt;mx:Button id="panBtn" click="doMap('pan')" label="漫游"/&gt;  
  &lt;mx:Button id="prevExtentBtn" click="{navToolbar.zoomToPrevExtent()}" label="前一视图"/&gt;  
  &lt;mx:Button id="nextExtentBtn" click="{navToolbar.zoomToNextExtent()}" label="后一视图"/&gt;  
 &lt;/os:ControlPanel&gt;

   
 &lt;fx:Script&gt;  
  &lt;![CDATA[  
   import org.openscales.core.Map;  
   import org.openscales.core.Trace;  
   import org.openscales.core.feature.Marker;  
   import org.openscales.core.feature.CustomMarker;  
   import org.openscales.core.feature.PointFeature;  
   import org.openscales.core.layer.FeatureLayer;  
   import org.openscales.core.popup.Anchored;  
   import org.openscales.core.style.Style;  
   import org.openscales.geometry.Point;  
   import org.openscales.geometry.basetypes.Location;  
   import org.openscales.proj4as.ProjProjection;  
   import toolbars.Navigation;  
     
   \[Bindable] private var map:Map = null;  
   public var markLyon:Marker;  
   \[Bindable] public var displayTrace:Boolean=false;  
   \[Bindable] public var displayFirebugTrace:Boolean=false;  
   private var navToolbar:Navigation=new Navigation();  
     
   private function initMap():void {  
    Trace.useFireBugConsole=displayFirebugTrace;  
    map = fxmap.map;  
    this.navToolbar.map=map;  
      
    var markers:FeatureLayer = new FeatureLayer("customMarkerLayer");  
    //添加默认标记  
    markers.projection = new ProjProjection("EPSG:4326");  
    markers.generateResolutions(19);  
    markers.style = Style.getDefaultPointStyle();  
    var marker:PointFeature = PointFeature.createPointFeature(new Location(-94.49783,40.51800));  
    markers.addFeature(marker);  
      
    //markers.addFeature(CustomMarker.createUrlBasedMarker("[http://earth.google.com/intl/en\_uk/outreach/images/add\_placemark.png",new](http://earth.google.com/intl/en_uk/outreach/images/add_placemark.png%22,new) Location(-94.49783,40.51800)));

    //添加标记，绑定事件弹出气泡  
    var lonlat:Location = new Location(-95.49783,42.51800, ProjProjection.getProjProjection("EPSG:4326"));  
    markLyon = new Marker(new org.openscales.geometry.Point(lonlat.lon, lonlat.lat), {popupContentHTML: "This is a popup&lt;br /&gt;&lt;b&gt;Bold text&lt;/b&gt;&lt;br /&gt;&lt;u&gt;Link to &lt;a href=\\"[http://openscales.org\\](http://openscales.org%5C)" target=\\"\_new\\"&gt;OpenScales&lt;/a&gt;&lt;/u&gt;"});  
    markLyon.addEventListener(MouseEvent.CLICK, showPopup);  
    markers.addFeature(markLyon);  
      
    map.addLayer(markers);  
      
    var vect:FeatureLayer = new FeatureLayer("Reprojected vector");  
    vect.projection = ProjProjection.getProjProjection("EPSG:4326");  
    vect.style = Style.getDefaultPointStyle();  
    var p:org.openscales.geometry.Point = new org.openscales.geometry.Point(lonlat.lon, lonlat.lat);  
    vect.generateResolutions(19);  
    vect.addFeature(new PointFeature(p));  
    map.addLayer(vect);  
   }  
     
   private function doMap(tool:String):void{  
    navToolbar.activate(tool);  
   }  
     
   private function resizeMap():void {  
    miniMap.height = 0.2 * this.height;  
    miniMap.width = this.width * miniMap.height / this.height;  
   }  
     
   private function showPopup(e:MouseEvent):void {  
    var popup:Anchored = new Anchored();  
    popup.feature = markLyon;  
    map.addPopup(popup, true);  
   }  
     
  ]]&gt;  
 &lt;/fx:Script&gt;  
&lt;/s:Application&gt;

效果如图：

   
![](http://dl.iteye.com/upload/attachment/489948/9b849715-e65a-3b83-b8d1-79ac652f6712.jpg)  
 

 

本例实现了地图的基本功能包括：导航栏、工具栏、图层管理器、鹰眼、比例尺、鼠标坐标显示、标注气泡显示等。