Невозможно получить данные за пределами DirectionsService - Angular 2+ - Google Maps - PullRequest
0 голосов
/ 06 сентября 2018

У меня есть список адресов. Я отправляю эти адреса в Google, и они возвращают мне маршрут, связанный с дорогой, чтобы я мог нарисовать ломаную линию от одного адреса к другому. Проблема заключается в том, что эти данные можно получить за пределами сайта directionsService, предоставляемого Google. Когда я устанавливаю данные в глобальную переменную и пытаюсь вызвать их вне функции directionsService, они возвращаются как нулевые. Я не могу получить данные за пределами этой функции. В это я верю, поэтому я не могу просматривать полилинии.

Если у меня есть массив по умолчанию lat и lngs, я могу просматривать полилинии. Только не тогда, когда я пытаюсь воспользоваться услугой. То, что я пытаюсь назвать, это this.routepath.

polylines.ts

displayRoute(routeLocations){
      this.waypoints = []
      google.maps.LatLng(parseFloat(item.latitude),parseFloat(item.longitude)));

        for (var i = 0; i < this.routeLocations.stops.length; i++) {
          this.waypoints.push({

            location : this.routeLocations.stops[i].address1 + " "  + this.routeLocations.stops[i].city + ", "  + this.routeLocations.stops[i].state,
            stopover : true
          });
        }

      this.firststopRec = routeLocations.stops.filter(item => {
        return item.locationId;
      })[0];

      this.laststopRec = routeLocations.stops[routeLocations.stops.length - 1];

      var directionsService = new google.maps.DirectionsService;
      var directionsDisplay = new google.maps.DirectionsRenderer;
      //Call polylines function 
      this.polylines = this.calculateAndDisplayRoute(directionsService, directionsDisplay, this.waypoints,this.firststopRec,this.laststopRec, routeLocations);

    }


 private calculateAndDisplayRoute(directionsService, directionsDisplay, waypoints, firststopRec,laststopRec, routeLocations){


     var frtLatlng = new google.maps.LatLng(parseFloat(firststopRec.latitude),parseFloat(firststopRec.longitude));
     var lstLatlng = new google.maps.LatLng(parseFloat(laststopRec.latitude),parseFloat(laststopRec.longitude));

    console.log(this.routeLocations.stops, 'logging way points')

      var request = {
        origin: frtLatlng,
        destination: lstLatlng,
        waypoints: waypoints,
        optimizeWaypoints: true,
        travelMode: google.maps.TravelMode.DRIVING
      };

      directionsService.route(request, function(response, status)
      {
        console.log(status, request, response, 'logigng direction service col')
        if (status == google.maps.DirectionsStatus.OK) 
        {
          directionsDisplay.setDirections(response);
          //var route = response.routes[0];

          this.routepath = response.routes[0].overview_path.map(function (location) {
            return {latitude: location.lat(), longitude: location.lng()};
          });


        }
      }) 

}
...