【问题标题】:Google Maps direction service does not show谷歌地图方向服务不显示
【发布时间】:2016-09-18 00:17:33
【问题描述】:

我有以下代码来显示从一个点到另一个点的方向。我已经按照用户@user2314737http://stackoverflow.com/questions/5959788/google-maps-api-v3-how-show-the-direction-from-a-point-a-to-point-b-blue-line这篇帖子提供的代码。

我根据我的知识稍微更改了代码。但是我的地图上没有显示方向;我做错了什么?

这里是代码;

        <script>
        function initialize(){
            var latlng = new google.maps.LatLng(6.242635,80.051840); //latitude and longitude respectively, pointA
            var pointB = new google.maps.LatLng(6.237038,80.054709);

            var contentString = '<h1> Volunteer House </h1>'+
                '<p>Many people also refer to this home as <b>VH</b>. This is a must see place in the city.'+ 
                    '<br><br>Direction - Pass the <i>balangoda Children Park</i> and head along the <i>K Road</i>. '+
                    'Turn to left at the second turn and walk just 25m.</p>'+
                    '<br>Address - K Road, balangoda, Sri Lanka'+
                    '<br>Website: Volunteer House, <a href="http://r.comlu.com" target="_blank">'+
                    'http://r.comlu.com</a>';

            var infowindow = new google.maps.InfoWindow({
                content: contentString
            });             

            var mapProp = {
                center:latlng, //or you can pointB
                zoom:15, //4 - displays in world map zoom
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);


            //------------------------------ Multiple markers using loops
            var places = [
                ['balangoda Children Park', 6.241713, 80.052307, 2], //2 is the zIndex - order in which these markers should display on top of each other
                ['My Home', 6.242693, 80.051855, 1]
            ];

            var images = ['children.ico', 'office2.ico'];


            var myMarker = new Array(); //store marker in array

            for( var i=0; i<places.length; i++ ){
                    var place = places[i];
                    myMarker[i] = new google.maps.Marker({
                            position: {lat: place[1], lng:place[2]},
                            map: map,
                            title: place[0],
                            icon: images[i],                                
                            zIndex: place[3]
                    });

                    if( i == 1 ){
                            myMarker[1].setAnimation(google.maps.Animation.BOUNCE);

                            myMarker[1].addListener('click', function(){ 
                                infowindow.open(map, myMarker[1]); 
                            })
                    }
            };

            //---------------------------------------------------------------------

            /*------------------------------ show direction from point A to B using Google Maps API Direction Service
               DirectionService - object to send and receive direction requests
               DirectionRenderer - object to render the returned route on the map */

            directionsService = new google.maps.DirectionsService;
            directionsDisplay = new google.maps.DirectionsRenderer({
                    map: map
            });

            //get route from A to B
            calculateAndDisplayRoute( directiionsService, directionsDisplay, latlng, pointB );

            //---------------------------------------------------------------------
        }

        function calculateAndDisplayRoute( dS, dD, pA, pB){
                dS.route(
                        { //1
                            origin: pA,
                            destination: pB,
                            travelMode: google.maps.TravelMode.DRIVING
                        },//1
                        function(response, status){ //2
                            if(status==google.maps.DirectionsStatus.OK){
                                dD.setDirections(response);
                            }
                            else{
                                window.alert('Directions request failed due to ' + status);
                            }
                        } //2
                );
        }


        google.maps.event.addDomListener(window, 'load', initialize)

    </script>

【问题讨论】:

  • 在加载页面并运行代码时先检查浏览器控制台是否有错误
  • 原始的、未经编辑的代码对您有用吗?如果是这样,你做了什么改变?另外,这与 PHP 有什么关系?您提供的所有代码都是 Javascript。
  • 不要评论上一个问题.. 在这里评论否则不可能关注你
  • 如果它在您的更改之前有效,但在更改之后无效,那么您应该在问题中记录您的更改。不管你认为它们有多小,它们的某些东西都会造成问题。
  • 我已经发布了一个答案希望有用...

标签: javascript google-maps


【解决方案1】:

你的错误是directionsService而不是directiionsService

calculateAndDisplayRoute( directionsService, directionsDisplay, latlng, pointB );

【讨论】:

  • 当我以该错误运行代码时,我在网页和萤火虫 (F12) 上都没有看到任何错误。 (程序的其余部分显示了除方向以外的地图)。与 PHP 不同,Javascript 默认不显示语法错误、未定义变量吗?有什么办法检查吗?
  • 您使用哪种浏览器..?
  • 我使用谷歌浏览器。
  • 我不知道 chrome 中的错误控制台在哪里。我有时使用带有 firebugs 的 Firefox,firefox 开发者版和带有开发者工具的 Opera ......并且这些浏览器在控制台中显示 aklway javascrit 结果(包括错误......)
猜你喜欢
  • 1970-01-01
  • 2015-08-22
  • 2017-02-23
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多