Skip To Content

Tutorial: Publishing a network analysis service

Available with Network Analyst license.

In this topic
Complexity: Beginner Data Requirement: Installed with software Data Path: <ArcGIS for Server installation directory>\help\samples\data\network_analyst\SanFrancisco.gdb Goal: Publish a network analysis service to ArcGIS Server and consume it in ArcMap and a web application.

Network analysis can be performed on a transportation network over the web by hosting network analysis services on ArcGIS Server. This includes finding the closest facility, the best route for a vehicle, the best routes for a fleet of vehicles; locating facilities using location-allocation; calculating an OD cost matrix; and generating service areas. This tutorial walks you through the process of creating a map document capable of being used for network analysis and publishing it as a network analysis service. It then refers you to help on how to consume the network analysis service in ArcMap and a web application. To learn more about the different types of network analysis and their capabilities, see Types of network analyses in the ArcGIS Help.

Before beginning this tutorial

If you've just installed ArcGIS Server, you need to complete some preparatory steps before you can connect to the server and publish services:

Tutorial data

The data for this tutorial is installed with ArcGIS Server and is located at <ArcGIS for Server installation directory>\help\samples\data\network_analyst.

Optionally, you can try to work through the steps using your own data instead.

Creating the map document

A network analysis service requires a map service that contains one or more network analysis layers. This means the map document published to the server must contain one or more layers that are network analysis layers created with tools from the Network Analyst toolbar. Network analysis services support route, closest facility, and service area analysis layers.

Follow these steps to create a single map document capable of performing all three of these types of network analysis:

  1. Start ArcMap and open a new blank map document.
  2. Click Customize > Extensions and check Network Analyst to enable the Network Analyst extension.
  3. Click Customize > Toolbars > Network Analyst to make the Network Analyst toolbar visible.
  4. Add a network dataset, which is required to be in the map to create a network analysis layer, by clicking Add DataAdd Data on the standard toolbar. Browse to the Streets_ND network dataset located in the SanFrancisco database. When prompted to add all feature classes that participate in Streets_ND to the map, click No.
  5. Add a route network analysis layer to your map by clicking the Network Analyst drop-down menu on the Network Analyst toolbar and choosing New Route.

    Network Analyst toolbar

    When a network analysis layer is created in ArcMap, it is automatically bound to the active network dataset. The San Francisco network was active when you created the route analysis layer, so these two layers are bound together. Also, the network analysis layer references the network dataset it is bound to by file path, so the network dataset layer is no longer needed in ArcMap.

  6. Right-click Streets_ND or the name of your network dataset layer and click Remove. Although the map is blank, the route analysis layer is present and can be published as a service. When you eventually add the published network analysis service to a map, it won't display any features initially, that is, until it generates results. To provide additional context when you consume a network analysis service, you can also consume a cached map service, such as one provided by ArcGIS Online.
  7. Click File > Save As to save the map document.

Publishing the network analysis service

Once you have a map document with a network analysis layer, you're ready to publish it as a network analysis service. You'll publish the map document as a service and enable the network analysis capability, which creates a network analysis service of the same name.

