请有人可以帮助我解决以下问题。在最后一行中,我收到一条错误消息,指出我
无法从字符串转换为双精度
尽管可以在前两行中使用。帮助将不胜感激。
<script>
mapboxgl.accessToken = '........';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [25.147437, 37.548452],
zoom: 9.5
});
// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());
map.on('load', function () {
var location = document.getElementById("1234").innerText;
var locations= eval('['+location+'];');
// Add a layer showing the places.
map.addLayer({
"id": "places",
"type": "symbol",
"source": {"type": "geojson","data": {"type": "FeatureCollection","features": locations}},"layout": {"icon-image": "{icon}-15","icon-allow-overlap": true}});
// When a click event occurs on a feature in the places layer, open a popup at the
// location of the feature, with description HTML from its properties.
map.on('click', 'places', function (e) {
var coordinates = e.features[0].geometry.coordinates.slice();
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML('<b>'+ e.features[0].properties.title + '</b>' + e.features[0].properties.description + '<p><img src='+e.features[0].properties.image+'></img></p>')
.addTo(map);
});
// Change the cursor to a pointer when the mouse is over the places layer.
map.on('mouseenter', 'places', function () {
map.getCanvas().style.cursor = 'pointer';
});
// Change it back to a pointer when it leaves.
map.on('mouseleave', 'places', function () {
map.getCanvas().style.cursor = '';
});
});
</script>
答案 0 :(得分:1)
为了检测错误的格式,您可以尝试TryParse
方法(在您的情况下为double.TryParse
)
double y1 = 0;
double x1 = 0;
double y2 = 0;
double x2 = 0;
if (!double.TryParse(YCrd1.Text, out y1))
Obs1Angle.Text = "Incorrect YCrd1 value";
else if (!double.TryParse(YCrd2.Text, out y2))
Obs1Angle.Text = "Incorrect YCrd2 value";
else if (!double.TryParse(XCrd1.Text, out x1))
Obs1Angle.Text = "Incorrect XCrd1 value";
else if (!double.TryParse(XCrd2.Text, out x2))
Obs1Angle.Text = "Incorrect XCrd2 value";
else {
// All values (x1, x2, y1, y2) are parsed
DeltaX.Text = $"{x2 - x1}";
DeltaY.Text = $"{y2 - y1}";
//DONE: If you want to compute Angle, you want Arc function (ASin, ATan, ACos etc.)
// Please, note, Atan2 - in case x2 - x1 == 0
Obs1Angle.Text = $"{Math.Atan2(y2 - y1, x2 - x1)}";
}
答案 1 :(得分:0)
尝试输入代码时,我收到错误消息:
CS1503参数1:无法从“十进制”转换为“双精度”
如果查看Math.Tan的文档,您会发现它以double
作为输入参数,而不是十进制!因此,您需要进行其他转换:
编辑:此外,您选择了错误的方法。看来您想要一个角度。在这种情况下,您需要使用以参数为参数的Atan方法:
代表切线的数字。
并返回:
以弧度为单位的角度θ,使-π/ 2≤θ≤π/ 2。
Obs1Angle.Text = Math.Atan((Convert.ToDouble(DeltaY.Text)) / (Convert.ToDouble(DeltaX.Text))).ToString();
答案 2 :(得分:0)
您应使用TryParse
来防止您的字符串不是数字。
decimal DeltaYVal, DeltaXVal;
if (decimal.TryParse(DeltaY.Text, out DeltaYVal) && decimal.TryParse(DeltaY.Text, out DeltaXVal))
{
Obs1Angle.Text = Math.Tan(DeltaYVal / DeltaXVal).ToString();
}
答案 3 :(得分:0)
转换为Double
并执行一次。
var deltaY = Convert.ToDouble(YCrd2.Text) - Convert.ToDouble(YCrd1.Text);
var deltaX = Convert.ToDouble(XCrd2.Text) - Convert.ToDouble(XCrd1.Text);
DeltaY.Text = deltaY.ToString();
DeltaX.Text = deltaX.ToString();
Obs1Angle.Text = Math.Tan(deltaY /deltaX).ToString();
很多整洁的代码,不是吗?
或者,如果要进行解析,请使用C#7.0 ...
if (double.TryParse(YCrd2.Text, out double yCrd2) &&
double.TryParse(YCrd1.Text, out double yCrd1) &&
double.TryParse(XCrd2.Text, out double xCrd2) &&
double.TryParse(XCrd1.Text, out double xCrd1))
{
var deltaY = yCrd2 - yCrd1;
var deltaX = xCrd2 - xCrd1;
DeltaY.Text = deltaY.ToString();
DeltaX.Text = deltaX.ToString();
Obs1Angle.Text = Math.Tan(deltaY /deltaX).ToString();
}
else
{
// show error in string to double conversion
}
PS:正如@DmitryBychenko在评论中指出的那样,如果Math.Atan2
可以为0,请使用Math.Tan
而不是DeltaX.Text
。