Hide Table of Contents
View Geoprocessing - Point in polygon search sample in sandbox
Geoprocessing - Point in polygon search


You can use the geometry service to determine spatial relationships between two sets of geometries. The query task can also calculate spatial relationships, but the geometry service exposes more relationship types and can be used with geometries that do not come from ArcGIS Server.

For example, this sample finds pizza restaurants within a 3 minute drive of a point that you click. The pizza restaurants are not part of a map service. They are just graphics on the map. The drive time polygon is not part of a map service either. It is returned as the result of a geoprocessing service.

Both the pizza restaurants and the drive time polygon are passed to the GeometryService.relation() method. Notice that one of the parameters for this method is the relationship type. In this sample we're looking for restaurants within the drive time polygon. In other words, they satisfy the relationship type esri.tasks.GeometryService.SPATIAL_REL_WITHIN.

You can specify a callback function to which an array of results will be sent. The array contains references to pairs of geometries that satisfied the relationship type. Each item in the array has a graphic1 and graphic2 property, referring to the order in which the graphics were passed to the relation function.

Note: Another simple way to check whether a point lies within a polygon is to use the Polygon.contains() method.


<!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Geometry Service: Relation</title>

    <link rel="stylesheet" href="https://js.arcgis.com/3.41/esri/css/esri.css">

      html, body, #mapDiv {
        height: 100%;
        margin: 0;
        padding: 0;
        width: 100%;
      #info {
        bottom: 20px;
        color: #444;
        height: auto;
        font-family: arial;
        left: 20px;
        margin: 5px;
        padding: 10px;
        position: absolute;
        text-align: left;
        width: 200px;
        z-index: 40;
      .label {
        display: inline-block;
        width: 4em;

    <script src="https://js.arcgis.com/3.41/"></script>
      var map;

        "dojo/dom", "dojo/_base/array", "dojo/promise/all", "dojo/json",

        "esri/map", "esri/domUtils", "esri/graphic", "esri/graphicsUtils",
        "esri/geometry/Point", "esri/tasks/GeometryService", "esri/tasks/Geoprocessor",
        "esri/tasks/FeatureSet", "esri/tasks/RelationParameters",
        "esri/Color", "esri/symbols/SimpleLineSymbol",
        "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleFillSymbol",
        "esri/request", "dojo/domReady!"
      ], function(
        dom, array, all, JSON,

        Map, domUtils, Graphic, graphicsUtils,
        Point, GeometryService, Geoprocessor,
        FeatureSet, RelationParameters,
        Color, SimpleLineSymbol,
        SimpleMarkerSymbol, SimpleFillSymbol,
      ) {

        var geoprocessor, geometryService, geometries, baseGraphics;

        map = new esri.Map("mapDiv", {
          basemap: "topo-vector",
          center: [-117.185, 34.052],
          zoom: 13
        map.on("click", mapClicked);

        geometryService = new GeometryService("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer");
        geoprocessor = new Geoprocessor("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons");
          wkid: 4326

        function mapClicked(evt) {
          geometries = [];

          // add a simple marker graphic at the location where the user clicked on the map.
          var pointSymbol = new SimpleMarkerSymbol(
            SimpleMarkerSymbol.STYLE_CROSS, 22,
            new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 128, 0]), 4));

          var clickPointGraphic = new Graphic(evt.mapPoint, pointSymbol);

          // use promise/all to monitor when place search and drive time calculation finish
            poiSearch: executeLocalSearch(clickPointGraphic),
            driveTimes: getDriveTimePolygon(clickPointGraphic)

        function executeLocalSearch(graphic) {
          // find (up to) 20 pizza restaurants within a the map's current extent
          var params = {
            text: "pizza",
            location: JSON.stringify(graphic.geometry.toJson()),
            bbox: JSON.stringify(map.extent.toJson()),
            maxLocations: 20,
            f: "json"

          return esriRequest({
            url: "//geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find",
            content: params,
            callbackParamName: "callback"

        function showLocations(response) {
          // create a symbol (Red Square) for pizza restaurants
          var pointSymbol = new SimpleMarkerSymbol(
            SimpleMarkerSymbol.STYLE_SQUARE, 6,
            new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 0, 0]), 1),
            new Color([255, 0, 0]));

          // add the pizza restaurants to the map
          var results = response.locations;
          baseGraphics = array.map(results, function(r) {
            var graphic = new Graphic(r.feature);
            return map.graphics.add(graphic);

        function getDriveTimePolygon(graphic) {
          // the graphic representing the selected location is passed onto the GP Task
          var featureSet = new FeatureSet();
          featureSet.features = [graphic];

          var params = {
            "Input_Location": featureSet,
            "Drive_Times": 3
          return geoprocessor.execute(params);

        function showDriveTime(results, messages) {
          var feature = results[0].value.features[0];

          // add the drive time polygon to the map
          var polySymbolRed = new SimpleFillSymbol(
            new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
              new Color([0, 0, 0, 1]), 1),
            new Color([255, 0, 0, 0.2]));

        function relateGeometries(results) {
          // check that both requests completed successfully
          if ( !results.poiSearch || !results.driveTimes ) {
            alert("Unable to compute point in polygon.");
          console.log("baseGraphics", baseGraphics);
          var relationParams = new RelationParameters();
          relationParams.geometries1 = graphicsUtils.getGeometries(baseGraphics);
          relationParams.geometries2 = graphicsUtils.getGeometries(results.driveTimes[0].value.features);
          relationParams.relation = RelationParameters.SPATIAL_REL_WITHIN;

          // alternatively, could use polygon.contains instead of using the geometry service

        function addRelateResultsToMap(relations) {
          console.log("add relate results", relations);
          // create a Green Square symbol
          var symbol = new SimpleMarkerSymbol(
            SimpleMarkerSymbol.STYLE_SQUARE, 8,
            new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
              new Color([0, 0, 0]), 1),
            new Color([0, 255, 0]));
          // highlight the pizza places that satisfy the spatial relation (WITHIN) against the 3-minute drive time polygon
          array.forEach(relations, function(relation) {


  <body class="claro">
    <div id="mapDiv"></div>
    <div id="info" class="esriSimpleSlider">
      Click on the map to show Pizza restaurants that are within 3 minute drive time.
Show Modal