Przeliczanie danych GeoLokalizacyjnych

0

Witam

Od jakiegoś czasu próbuję przetworzyć dane pobierane cyklicznie z wbudowanego w urządzenie mobilne lokalizatora GPS na zwykłe stopnie i napisałem taką prostą stronkę w ASP.Net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test5.aspx.cs" Inherits="AjaxPart1.Test5" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Ajax testy własne</title>

    <script type="text/javascript">

        var wartosc1 = "";
        var wartosc2 = "";
        var wartosc3 = "";
        var error = "";
        var dane = new Array();

        setInterval(getLocation, 5000);

        function getLocation() {
            if (navigator.geolocation)
            {
                navigator.geolocation.getCurrentPosition(KonstruujArray, function () { }, { enableHighAccuracy: true });
            }
            else {
                error.innerHTML = "Ta przeglądarka nie obsługuje geolokacji.";
            }
        }

        function KonstruujArray(coordinates) {
            wartosc1 = coordinates.coords.latitude;
            wartosc2 = coordinates.coords.longitude;
            wartosc3 = coordinates.coords.accuracy;
            document.getElementById("<%=HiddenField1.ClientID%>").value = wartosc1;
            document.getElementById("<%=HiddenField2.ClientID%>").value = wartosc2;
            document.getElementById("<%=HiddenField3.ClientID%>").value = wartosc3;
            dane[0] = document.getElementById("<%=HiddenField1.ClientID%>").value;
            dane[1] = document.getElementById("<%=HiddenField2.ClientID%>").value;
            dane[2] = document.getElementById("<%=HiddenField3.ClientID%>").value;
            PageMethods.PrzeliczArray(dane, OnSuccess);
        }
        function OnSuccess(response, userContext, methodName) {
            document.getElementById("<%=Label1.ClientID %>").innerHTML = response[0];
            document.getElementById("<%=Label2.ClientID %>").innerHTML = response[1];
            document.getElementById("<%=Label3.ClientID %>").innerHTML = response[2];
            wartosc1 = "";
            wartosc2 = "";
            wartosc3 = "";
        }
</script>

</head>
<body>
    <form id="form1" runat="server">
            <div>
                
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>

                <asp:HiddenField ID="HiddenField1" runat="server" />
                <asp:HiddenField ID="HiddenField2" runat="server" />
                <asp:HiddenField ID="HiddenField3" runat="server" />
                <input id="btnGetTime" type="button" value="Show Current Time" onclick="getLocation()"/>
                <br />
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                <br />
                <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
                <br />
                <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
            </div>
    </form>
</body>
</html>

i backend...

using System;
using System.Web.UI;
using System.Device.Location;

namespace AjaxPart1
{
    public partial class Test5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "przeliczOdleglosc", "getLocation();", true);
        }

        // komunikacja na pojedynczej zmiennej przeslanej z strony html i zwrot przetworzonych danych
        [System.Web.Services.WebMethod]
        public static string[] PrzeliczArray(string[] arrayIn)
        {
            string wartosc1 = arrayIn[0];
            string wartosc2 = arrayIn[1];
            string wartosc3 = arrayIn[2];

            // aktualne współrzędne podane ze ClientSide w tablicy
            GeoCoordinate objectPosition = new GeoCoordinate(Convert.ToDouble(wartosc1), Convert.ToDouble(wartosc2));

            // współrzędne gate Shengen 1
            GeoCoordinate targetPosition = new GeoCoordinate(52.17021166666667, 20.971659999999996);

            // zwrocenie odleglosci miedzy wspolrzednymi
            double odleglosc = objectPosition.GetDistanceTo(targetPosition);


            double latitude = Convert.ToDouble(wartosc1);
            double longitude = Convert.ToDouble(wartosc2);

            string latitude_Kierunek = (latitude >= 0 ? "N" : "S");
            latitude = Math.Abs(latitude);
            double latMinPart = ((latitude - Math.Truncate(latitude) / 1) * 60);
            double latSecPart = ((latMinPart - Math.Truncate(latMinPart) / 1) * 60);

            string longitude_Kierunek = (longitude >= 0 ? "E" : "W");
            longitude = Math.Abs(longitude);
            double lonMinPart = ((longitude - Math.Truncate(longitude) / 1) * 60);
            double lonSecPart = ((lonMinPart - Math.Truncate(lonMinPart) / 1) * 60);

            string wsp1 = Convert.ToString(Math.Truncate(latitude) + " " + Math.Truncate(latMinPart) + " " + Math.Truncate(latSecPart) + " " + latitude_Kierunek);
            string wsp2 = Convert.ToString(Math.Truncate(longitude) + " " + Math.Truncate(lonMinPart) + " " + Math.Truncate(lonSecPart) + " " + longitude_Kierunek);

            string[] wynikowaArray = new string[3];

            wynikowaArray[0] = wsp1;
            wynikowaArray[1] = wsp2;
            wynikowaArray[2] = odleglosc.ToString();
            return wynikowaArray;
        }
    }

i w momencie gdy program dochodzi co tworzenia nowego obiektu

GeoCoordinate objectPosition = new GeoCoordinate(Convert.ToDouble(wartosc1), Convert.ToDouble(wartosc2));

wszystko wraca na front-end, i nic więcej nie przelicza, chociaż kod wydaje się być poprawny.

Czy ktoś mógłby mi wytłumaczyć dlaczego, albo podpowiedzieć jak to zrobić poprawnie?

2

Sprawadz przed tworzniem obiektu czy te Convert.ToDouble wykonuje sie poprawnie byc moze cos w stringu zlego siedzi ;)

0
Akihito napisał(a):

Sprawadz przed tworzniem obiektu czy te Convert.ToDouble wykonuje sie poprawnie byc moze cos w stringu zlego siedzi ;)

problem już rozwiązałem - jeśli kogoś interesuje gdzie był to służę:

żeczywiście Convert.tocokolwiek - nie wykonuje sie poprawnie w takiej konstrukcji
należy zastosować taką konstrukcję:

      Double.TryParse(arrayIn[0].ToString(), out latitude);
      Double.TryParse(arrayIn[1].ToString(), out longitude);

i wszystko zaczyna śmigać elegancko

Dzieki za reakcję :-)

1 użytkowników online, w tym zalogowanych: 0, gości: 1