Select to view content in your preferred language

How to Calculate Distance Between Two Give Points

3392
1
Jump to solution
12-12-2017 02:37 PM
BehrouzHosseini
Regular Contributor

Using ArcGIS API for JavaScript 3.22 and having two given Points as

var pt1 = new Point(-106.61, 35.1107, new SpatialReference({ wkid: 4326 }));
var pt2 = new Point(-108.61, 35.1107, new SpatialReference({ wkid: 4326 }));

can you please let me know how I can calculate the distance from `pt1 to `pt2`?

var map;
 require([
 "esri/map",
 "esri/geometry/Point",
 "esri/SpatialReference",
 "esri/geometry/webMercatorUtils",
 "dojo/domReady!"
 ], function(Map, Point, SpatialReference, webMercatorUtils) {
 map = new Map("map", {
 basemap: "topo",
 center: [-106.61, 35.1107],
 zoom: 8
 });
 geometryService = new esri.tasks.GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
 var pt1 = new Point(-106.61, 35.1107, new SpatialReference({
 wkid: 4326
 }));
 var pt2 = new Point(-108.61, 35.1107, new SpatialReference({
 wkid: 4326
 }));
 });
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Bengi,

   Here is your code updated for that:

var map;
require([
  "esri/map",
  "esri/geometry/Point",
  "esri/SpatialReference",
  "esri/tasks/GeometryService",
  "esri/tasks/DistanceParameters",
  "dojo/domReady!"
  ], function(Map, Point, SpatialReference, GeometryService, DistanceParameters
){
  map = new Map("map", {
    basemap: "topo",
    center: [-106.61, 35.1107],
    zoom: 8
  });
  geometryService = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
  var pt1 = new Point(-106.61, 35.1107, new SpatialReference({
    wkid: 4326
  }));
  var pt2 = new Point(-108.61, 35.1107, new SpatialReference({
    wkid: 4326
  }));

  var distParams = new DistanceParameters();
  distParams.distanceUnit = GeometryService.UNIT_STATUTE_MILE;

  distParams.geometry1 = pt1;
  distParams.geometry2 = pt2;
  distParams.geodesic = true;
  geometryService.distance(distParams, function(distance) {
    //result = distance;
  });
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

1 Reply
RobertScheitlin__GISP
MVP Emeritus

Bengi,

   Here is your code updated for that:

var map;
require([
  "esri/map",
  "esri/geometry/Point",
  "esri/SpatialReference",
  "esri/tasks/GeometryService",
  "esri/tasks/DistanceParameters",
  "dojo/domReady!"
  ], function(Map, Point, SpatialReference, GeometryService, DistanceParameters
){
  map = new Map("map", {
    basemap: "topo",
    center: [-106.61, 35.1107],
    zoom: 8
  });
  geometryService = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
  var pt1 = new Point(-106.61, 35.1107, new SpatialReference({
    wkid: 4326
  }));
  var pt2 = new Point(-108.61, 35.1107, new SpatialReference({
    wkid: 4326
  }));

  var distParams = new DistanceParameters();
  distParams.distanceUnit = GeometryService.UNIT_STATUTE_MILE;

  distParams.geometry1 = pt1;
  distParams.geometry2 = pt2;
  distParams.geodesic = true;
  geometryService.distance(distParams, function(distance) {
    //result = distance;
  });
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