如何在C#中获取设备的当前位置

时间:2013-12-04 06:15:50

标签: c# asp.net asp.net-mvc

我正在申请申请,我需要找到距离最近的200英里范围内的医院。我有一个医院列表,其纬度和经度都保存在数据库中,但我想得到设备的位置。我如何获得设备位置。我在下面使用以下代码。

namespace GoogleMaps
{
    class Program
    {
        private double HaversineDistance(double firstLat, double firstLong, double secondLat, double secondLong)
        {
            double dLat = this.toRadian(secondLat - firstLat);
            double dLon = this.toRadian(secondLong - firstLong);

            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Cos(this.toRadian(firstLat)) * Math.Cos(this.toRadian(secondLat)) *
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
            double d = 6371 * 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
            return d;

        }

        private double toRadian(double val)
        {
            return (Math.PI / 180) * val;
        }

        static void Main(string[] args)
        {
            double dist;
            Program a = new Program();
            dist = a.HaversineDistance(28.6100, 77.2300, 26.8470, 80.9470);
            Console.WriteLine("Harversine Distance  = " + dist);
            Console.ReadKey();
        }
    }
}

2 个答案:

答案 0 :(得分:4)

请你可以使用 GeoCoordinateWatcher Class

或查看this

这里也是click

的好解决方案

上面的链接演示是:

enter image description here

答案 1 :(得分:0)

well you can use this code it  calculates distance between my current location with the location  i have hard coded i used india gate you can easily get the fixed locations from database with little modifications.

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body runat="server">
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (p) {
            var cur_loc = (p.coords.latitude + "," + p.coords.longitude);
            var indiagate = ("28.6109" + "," + "77.2344");//cordinates if India Gate
            var com_cord = indiagate + ":" + cur_loc;
            var indcord = com_cord.split(":");
            for (var i = 0; i < indcord.length; i++) {
                var ncord_cord = (indcord[i]);
                var split_fincord = ncord_cord.split(",");
                var finalcordinates = "(" + parseFloat(split_fincord[0]) + "," + parseFloat(split_fincord[1]) + ")";
                var data = indcord[i];
                var LatLng = new google.maps.LatLng(parseFloat(split_fincord[0]), parseFloat(split_fincord[1]));
                var mapOptions = {
                    center: LatLng,
                    zoom: 13,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
                var marker = new google.maps.Marker({
                    position: LatLng,
                    map: map,
                    title: "<div style = 'height:60px;width:200px'><b>Your location:</b><br />Latitude: " + parseFloat(split_fincord[0]) + "<br />Longitude: " + parseFloat(split_fincord[1])


                });




                google.maps.event.addListener(marker, "click", function (e) {
                    var infoWindow = new google.maps.InfoWindow(), marker, data;
                    infoWindow.setContent(marker.title);
                    infoWindow.open(map, marker);
                });


            }
            var newlocstring = com_cord.replace(":", ",");
            //alert (newlocstring);
            var new_pts = newlocstring.split(",");

            var d = getDistanceFromLatLonInKm(new_pts[0], new_pts[1], new_pts[2], new_pts[3]);
            //alert(d);
            document.getElementById('<%=lbl1.ClientID%>').textContent = d.toFixed(2) + "Km";
            calcRoute(new_pts[0], new_pts[1], new_pts[2], new_pts[3])
        });



    } else {
        alert('Geo Location feature is not supported in this browser.');
    }

    function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {

        var R = 6371; // Radius of the earth in km
        var dLat = deg2rad(parseFloat(lat2) - parseFloat(lat1));  // deg2rad below

        var dLon = deg2rad(parseFloat(lon2) - parseFloat(lon1));

        var a =
          Math.sin(dLat / 2) * Math.sin(dLat / 2) +
          Math.cos(deg2rad(parseFloat(lat1))) * Math.cos(deg2rad(parseFloat(lat2))) *
          Math.sin(dLon / 2) * Math.sin(dLon / 2);

        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        var d = R * c; // Distance in km
        return d;
    }

    function deg2rad(deg) {
        return deg * (Math.PI / 180)
    }

    function calcRoute(lat1, lon1, lat2, lon2) {
        var start = new google.maps.LatLng(parseFloat((lat1)), parseFloat(lon1));


        var end = new google.maps.LatLng(parseFloat(lat2), parseFloat(lon2));

        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {

            if (status == google.maps.DirectionsStatus.OK) {

                directionsDisplay.setDirections(response);

            }

        });
    };

</script>
    <form id="loctracer" runat="server">
        <div><asp:Label runat="server" ID="Label2" Font-Bold="true" Font-Size="Large" BackColor="#0000ff" ForeColor="White"> RADIANT TECHNOLOGIES</asp:Label></div>
        <div id="dvMap" style="width: 500px; height: 500px">
        </div>
        <div><asp:Label runat="server" ID="Label1" Font-Bold="true" Font-Size="Large">Distance Between Your Location And India Gate Is :</asp:Label></div>
        <div><asp:Label runat="server" ID="lbl1" Font-Bold="true" Font-Size="Large"> </asp:Label></div>

    </form>

</body>
</html>