To configure and publish your map document to ArcGIS Server using ArcMap, follow these steps.

  1. Remaining in ArcMap, choose File > Share As > Service from the main menu.
  2. In the Share as Service window, choose Publish a service. Click Next.
  3. In the Publish a Service dialog box, click Connect to ArcGIS ServerAdd ArcGIS Server to create a new connection to the server.
  4. In the Add ArcGIS Server window, choose Publish GIS Services. Click Next.
  5. For the Server URL, type the URL of the ArcGIS Server site you want to connect to. For example, http://gisserver.domain.com:6080/arcgis.
  6. From the Server Type drop-down list, choose ArcGIS Server.
  7. During the publishing process, a service definition file is created and temporarily stored locally on disk. When the publishing process completes, the service definition uploads to the server and the local file is deleted. For the purposes of this tutorial, accept the default staging folder and continue.
  8. If your server administrator has enabled security for your site, enter your User Name and Password. Click Finish.
  9. Optionally, in the Publish a Service window, enter a new name for the service. The name cannot be more than 120 characters long and may contain only alphanumeric characters and underscores. Click Next.
  10. By default, services are published to the root folder (root) of ArcGIS Server. Services can be organized into subfolders under the root folder. Choose the folder where you want to publish the service or create a new folder to contain the service. Click Continue.
  11. The Service Editor displays. You'll use the Service Editor to choose what users can do with your network analysis service and take fine-grained control of how the server exposes your service. Click the Capabilities tab.
  12. By default, mapping and KML are enabled. Check Network Analysis.
  13. In the left pane of the Service Editor, click the Network Analysis tab and review the operations and properties enabled for your network analysis service. For the purposes of this tutorial, you can accept the defaults and continue. However, if you want to learn more, see Network analysis services. For information on how to best configure your network analysis service for deployment, see Tuning and configuring services.
  14. Click AnalyzeAnalyze. This examines your map document to see if it can be published to the server.
    Tip:

    To give yourself more viewing area when configuring your network analysis service, click the CollapseCollapse button at the top of the Service Editor.

  15. Fix any ErrorsError in the Prepare window; this must be done before you can publish. Optionally, you can fix warnings and informational messages to further improve the performance and appearance of your network analysis service. For more information about resolving these issues, see Analyzing your GIS resource.
    Note:

    You can register folders and geodatabases with your ArcGIS Server site, thus ensuring that the server can recognize and use your data. If you proceed with the following steps, any data referenced by your map document originating from an unregister folder or geodatabase is copied to the server at the time that you publish. This is a precautionary measure to ensure that the server can access all the data used by the service. For full instructions on registering a folder or geodatabase with your ArcGIS Server site, see Registering your data with ArcGIS Server using ArcGIS for Desktop.

  16. Once you've fixed the errors in your map document, click PublishPublish.

Your map document publishes to ArcGIS Server, which creates a map service and an associated network analysis service with the same name.

Consuming the network analysis service

Clients of ArcGIS Server, for example, ArcGIS for Desktop and web browsers, have the capability to consume network analysis services. In ArcMap, the Find RouteFind Route button provides you with out-of-the-box functionality to utilize routing capabilities provided by your network analysis service. In web browsers, network analysis services are consumed programmatically through web applications built with ArcGIS.com, Portal for ArcGIS, and the ArcGIS API for JavaScript. For more information about these clients, see Network analysis services .

To learn how to consume your network analysis service in ArcGIS for Desktop or a basic web application built with the ArcGIS API for JavaScript, see the sections below:

Consuming a routing service in ArcMap

  1. Start ArcMap and open a new blank ArcMap document.
  2. You should still have a publisher connection to ArcGIS Server, but you will need a user connection to consume this service in ArcMap. In the Catalog window, expand the GIS Servers node and double-click Add ArcGIS Server to begin making a user connection. For step-by-step instructions on setting up the connection, see Making a user connection to ArcGIS Server in ArcGIS for Desktop.
  3. In the Catalog window, expand the GIS Servers node and double-click your new ArcGIS Server connection, which ends with "(user)".
  4. Within your server connection, locate the map service that contains your network analysis service and drag it into the table of contents.
  5. Although the layer is present in the table of contents, the map is still blank. To provide context to your map, add a basemap layer from ArcGIS Online.
    1. Click the drop-down arrow next to the Add Data button Add Data on the Standard toolbar and choose Add Basemap.
    2. Double-click a basemap, such as Topographic.
  6. Zoom in to the San Francisco Peninsula, which is on the south side of the map, because the stops you are going to add need to fall within the bounds of the San Francisco network dataset that you used to create the network analysis layer. If you are not using the provided SanFrancisco database, you can skip this step.
  7. On the Tools toolbar, click Find RouteFind Route.
  8. On the Find Route dialog box, click the Options tab.
  9. Click the Routing Service drop-down list and choose Route <Layers\[name of your route service]>.
  10. Click the Stops tab. Use the controls and parameters in the Stops tab to provide inputs to the network analysis.
  11. When you are ready to solve the route, click Find Route.

