private static extern void GetRecommendationsOneGroup( UserRequirementFromCSharp reqirement, ref IntPtr routes, ref int level, ref IntPtr error_code, ref IntPtr error, ref IntPtr remark, ref int route_group_nums, ref int route_size_in_routes, ref IntPtr first_route_of_one_size_array);
public async Task <double> GetDirectedDistanceAsync(UserRequirementFromCSharp userRequirement) { var startCityLngAndLatTask = GetLongitudeAndLatitudeDictionaryAsync(userRequirement.start_cities); var arrivalCityLngAndLatTask = GetLongitudeAndLatitudeDictionaryAsync(userRequirement.arrive_cities); var startCityLngAndLat = await startCityLngAndLatTask; var arrivalCityLngAndLat = await arrivalCityLngAndLatTask; double R = 6371;//地球半径 return(Math.Acos(Math.Sin((Math.PI / 180) * startCityLngAndLat["lat"]) * Math.Sin((Math.PI / 180) * arrivalCityLngAndLat["lat"]) + Math.Cos((Math.PI / 180) * startCityLngAndLat["lat"]) * Math.Cos((Math.PI / 180) * arrivalCityLngAndLat["lat"]) * Math.Cos((Math.PI / 180) * arrivalCityLngAndLat["lng"] - (Math.PI / 180) * startCityLngAndLat["lng"])) * R); }
public async Task <IEnumerable <IEnumerable <Route> > > GetRouteFromCppAsync() { return(await Task.Run(() => { int level = 0; IntPtr error_code = new IntPtr(); IntPtr error = new IntPtr(); IntPtr remark = new IntPtr(); int route_group_nums = 0; int route_size_in_routes = 0; IntPtr first_route_of_one_size_array = new IntPtr(); IntPtr routesFromCpp = new IntPtr(); List <List <Route> > routes = new List <List <Route> >(); UserRequirementFromCSharp userRequirement = (UserRequirementFromCSharp)_userRequirement; GetRecommendationsOneGroup(userRequirement, ref routesFromCpp, ref level, ref error_code, ref error, ref remark, ref route_group_nums, ref route_size_in_routes, ref first_route_of_one_size_array); if (level != 0)//出现了异常了 { string errorCodeStr = Marshal.PtrToStringAnsi(error_code); string errorMessage = Marshal.PtrToStringAnsi(error); DealExceptionFromCpp(level, errorCodeStr, errorMessage); } if (remark != null) { _remark = Marshal.PtrToStringAnsi(remark); } int[] first_route_of_one_size_array_managed = new int[route_group_nums]; if (first_route_of_one_size_array != null) { Marshal.Copy(first_route_of_one_size_array, first_route_of_one_size_array_managed, 0, route_group_nums); } IntPtr[] result = new IntPtr[route_size_in_routes]; if (routesFromCpp != null) { Marshal.Copy(routesFromCpp, result, 0, route_size_in_routes); } if (result.Length == 0) { throw new CustomExceptionOfHongJieSun(3, "ROUTE_RESULT_EMPTY", "对不起,该需求暂时没有可推荐的结果"); } int routeReadStartIndex = 0;//开始读取线路的下标 for (int i = 0; i < route_group_nums; i++) { routes.Add(new List <Route>(first_route_of_one_size_array_managed[i])); if (first_route_of_one_size_array_managed[i] == 1) { routes[i].Add(Marshal.PtrToStructure <Route>(result[routeReadStartIndex])); routeReadStartIndex++; continue; } else if (first_route_of_one_size_array_managed[i] == 2) { routes[i].Add(Marshal.PtrToStructure <Route>(result[routeReadStartIndex])); routeReadStartIndex++; routes[i].Add(Marshal.PtrToStructure <Route>(result[routeReadStartIndex])); routeReadStartIndex++; continue; } else { throw new CustomExceptionOfHongJieSun(1, "ERROR_SIZE_OF_ONE_ROUTE", $"first_route_of_one_size_array_managed[{i}]={first_route_of_one_size_array_managed[i]}错误的大小,只有1或2正确"); } } FreeMemoryOneGroup(ref routesFromCpp, ref first_route_of_one_size_array, route_size_in_routes); return routes; })); }