简体   繁体   English

Google Maps API:绘制路线时归零或无效请求

[英]Google Maps API: ZERO RESULTS or INVALID REQUEST while drawing routes

I am trying to draw a route based on the co-ordinates that i receive from the database. 我正在尝试根据从数据库中接收到的坐标绘制路线。 But somehow i am not able to draw. 但是我不知怎么画。 I either get ZERO RESULTS or INVALID REQUEST .. The route i am trying to draw is a TRANSIT ROUTE. 我要么得到ZERO RESULTS要么得到INVALID REQUEST 。我要绘制的路线是TRANSIT路线。 Where as i found some similar issues( Link ) being addressed in the site but the solution given was to add departure time or arrival time in the parameters. 当我在站点中发现一些类似的问题( 链接 )时,但给出的解决方案是在参数中添加departure timearrival time And it was an accepted answer. 这是一个公认的答案。 The same which i tried is not working currently. 我尝试过的相同功能目前无法正常工作。 I have posted the code that i have tried. 我已经发布了我尝试过的代码。 Below are the co-ordinates: 以下是坐标:

19.1860640243063 72.9759523272514
19.1902699 73.023094
19.2178474133021 73.086293040406
19.2354157727173 73.1302742969937

Please help. 请帮忙。

 load : function(response) 
        {
            for(var i=0;i<response.length;i++)
            {
                if(response[i].linkData!='undefined')
                {
                 link=response[i].linkData;
                 var lastPos=(response.length-1);
                 linkDes=response[lastPos].linkData;    
                 var linkDes=link.split(" ");       
                 var linkValue=link.split(" ");                                          
                 var latDes= parseFloat(linkDes[0]);                
                 var longDes= parseFloat(linkDes[1]);           
                 var lat = parseFloat(linkValue[0]);  //convert string to float                 
                 var lon = parseFloat(linkValue[1]);  //convert string to float             
                 if(count==0)
                 {   
                   var source=new google.maps.LatLng(lat, lon); 
                   count++;
                 }  
                 if(i!=0 )  
                 {
                     geoLatLong=new google.maps.LatLng(lat, lon); 
                      count++;
                 }
                 if(i!=response.length-1)
                 {
                     geoLatLong=new google.maps.LatLng(lat, lon);   
                 }
                 if(latDes!="" && longDes!="")
                  {         
                     var destination=new google.maps.LatLng(latDes, longDes); 
                  }
                  if(count>1 && count<=response.length-1) 
                  {
                   geoLatLongArray.push(geoLatLong);   
                  }

                }
            }
            for(var i=0;i<geoLatLongArray.length;i++)
            {
              waypts.push({location:geoLatLongArray[i],stopover:true}); 
            }
            var request = {
                    origin: source,
                    destination: destination,
                    waypoints: waypts,
                   optimizeWaypoints: true,

                    travelMode: google.maps.DirectionsTravelMode.TRANSIT,
                    transitOptions: 
                    {
                     departureTime: new Date()   
                    }
                };
                directionsService.route(request, function(response, status) {
                  if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                  } else {
                    alert("directions response "+status);
                  }
                });
        }

According to v3 API Docs 根据v3 API文档

google.maps.TravelMode.TRANSIT

is the correct syntax. 是正确的语法。

I had similar issue. 我有类似的问题。 I fixed it after making origin to an address field (instead of coordinates). 我将原点定为地址字段(而不是坐标)后将其修复。 this issue persists only when both origin and destination are coordinates. 仅当起点和终点均为坐标时,此问题仍然存在。

You can perhaps convert your coordinates into address using Reverse Geocode API and then pass the converted addresses to origin / destination. 您也许可以使用反向地理编码API将坐标转换为地址,然后将转换后的地址传递到起点/终点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM