ho bisogno di una funzione per calcolare la distanza tra il punto A e il punto B.Come utilizzare System.Spatial.GeographyPoint
Namespace System.Spatial
sembra essere esattamente quello che mi serve, ma io non riesco a capire come usarlo.
IPoint
è un'interfaccia che fornisce Latitude
e Longitude
, entrambi di tipo double
.
Prima prova:
public static double CalculateDistance(IPoint pointA, IPoint pointB)
{
var gp1 = GeographyPoint.Create(pointA.Latitude, pointA.Longitude);
var gp2 = GeographyPoint.Create(pointB.Latitude, pointB.Longitude);
return gp1.Distance(gp2) ?? 0;
}
che termina con un NotImplementedException
su point1.Distance(point2)
. Il messaggio è in francese, ma in pratica dice "Nessuna operazione registrata. Fornisci l'operazione utilizzando la proprietà SpatialImplementation.CurrentImplementation.Operations".
Ok, cercherò questo allora:
public static double CalculateDistance(IPoint pointA, IPoint pointB)
{
var gp1 = GeographyPoint.Create(pointA.Latitude, pointA.Longitude);
var gp2 = GeographyPoint.Create(pointB.Latitude, pointB.Longitude);
return SpatialImplementation.CurrentImplementation.Operations.Distance(gp1, gp2);
}
ora ho un NullReferenceException
su SpatialImplementation.CurrentImplementation.Operations
.
Msdn non è molto dettagliato.
Chiunque può spiegare come ottenere queste implementazioni?
Come si chiama quel metodo e si è implementata l'interfaccia 'IPoint' con una classe? – Habib
Certo che l'ho implementato. Fornisce solo latitudine e longitudine. Gli errori sono esattamente ciò che ho detto. Sto chiamando questo da unit test per ora. – Johnny5