Consuming a routing service in a basic web application

  1. Before you can consume a network analysis service in a web application, you need to know its URL. In a web browser, navigate to the ArcGIS Server Services Directory, for example, http://gisserver.domain.com:6080/arcgis/rest/services.
  2. In the services list, click your network analysis service. All network analysis services are appended with (NAServer). If your service resides in a folder, navigate to that folder and select the service.
  3. A page displays showing the name of your service, service description, and a list of available analysis layers. Click Route.
  4. A page displays showing the properties of the route layer. In your browser's address bar, copy the URL of this page and paste it into a text editor. This is the REST URL to the route task of your network analysis service. The URL formats as follows: http://gisserver.domain.com:6080/arcgis/rest/services/folder/service/NAServer/Route.
  5. Copy and paste the sample code below into a text editor, such as Notepad.

    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10">
        <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
          on iOS devices-->
        <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
        <title>Simple Routing</title>
    
        <link rel="stylesheet" href="http://js.arcgis.com/3.6/js/dojo/dijit/themes/claro/claro.css">
        <link rel="stylesheet" href="http://js.arcgis.com/3.6/js/esri/css/esri.css">
        
        <script src="http://js.arcgis.com/3.6/"></script>
        <script>
          dojo.require("esri.map");
          dojo.require("esri.tasks.route");
    
          var map, routeTask, routeParams;
          var stopSymbol, routeSymbol, lastStop;
    
          function init() {
            map = new esri.Map("map", {
              basemap: "streets",
              center: [-122.44, 37.78],
              zoom: 12
            });
    
            dojo.connect(map, "onClick", addStop);
    
            routeTask = new esri.tasks.RouteTask("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route");
            
            //set up the route parameters
            routeParams = new esri.tasks.RouteParameters();
            routeParams.stops = new esri.tasks.FeatureSet();
            routeParams.outSpatialReference = {"wkid":102100};
            
            dojo.connect(routeTask, "onSolveComplete", showRoute);
            dojo.connect(routeTask, "onError", errorHandler);
    
            //define the symbology used to display the route
            stopSymbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CROSS).setSize(15);
            stopSymbol.outline.setWidth(4);
            routeSymbol = new esri.symbol.SimpleLineSymbol().setColor(new dojo.Color([0,0,255,0.5])).setWidth(5);
          }
    
          //Adds a graphic when the user clicks the map. If 2 or more points exist, route is solved.
          function addStop(evt) {
            var stop = map.graphics.add(new esri.Graphic(evt.mapPoint, stopSymbol));
            routeParams.stops.features.push(stop);
    
            if (routeParams.stops.features.length >= 2) {
              routeTask.solve(routeParams);
              lastStop = routeParams.stops.features.splice(0, 1)[0];
            }
          }
    
          //Adds the solved route to the map as a graphic
          function showRoute(solveResult) {
            map.graphics.add(solveResult.routeResults[0].route.setSymbol(routeSymbol));
          }
    
          //Displays any error returned by the Route Task
          function errorHandler(err) {
            alert("An error occurred\n" + err.message + "\n" + err.details.join("\n"));
    
            routeParams.stops.features.splice(0, 0, lastStop);
            map.graphics.remove(routeParams.stops.features.splice(1, 1)[0]);
          }
          dojo.ready(init);
        </script>
    
      </head>
      <body class="claro">
        <div id="map" style="width:600px; height:400px; border:1px solid #000;"></div>
         <p>Click on the map to add stops to the route. The route from the last stop to the newly added stop is calculated. If a stop is not reachable, it is removed and the last valid point is set as the starting point.</p>
      </body>
    </html>

  6. Locate the routeTask = new esri.tasks.RouteTask parameter and replace the URL in the parentheses to match the REST URL of your network analysis route task.
  7. Save and close the text document.
  8. Modify the file extension of the document to use .html instead of its native extension. For example, change Route.txt to Route.html.
  9. Open the HTML document with a web browser. Follow the instructions in the web application to use your network analysis service.