简体   繁体   English

Google地图,在8个以上的航路点之间绘制线

[英]Google Maps, draw lines between more than 8 waypoints

We have json. 我们有json。 In json have 10 latitude and 10 longitude.We tried draw line between those points.But We got Status code google.maps.DirectionsResult.MAX_WAYPOINTS_EXCEEDED .So after deleted two points.Now We tried to draw the lines between 8 points it's working nice. 在json中有10个纬度和10个经度。我们尝试在这些点之间画线。但是我们得到了状态码google.maps.DirectionsResult.MAX_WAYPOINTS_EXCEEDED 。因此删除了两点后,现在我们试图在8点之间画线很不错。 But in json some time come 50 to 100 latitude and longitude.We tried like this 但是在json中,有些时候会出现50至100的经度和纬度。

 var aa=waypts[0].location.k;
  var bb=waypts[0].location.D;

  var cc=waypts[waypts.length-1].location.k
  var dd=waypts[waypts.length-1].location.D;

var start1= new google.maps.LatLng(aa,bb);
var end1=new google.maps.LatLng(cc, dd);

  var request = {
      origin: start1,
      destination: end1,
      waypoints: waypts,
      optimizeWaypoints: true,
      travelMode: google.maps.TravelMode.DRIVING
  };
  debugger;

  directionsService.route(request, function(response, status) {
  debugger;
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      var route = response.routes[0];
    }
  });

first tell me how to draw line between more then 8 points.Please guide me 首先告诉我如何在8点之间画线。请指导我

Short answer not in a single request. 简短的回答不在一个请求中。

The documentation for Google Maps Directions states that Google Maps Directions的文档指出:

MAX_WAYPOINTS_EXCEEDED indicates that too many waypoints were provided in the request The maximum allowed waypoints is 8, plus the origin, and destination. MAX_WAYPOINTS_EXCEEDED表示请求中提供的路标太多。允许的最大路标为8,加上起点和终点。 ( Google Maps API for Work customers may contain requests with up to 23 waypoints.) Google Maps API for Work客户可能包含最多23个航点的请求。)

Under the header of Usage Limits 在“使用限制”标题下

Users of the free API get Up to 8 waypoints (plus origin and destination) allowed in each request and a total of 2,500 directions requests per 24 hour period. 使用免费API的用户在每个请求中最多可以获取8个航路点(加上起点和终点),并且每24小时内总共有2500个路线请求。

So if you run multiple requests, fetching 8 waypoints (plus origin and destination) at a time you can achieve this. 因此,如果您运行多个请求,一次获取8个航点(加上起点和终点)就可以实现。 Just note the maximum of 2,500 directions requests per 24 hours. 只需注意每24小时最多2500个路线请求。

You should use multiple requests as the other answer suggests. 其他答案建议您使用多个请求。 Here is an example: 这是一个例子:

var service = new maps.DirectionsService();
function calcRoute(points, startIndex) {

    var start = points[startIndex];
    var destinationIndex = startIndex + 8 > points.length - 1 ? points.length - 1 : startIndex + 8;
    var destination = points[destinationIndex];

    function handlerRouteResult(result, status) {

        var path = [];
        if (status === maps.DirectionsStatus.OVER_QUERY_LIMIT) {
            setTimeout(function() {
                calcRoute(points, startIndex);
            }, 100);
        }
        else if (status === maps.DirectionsStatus.OK) {
            var overviewPath = result.routes[0].overview_path;
            for (var routePointIndex = 0; routePointIndex < overviewPath.length; routePointIndex++) {
                path.push(overviewPath[routePointIndex]);
            }
        } else {
            for (var pointIndex = startIndex; pointIndex <= destinationIndex; pointIndex++) {
                path.push(points[pointIndex]);
            }
        }

        var line = new maps.Polyline({
            map: map,
            strokeColor: '#235c23',
            path: path
        });
    }

    var waypoints = [];
    for (var waypointIndex = startIndex; waypointIndex < destinationIndex - 1; waypointIndex++) {
        waypoints.push({ location: points[waypointIndex] });
    }

    service.route({
        origin: start,
        destination: destination,
        travelMode: maps.DirectionsTravelMode.DRIVING,
        waypoints: waypoints
    }, handlerRouteResult);
}

for (var i = 0; i < pathPoints.length - 1; i += 8) {
    calcRoute(pathPoints, i);
}

It also take the OVER_QUERY_LIMIT response in account that you receive you make to many API calls. 考虑到您收到的对许多API调用的响应,它还会考虑OVER_QUERY_LIMIT响应。 In this case it will retry the route query. 在这种情况下,它将重试路由查询。

As others said, it's imposable to do it using Google's JavaScript API. 就像其他人所说的那样,使用Google的JavaScript API不可能做到这一点。 However, Google does allow up to 23 waypoints with a server-side request. 但是,Google的服务器端请求最多允许23个路标。 So using PHP and JavaScript, I was able to make a workaround. 因此,使用PHP和JavaScript,我可以做出解决方法。

I explained it here along with the code. 在这里解释了代码。

What you have to do is get the "waypoint order" from the server side request and then have JavaScript give you directions between each location. 您要做的就是从服务器端请求中获取“航点顺序”,然后让JavaScript为您提供每个位置之间的路线。

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

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