我敢肯定这会很容易回答,并且可以归因于我对 C# 的“耳后湿”不止一点,但我在调用并将值放入函数之前遇到问题,然后在屏幕上写入响应.
该函数和调用在 PHP 中完美运行(我有更多经验),所以我认为问题更多在于在 C# 中使用正确的语法而不是其他任何东西。也许我还在尝试用我的 PHP 帽子写它?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace postcode
{
public partial class postcode_finder : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Example lat/long values entered below
Response.Write(distance(32.9697, -96.80322, 29.46786, -98.53506, "M"));
}
public double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
dist = Math.Acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K')
{
dist = dist * 1.609344;
}
else if (unit == 'N')
{
dist = dist * 0.8684;
}
return dist;
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:: This function converts decimal degrees to radians :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
public double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:: This function converts radians to decimal degrees :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
public double rad2deg(double rad)
{
return (rad / Math.PI * 180.0);
}
}
}
我得到的错误说'postcode.postcode_finder.distance(double,double,double,double,char)的最佳重载方法匹配'有一些无效的参数'。
我哪里错了?这里希望有更多专业知识的人可以在正确的方向上推动我。
谢谢