谷歌地图的javascript方向

时间:2017-04-19 23:41:04

标签: javascript google-maps google-maps-api-3

如何将start = $("#from").val();,值选项更改为用户当前位置?没有输入开始地址就更舒服了。

任何帮助?

<script type="text/javascript">

        $(document).on("pageinit", "#map_page", function() {
            initialize();
        });

        $(document).on('click', '#submit', function(e) {
            e.preventDefault();
            calculateRoute();
        });

        var directionDisplay,
            directionsService = new google.maps.DirectionsService(),
            map;

        function initialize() 
        {   


            directionsDisplay = new google.maps.DirectionsRenderer();
            directionsService = new google.maps.DirectionsService();
            var mapCenter = new google.maps.LatLng(51.507009, -0.170720);

            var myOptions = {
                zoom:16,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: mapCenter
            }

            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            directionsDisplay.setMap(map);
            directionsDisplay.setPanel(document.getElementById("directions"));  
        }





        function calculateRoute() 
        {
            var selectedMode = $("#mode").val(),
                start = $("#from").val();,
                end = new google.maps.LatLng(51.507009, -0.170720);
                //start = $("#from").val();

            if(start == '' || end == '')
            {
                // cannot calculate route
                $("#results").hide();
                return;
            }
            else
            {
                var request = {
                    origin:start, 
                    destination:end,
                    travelMode: google.maps.DirectionsTravelMode[selectedMode]
                };

                directionsService.route(request, function(response, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setDirections(response); 
                        $("#results").show();
                        /*
                            var myRoute = response.routes[0].legs[0];
                            for (var i = 0; i < myRoute.steps.length; i++) {
                                alert(myRoute.steps[i].instructions);
                            }
                        */
                    }
                    else {
                        $("#results").hide();
                    }
                });

            }
        }
    </script>

1 个答案:

答案 0 :(得分:0)

解决方案是使用https而不是http getCurrentPosition() and watchPosition()