Google Maps Directions服务不显示 [英] Google Maps direction service does not show

查看:82
本文介绍了Google Maps Directions服务不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code to show direction form one point to another. I have followed the code provided by this post http://stackoverflow.com/questions/5959788/google-maps-api-v3-how-show-the-direction-from-a-point-a-to-point-b-blue-lineby the user @user2314737.

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

I changed the code little bit according to my knowledge. But the directions are not shown on my map; What am I doing wrong?

这是代码;

        <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>

推荐答案

您遇到的错误是DirectionsService,而不是directiionsService

You have a mistake is directionsService and not directiionsService

calculateAndDisplayRoute( directionsService, directionsDisplay, latlng, pointB );

这篇关于Google Maps Directions服务不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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