如何使用Google Maps Route API分配ID

时间:2018-09-17 13:48:07

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

我正在使用Google Maps Route Direction Service开发一个应用程序,该功能( directionsService.route )如何以异步方式执行,如何可以分配ID( var id = this)。 rutas [i] .id )绘制到的每条路线。 这是我的代码:

directionsService = new google.maps.DirectionsService();
    this.rutaService.obtenerRutas().then(response => {
      this.rutas = response;
      for(var i=0;i<this.rutas.length;i++){
          var routeData= this.rutas[i].routeData;
          var str = routeData;
          var obj=JSON.parse(str);
          originLng = obj.origin.originLng;
          originLat = obj.origin.originLat;
          destinationLat = obj.destination.destinationLat;
          destinationLng = obj.destination.destinationLng;

          var id= this.rutas[i].id;//Route ID

          if(obj.waypoints.length>0){

             for(var j=0;j<obj.waypoints.length;j++){

               waypts.push({
                   location: new google.maps.LatLng(obj.waypoints[j][0], obj.waypoints[j][1]),
                   stopover: true
               });
             }
             req = {
                 origin:new google.maps.LatLng(originLat, originLng),
                 destination: new google.maps.LatLng(destinationLat, destinationLng),
                 waypoints: waypts,
                 travelMode: google.maps.DirectionsTravelMode.DRIVING
              };

              waypts = [];
          }else{
             req = {
                 origin:new google.maps.LatLng(originLat, originLng),
                 destination: new google.maps.LatLng(destinationLat, destinationLng),
                 travelMode: google.maps.DirectionsTravelMode.DRIVING
              };
           }
          directionsService.route(req, function(result, status) {
                console.log(status);
                if (status === google.maps.DirectionsStatus.OK) {
                    var col = '#' + (( 1 << 24) * Math.random() | 0).toString(16);
                    var pinImage = new google.maps.MarkerImage('http://www.googlemapsmarkers.com/v1/27893e/');
                    originMarker = new google.maps.Marker({
                      position: req.origin,
                      icon: pinImage
                    });
                    originMarkers[cur] = originMarker;
                    originMarkers[cur].setMap(map);
                    pinImage = new google.maps.MarkerImage('http://www.googlemapsmarkers.com/v1/7d2181/');
                    destinationMarker = new google.maps.Marker({
                      position: req.destination,
                      icon: pinImage
                    });
                    destinationMarkers[cur] = destinationMarker;
                    destinationMarkers[cur].setMap(map);
                    var color = col;
                    directionsDisplay = new google.maps.DirectionsRenderer({
                      polylineOptions: {
                        strokeColor: color
                      }
                    });
                    renderArray[cur] = directionsDisplay;
                    renderArray[cur].setOptions({suppressMarkers: true});
                    renderArray[cur].setMap(map);
                    renderArray[cur].setDirections(result);
                    cur++;

                }
            });
            map.setZoom(10);
            map.setCenter(new google.maps.LatLng(originLat, originLng));
       }
    });

为什么我要绘制的某些路线的状态为“ OVER_QUERY_LIMIT”?任何帮助表示赞赏。

0 个答案:

没有答案