Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
JavaScript
<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>
Posted
Updated 13-May-14 12:24pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900