@arcgis/core - Find distance between 2 points

545
5
12-04-2022 05:48 AM
MosheLevi
New Contributor

Hi, I am use @arcgis/core package in Angular framework.

I want to calculate distance between 2 points by lat and lng of each point using this package. 

I guess that i need something like described in this link. But i don't know how to convert this to the ESM approach. 

I guess that i need to import the packages like below, but i don't know how to continue from there. 

Any help will be appreciated.

Note that i have no need to draw Map. I need just the calculation.

import * as GeometryService from "@arcgis/core/geometry";
import Point from "@arcgis/core/geometry/Point";
import Map from "@arcgis/core/Map";
import SpatialReference from "@arcgis/core/geometry/SpatialReference";
import DistanceParameters from "@arcgis/core/rest/support/DistanceParameters";
0 Kudos
5 Replies
ReneRubalcava
Frequent Contributor

You can use the geometry engine for this, no need to go to the service.

https://developers.arcgis.com/javascript/latest/api-reference/esri-geometry-geometryEngine.html#dist...

Here's a sample where it's dynamically imported and used.

https://github.com/odoe/nearby-app-vanilla/blob/main/src/app.ts#L50-L71

0 Kudos
MosheLevi
New Contributor

First of all, thank you very much for your response!


Actually, I need to calculate a distance of driving or walking (routing).
From what I understand, the distance  function calculates Planar distance.

 

I'm sorry I didn't explain it in advance properly

0 Kudos
MosheLevi
New Contributor

After more searching, i find out this page witch seems to fit what i need. 

I just don't understand what it is the:

portalItem: { id: "69569b47b1e445b8a42ec12feab41ce9" }

I try to replace this to list of stops (that this what i actually need) like this:

    const stops = [
      new Stop({ geometry: { latitude: 32.180763, longitude: 34.929558 } }),
      new Stop({ geometry: { latitude: 31.9339, longitude: 34.8878 } })
    ];
    const routeLayer = new RouteLayer({
      stops,
      // portalItem: {
      //   id: "69569b47b1e445b8a42ec12feab41ce9"
      // }
    });
 
But then, the 'routeLayer.routeInfo' is null. 
 
What am i missing? 
0 Kudos
BlakeTerhune
MVP Regular Contributor

The portalItem id is the item in Portal for ArcGIS that is the routeLayer, which looks as though you can create it from stops.

// A minimum of two stops is required for routing.
const stops = [
    new Stop({ geometry: { latitude: 32.180763, longitude: 34.929558 } }),
    new Stop({ geometry: { latitude: 31.9339, longitude: 34.8878 } })
];
// Create route layer and assign the stops. Only solved routelayers will be rendered.
const routeLayer = new RouteLayer({
    stops
});
0 Kudos
ReneRubalcava
Frequent Contributor

Once you create the route layer, you need to call the solve method to get your route and features

https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-RouteLayer.html#solve

Then you can get the routeInfo and it will have a totalDistance value you can use

https://developers.arcgis.com/javascript/latest/api-reference/esri-rest-support-RouteInfo.html#total...

0 Kudos