Computing distance in Map 3D using .net API

By Daniel Du

Q:

I’d like to know how to compute the distance of two points using map 3D .net API

A:

Here is the sample code to do the same:

[CommandMethod("ComputeDistance")]

public void ComputeDistance()

{

  //I am using LL84 coordinate system for this sample

  //As an example, this is longtitue/Latitue of two points

 

  double x1 = -87.7104750022991;

  double y1 = 43.7017449116101;

  double x2 = -87.703061972587;

  double y2 = 43.7016702994388;

 

  Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

 

  //Get coordinate system of current map

  AcMapMap currentMap = AcMapMap.GetCurrentMap();

  string srsWkt = currentMap.GetMapSRS();

  //ed.WriteMessage("srs = " + srsWkt + "\n");

  MgCoordinateSystemFactory coordSysFactory = new MgCoordinateSystemFactory();

  MgCoordinateSystem coordSys = coordSysFactory.Create(srsWkt);

 

  //compute gread circle distance

  double distance = coordSys.MeasureGreatCircleDistance(x1, y1, x2, y2);

  distance = coordSys.ConvertCoordinateSystemUnitsToMeters(distance);

  ed.WriteMessage("gread circle dist = " + distance.ToString() + "\n");

 

  //compute Euclidean distance

  distance = coordSys.MeasureEuclideanDistance(x1, y1, x2, y2);

  distance = coordSys.ConvertCoordinateSystemUnitsToMeters(distance);

  ed.WriteMessage("Euclidean distance = " + distance.ToString() + "\n");

 

 

  //Another method, compute the distance from Newyork to Boston

  MgCoordinateSystemMeasure coordSysMeasure = coordSys.GetMeasure();

  double dist = coordSysMeasure.GetDistance(-74.806394, 40.714169, -71.061342, 42.355892);

  dist = coordSys.ConvertCoordinateSystemUnitsToMeters(dist);

  ed.WriteMessage("distance from Newyork to Boston is " + dist.ToString() + " meters\n");

 

}

Hope this helps.


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading