Skip To Content

Basic Service Area

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:esri="http://www.esri.com/2008/ags"
               pageTitle="Basic Service Area sample">
    <!--
    Description:
    This sample demonstrates how to use the ServiceAreaTask
    to calculate a 1 minute drive time around a user defined geographic location.
    A network service area is a region that encompasses all accessible streets
    (that is, streets that are within a specified impedance).
    A 1-minute service area for a point on a network includes all the streets
    that can be reached within one minute from that point.

    When user clicks on map, a facility (point marker) is added where user clicked and
    a ServiceArea request is sent to the server with that facility.
    Once the request has been solved (and returned to client), the result is
    added to the resultsGraphicsLayer.

    A second click will clear all graphics and repeat the above.

    Documentation:
    For more information, see the API documentation.
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/FeatureSet.html
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/Graphic.html
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/Map.html
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/events/MapMouseEvent.html
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/events/ServiceAreaEvent.html
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/tasks/ServiceAreaTask.html
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/tasks/ServiceAreaTask.html#solve()
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/tasks/supportClasses/ServiceAreaParameters.html
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/tasks/supportClasses/ServiceAreaSolveResult.html

    ArcGIS REST API documentation:
    http://resources.arcgis.com/en/help/rest/apiref/naserver.html
    http://resources.arcgis.com/en/help/rest/apiref/nalayer.html
    http://resources.arcgis.com/en/help/rest/apiref/saSolve.html

    ArcGIS for Server documentation:
    http://resources.arcgis.com/en/help/main/10.1/index.html#/Network_analysis_services/015400000276000000/

    ArcGIS for Desktop documentation:
    http://resources.arcgis.com/en/help/main/10.1/index.html#/What_is_the_ArcGIS_Network_Analyst_extension/004700000001000000/
    http://resources.arcgis.com/en/help/main/10.1/index.html#/About_the_ArcGIS_Network_Analyst_extension_tutorial/00470000005r000000/
    http://resources.arcgis.com/en/help/main/10.1/index.html#/Service_area_analysis/004700000048000000/

    NOTE: In order to support this workflow (author and publish the services) in your own environment,
    you will need a streets network dataset such as StreetMap Premium for ArcGIS,
    ArcGIS Network Analyst for Desktop, and ArcGIS Network Analyst for Server.
    -->

    <fx:Style>
        @namespace mx "library://ns.adobe.com/flex/mx";
        mx|ToolTip
        {
            font-size: 14;
            backgroundColor: #EEEEEE;
        }
    </fx:Style>

    <fx:Script>
        <![CDATA[
            import com.esri.ags.FeatureSet;
            import com.esri.ags.Graphic;
            import com.esri.ags.SpatialReference;
            import com.esri.ags.events.MapMouseEvent;
            import com.esri.ags.events.ServiceAreaEvent;
            import com.esri.ags.tasks.supportClasses.NAMessage;

            import mx.controls.Alert;
            import mx.events.CloseEvent;
            import mx.rpc.events.FaultEvent;

            []private var facilities:FeatureSet;

            private function mapClickHandler(event:MapMouseEvent):void
            {
                // clear existing graphics on each map click
                if (facilitiesGraphicsLayer.numChildren > 0)
                {
                    facilitiesGraphicsLayer.clear();
                }
                if (resultsGraphicsLayer.numChildren > 0)
                {
                    resultsGraphicsLayer.clear();
                }

                facilities = new FeatureSet([]);

                var facility:Graphic = new Graphic(event.mapPoint);
                facilitiesGraphicsLayer.add(facility);

                facilities.features.push(facility);
                saParams.facilities = facilities;

                saTask.solve(saParams);
            }

            private function solveCompleteHandler(event:ServiceAreaEvent):void
            {
                // loop through service area polygons
                for (var i:int = 0; i < event.serviceAreaSolveResult.serviceAreaPolygons.length; i++)
                {
                    var polygonGraphic:Graphic = event.serviceAreaSolveResult.serviceAreaPolygons[i];
                    polygonGraphic.toolTip = polygonGraphic.attributes.Name;
                    resultsGraphicsLayer.add(polygonGraphic);
                }

                // Check for messages
                if (event.serviceAreaSolveResult.messages.length > 0)
                {
                    var showErrors:Boolean = false;
                    var msgDesc:String = "";
                    var numMessages:int = event.serviceAreaSolveResult.messages.length;
                    for (var j:int = 0; j < numMessages; j++)
                    {
                        var msg:NAMessage = event.serviceAreaSolveResult.messages[0] as NAMessage;
                        if (msg.type == 50)
                        {
                            msgDesc += "Warning: " + msg.description;
                        }
                        else if (msg.type == 100)
                        {

                            msgDesc += "Error: " + msg.description;
                            showErrors = true;
                        }
                        if (j < numMessages)
                        {
                            msgDesc += "\n";
                        }
                    }
                    if (showErrors)
                    {
                        facilitiesGraphicsLayer.remove(facilities.features.pop());
                        Alert.show(msgDesc, "Service Area Messages");
                    }
                }
            }

            private function faultHandler(event:FaultEvent):void
            {
                var msg:String = "Unable to calculate the service area.\nTry clicking closer to a street.\nTo see the full error message click 'OK'";
                Alert.show(msg, "Service Information", Alert.OK | Alert.CANCEL, this, onAlertClose, null, Alert.CANCEL);
                function onAlertClose(evt:CloseEvent):void
                {
                    switch (evt.detail) {
                        case Alert.OK:
                        {Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "Service Area Error " + event.fault.faultCode);
                            break;}
                        case Alert.CANCEL:
                            break;
                    }
                }
                // clear the "bad" facility
                facilitiesGraphicsLayer.clear();
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <esri:ServiceAreaTask id="saTask"
                              concurrency="last"
                              fault="faultHandler(event)"
                              requestTimeout="30"
                              showBusyCursor="true"
                              solveComplete="solveCompleteHandler(event)"
                              url="http://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/ServiceArea"/>

        <esri:ServiceAreaParameters id="saParams"
                                    defaultBreaks="{[]}"
                                    impedanceAttribute="TravelTime"
                                    outSpatialReference="{myMap.spatialReference}"
                                    restrictionAttributes="{['Avoid Gates','Avoid Private Roads','Avoid Unpaved Roads','Divider Restriction','Driving an Automobile','Oneway','Through Traffic Prohibited']}"
                                    returnFacilities="false"/>
    </fx:Declarations>

    <s:controlBarContent>
        <s:RichText width="100%">
            This sample demonstrates how to use the ServiceAreaTask
            to calculate a 1 minute drive time around a user defined geographic location.
            A network service area is a region that encompasses all accessible streets
            (that is, streets that are within a specified impedance).
            A 1-minute service area for a point on a network includes all the streets
            that can be reached within one minute from that point.
        </s:RichText>
    </s:controlBarContent>

    <esri:Map id="myMap" mapClick="mapClickHandler(event)">
        <esri:extent>
            <esri:Extent xmin="-13042947" ymin="3856278" xmax="-13041347" ymax="3857116">
                <esri:SpatialReference wkid="102100"/>
            </esri:Extent>
        </esri:extent>
        <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
        <esri:GraphicsLayer id="resultsGraphicsLayer">
            <esri:symbol>
                <esri:SimpleFillSymbol alpha="0.5" color="0xFF8800">
                    <esri:SimpleLineSymbol width="2" color="0xBD0010"/>
                </esri:SimpleFillSymbol>
            </esri:symbol>
        </esri:GraphicsLayer>
        <esri:GraphicsLayer id="facilitiesGraphicsLayer">
            <esri:symbol>
                <esri:SimpleMarkerSymbol color="0xEC0015"
                                         size="17"
                                         style="circle">
                    <esri:SimpleLineSymbol width="2" color="0xBD0010"/>
                </esri:SimpleMarkerSymbol>
            </esri:symbol>
        </esri:GraphicsLayer>
    </esri:Map>

</s:Application>