计算用户位置与转发器中DB的纬度逻辑之间的距离 [英] calculate distance between user location and latitude logitude from DB in repeater

查看:78
本文介绍了计算用户位置与转发器中DB的纬度逻辑之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示转发器标签中的总距离。我正在使用转发器从sql数据库显示我的记录。我在数据库中有纬度,经度列。我希望当我的结果页面加载时,它会要求用户分享他们当前的位置,当用户接受它时,然后计算用户当前位置和纬度/经度之间的距离。对于一个记录来说,这很容易,但是在转发器中有很多记录,我想它会计算所有结果的距离,然后显示回转发器标签。例如,在justdial网站上,他们显示距离的结果。这样用户就可以很容易地看到哪个广告离他的位置很近。怎么做...

我的代码:

I am trying to display total distance in the repeater label. I am using repeater to display my records from sql database. I have latitude, longitude column in the database. I want when my result page loads it will ask for user to share their current location, and when user accept it then calculate distance between user current location and latitude/longitude. For one record it's very easy to do but in repeater there are so many records, and i want it will calculate distance for all results and then show back to the repeater label. for example in justdial website they showing result with distance. So that user can easily see which Ad is near from his location. How to do it...
My code:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
    <script type="text/javascript">
        var map;
        function initialize() {
            // Try HTML5 geolocation
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    document.getElementById('<%= Label1.ClientID %>').innerHTML = pos;
                    calcRoute();
                }, function () {
                    handleNoGeolocation(true);
                });
            } else {
                // Browser doesn't support Geolocation
                handleNoGeolocation(false);
            }
            
        }
        
        function handleNoGeolocation(errorFlag) {
            if (errorFlag) {
                var content = 'Error: The Geolocation service failed.';
            } else {
                var content = 'Error: Your browser doesn\'t support geolocation.';  //' //[edit] fixing code color
            }
        }

        function calcRoute() {
            var start = pos;
            var end = "This is not fixed, for each record it should take its latitude/longitude value";
            var request = {
                origin: start,
                destination: end,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };
            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                    computeTotalDistance(response);
                }
            });
        }

        function computeTotalDistance(result) {
            var total = 0;
            var myroute = result.routes[0];
            for (i = 0; i < myroute.legs.length; i++) {
                total += myroute.legs[i].distance.value;
            }
            total = total / 1000;
// here i have to show result on label inside repeater, but it giving error: "The name 'lbl_distance' does not exist in the current context" but i have label with this id inside the repeater.
            document.getElementById('<%= lbl_distance.ClientID %>').innerHTML = total + " km";
            
        }

        google.maps.event.addDomListener(window, 'load', initialize);
        
    </script>

推荐答案

这篇关于计算用户位置与转发器中DB的纬度逻辑之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