Search code examples
apache-flexadobearcgisesri

Convert WebMercatorExtent in Extent in Flex


I retrieve dynamically an Array with latidude longitude values that need to be calculated into an Extent so they fit exactly on a map (Adobe Flex). The layers I'm using in the Esri Map component are now:

<esri:ArcGISTiledMapServiceLayer id="arcgisonlineLayer" load="{trace(arcgisonlineLayer.version)}"
                                     url="http://services.arcgisonline.nl/arcgis/rest/services/Basiskaarten/PDOK_BRT/MapServer"/>   

<esri:WMSLayer url="{wmsLayerUrl}">
    <esri:visibleLayers>
        <s:ArrayList>
            <fx:String>0</fx:String><!-- background colors -->
            <fx:String>1</fx:String><!-- signs -->
            <fx:String>2</fx:String><!-- red overview road map can be outcommented-->
                <fx:String>3</fx:String><!-- lines -->
        </s:ArrayList>
    </esri:visibleLayers>
</esri:WMSLayer>

Before I used the standard Esri layers...

<esri:ArcGISTiledMapServiceLayer id="serviceLayer"
        url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" 
        visible="{viewModeButtonBar.selectedIndex == 0}"/>

<esri:ArcGISTiledMapServiceLayer 
        url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"
        visible="{viewModeButtonBar.selectedIndex == 1}"/>  

..and I could use the WebMercatorExtent class to create an extend that would fit but now I need to use these layers and can't use the WebMercatorExtent because the service "http://services.arcgisonline.nl/arcgis/rest/services/Basiskaarten/PDOK_BRT/MapServer" uses

<esri:SpatialReference id="wgs" wkid="28992"/>

Which doesn't go with WebMercatorExtent. Anyone knows how to convert this com.esri.ags.geometry.WebMercatorExtent into an Extent?


Solution

  • Here is how it's done in code. You need to declare as GeometryService that uses some an external webservice. This one is the sampleservice from Esri that might be out of order when you read this answer because there has been an upgrade. Best is to use your own arcgis service, for now I used this one:

    http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer
    

    Put in to Declarations

    <fx:Declarations>
        <esri:GeometryService id="geometryService"
                              concurrency="last"
                              fault="geometryService_faultHandler(event)"
                              projectComplete="projectCompleteHandler(event)"
                              showBusyCursor="true"
                              url="http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer"/>
    </fx:Declarations>
    

    Then create the WebMercatorExtent, create a SpatialReference you want your Geometry converted to and project it. The web service will provide the answer...

                        var wmExtent:WebMercatorExtent = new WebMercatorExtent(
                        _mapItemBounds.getSouthWest().lng(),
                        _mapItemBounds.getSouthWest().lat(),
                        _mapItemBounds.getNorthEast().lng(),
                        _mapItemBounds.getNorthEast().lat());
                    var outSR:SpatialReference = new SpatialReference(28992);
                    const projectParameters:ProjectParameters = new ProjectParameters;
                    projectParameters.geometries = [ wmExtent];
                    projectParameters.outSpatialReference = outSR;
                    geometryService.project(projectParameters);
    

    ...in the function

            protected function projectCompleteHandler(event:GeometryServiceEvent):void
            {
                try
                {
                    // Note: As of version 2.0, GeometryService returns geometries (instead of graphics)
                    var extent:Extent = (event.result as Array)[0] as Extent;
                    map.extent = extent;
                }
                catch (error:Error)
                {
                    Alert.show(error.toString());
                }
            }