【问题标题】:Angular / google map not drawing polyline between path points角度/谷歌地图未在路径点之间绘制折线
【发布时间】:2018-08-29 18:37:58
【问题描述】:

我对 Angular 很陌生,问题是我无法让 agm-polyline 在我的地图上绘制路径。我的数据正在填充我的对象数组,但之后没有执行任何操作。提前感谢您的帮助。

<agm-map [latitude]="lat" [fitBounds]="bounds" [longitude]="lng" [zoom]="defaultZoom">
 <agm-polygon >
    <ng-container>
        <agm-polyline *ngFor="let polyline of polylines;let i = index;"  [strokeColor]="polyline.color" [visible]="true" [zIndex]="1">
           <agm-polyline-point *ngFor="let pint of polylines.path" [latitude]="pint.lat" [longitude]="pint.lng">
           </agm-polyline-point>
       </agm-polyline>
    </ng-container>
    </agm-polygon>
   <sebm-google-map-directions [origin]="origin" [destination]="destination" [travelMode]="travelMode"></sebm-google-map-directions>
  </agm-map>

 this.polylines = this.calculateAndDisplayRoute(directionsService, directionsDisplay,this.waypoints,this.firststopRec,this.laststopRec);


   private calculateAndDisplayRoute(directionsService, directionsDisplay,waypoints,firststopRec,laststopRec){
    var waypnts = [];
    var pts = [];
    this.maxSpeed = 40;
 var getpnts = this.waypoints.map(function(item){
  pts.push({
    latlng: new google.maps.LatLng(parseFloat(item.latitude),parseFloat(item.longitude))
  });
  return pts ;
});
let pnts = pts;
console.log("pnts: "+ JSON.stringify(pnts));
var newPnts = [];
for (let pnts of pts) {
  newPnts.push(pnts.latlng);
}

   var frtLatlng = new google.maps.LatLng(parseFloat(firststopRec.latitude),parseFloat(firststopRec.longitude));
   var lstLatlng = new google.maps.LatLng(parseFloat(laststopRec.latitude),parseFloat(laststopRec.longitude));
    var request = {
      origin: frtLatlng,
      destination: lstLatlng,
      waypoints: waypnts,
      optimizeWaypoints: true,
      travelMode: google.maps.TravelMode.DRIVING
    };
   // console.log("request: {request}: "+ JSON.stringify(request));
    directionsService.route(request, function(response, status) 
    {
      if (status == google.maps.DirectionsStatus.OK) 
      {
        directionsDisplay.setDirections(response);
        //var route = response.routes[0];
        var routewypt = response.routes[0].legs;

        let polylines = [];
        let i = 0;
        let newPolyline = {path: [], color: 'red', strokeWeight: 3};

        for (let point of newPnts) {
          newPolyline.path.push(point);

            newPolyline.path.push( point[i+1] );
            polylines.push(newPolyline);
            newPolyline = {path: [], color: 'red', strokeWeight: 3};
          i++;
        }
        let pint = polylines;
        console.log('pint: ' + JSON.stringify(pint));
        console.log('polylines: ' + JSON.stringify(polylines));
       return polylines;

      }
    }); 
  }

我的数据在那里,但折线路径未在地图上绘制

[{"path":[{"lat":39.1876259,"lng":-96.58321000000001},null],"color":"red","strokeWeight":3},....]

【问题讨论】:

  • 您的意思是数据没有反映在视图中?
  • 正确,我看不出有什么问题
  • 您是否遇到任何错误
  • 完全没有错误
  • 我认为你是变量是局部范围尝试全局定义变量

标签: javascript angular google-maps angular-ui-router google-polyline


【解决方案1】:

未检测到更改,因为它发生在异步谷歌方向回调中的角度框架之外。确保在构造函数中包含更改检测器,并且仅在分配值之后调用更改检测器。使用 lambda 来维护 google 方向回调中的范围。

强制变化检测

constructor(private ref: ChangeDetectorRef){}

...

this.calculateAndDisplayRoute(directionsService, directionsDisplay, this.waypoints, this.firststopRec, this.laststopRec);


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

    ...

    //USE LAMBDA HERE TO MAINTAIN SCOPE
    directionsService.route(request, (response, status) => {
        if (status == google.maps.DirectionsStatus.OK) {

            ...

            this.polylines = polylines;
            this.ref.detectChanges();
        }
    });
}
...

【讨论】:

  • 未捕获的类型错误:无法读取未定义的属性“detectChanges”。我已将其导入并放置在构造函数中
  • 你能分享完整的类,包括构造函数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 2017-10-03
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
  • 1970-01-01
相关资源
最近更新 更多