Calculate distance between two markers.

1696
6
Jump to solution
10-07-2014 09:36 AM
LucasBrito
New Contributor

We are trying to calculate the distance between two markers plotted in our map. We tried using the Polyline object but that returns a straight line from one marker to the other, not the real distance, or route. Is there a way to do so? We are using the following function...

function sitepins(val) {

                var iconPath  = "M15.834,29.084 15.834,16.166 2.917,16.166 29.083,2.917z";

                var initColor = "#005CE6";

                var ind=0;

                for (var index = 0; index < arr.length; index++){

                    if (arr[index]==val){

                        ind =index;

                    }

                Lon = arr[ind+1];

                Lat = arr[ind+2];

                Lon1 = arr[ind+4];

                Lat1 = arr[ind+5];

                //var polylineJson = {"paths":[[[-122.68,45.53], [-122.58,45.55]]]};

                var singlePathPolyline = new Polyline([[-50, 0], [-120, -20], [-130, 0]]);

                //var polyline = new Polyline(polylineJson);

                var lengths = geodesicUtils.geodesicLengths([singlePathPolyline], Units.KILOMETERS);

                console.log(lengths);

            }

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TimWitt2
MVP Alum

Lucas,

if you have a hosted route task, you can do something like this:

Find routes with barriers and multiple stops | ArcGIS API for JavaScript

Instead of the user clicking to points on the map, you can just feed it your points. As you can see in the screenshot, the route result will include drive length/time.

Hope this helps!

Tim

View solution in original post

6 Replies
TimWitt2
MVP Alum

Lucas,

Is it supposed to give you a route between the markers, like driving directions? If so, you might want to look into this

directions-amd | API Reference | ArcGIS API for JavaScript

Tim

0 Kudos
LucasBrito
New Contributor

Hello again Tim.

We are just hoping to show the distance from where the person fired an event in comparison to the location where he should be. So we don't really need directions, just the distance measurements...

0 Kudos
TimWitt2
MVP Alum
  1. var lengths = geodesicUtils.geodesicLengths([singlePathPolyline], Units.KILOMETERS); 
  2.                 console.log(lengths);

This should have returned the total length of your line. Did it not work? I think I might have a hard time understanding what you actually want to show.

0 Kudos
LucasBrito
New Contributor

Tim, we would like to display the driving time to the point, not the directions...

Going back to the snippet I provided, we get the following error: Uncaught TypeError: object is not a function

0 Kudos
TimWitt2
MVP Alum

Lucas,

if you have a hosted route task, you can do something like this:

Find routes with barriers and multiple stops | ArcGIS API for JavaScript

Instead of the user clicking to points on the map, you can just feed it your points. As you can see in the screenshot, the route result will include drive length/time.

Hope this helps!

Tim

Rodrigo_Castro
New Contributor

Good morning, I need to calculate the distance between 32000 polygons and memory error occurs during parsing. My computer has 8 gig of memory and a good processor that is not fully used during the analysis. I tried to use only 200 polygons in the analysis and the arcgis calculated normally. What is the limit on the number of polygons for the analysis of distance?

0 Kudos