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
}));
});
Solved! Go to Solution.
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;
});
});
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;
});
});