Add dynamic line using angular2 google maps

5.5k Views Asked by At

I am currently using https://github.com/SebastianM/angular2-google-maps Angular Google Maps link attached, my requirement is to draw dynamic line using agm-polyline when user clicks the button it places a point on map with latitude and longitude, now i want to draw a path using the same linee: my sample is

app.html

        <button (click)="add()">Add Line</button>
<agm-map [latitude]="lat" [longitude]="lng" [panControl]="true" [usePanning]="true" [rotateControl]="true">
        <agm-marker [latitude]="lat" [longitude]="lng">
        </agm-marker>
      </agm-marker>
              <agm-polyline *ngFor="let data of lines" [editable]="true">
                            <agm-polyline-point
                              [latitude]="data.lat"
                              [longitude]="data.lng">
                            </agm-polyline-point>
              </agm-polyline>
    </agm-map>

app.component.ts

 add() {
console.log('on shape')
this.lines.push({lat: 51.79, lng: 7.8});//creates a point now want to add draw path 

}

it creates a point now using this point i want to draw a line any help will be greatly appreciated

1

There are 1 best solutions below

0
On

You need to place your *ngFor="let data of lines" inside agm-polyline-point. Like this:-

<agm-polyline [editable]="true">
    <agm-polyline-point *ngFor="let data of lines" 
        [latitude]="data.lat"
        [longitude]="data.lng">
    </agm-polyline-point>
</agm-polyline>