动态创建Google Maps地址自动完成

发布于 2025-02-06 03:47:16 字数 1670 浏览 2 评论 0原文

这是我的用例:

我想让用户通过按钮创建动态地址输入。我希望那些动态创建的地址输入以使Google Maps AutoComplete具有。我设法创建了一个自动完成地址输入。尽管输入HTML标签已经在DOM中。我正在为此项目和 @Angular/Google-Maps NPM软件包使用Angular 11。

这是我到目前为止正在做的事情,我似乎无法使事情起作用。输入字段是正确创建和删除的,但似乎没有应用自动完成。

任何帮助将不胜感激:)

 ngAfterViewInit(): void {
    this.addAddress();
  }

  public addAddress(): void {
    const newInput = document.createElement('input');
    newInput.id = `${this.inputCounter}`;
    this.inputCounter++;
    this.HTMLInputElementArray.push(newInput);
    for (const inputText of this.HTMLInputElementArray) {
      const autocomplete = new google.maps.places.Autocomplete(inputText, {
        componentRestrictions: { country: 'CA' },
        types: ['geocode'],
      });
      google.maps.event.addListener(autocomplete, 'place_changed', () => {
        const place = autocomplete.getPlace();
        this.invokeEvent(place);
      });
    }
  }

  public invokeEvent(place: object): void {
    this.setAddress.emit(place);
  }

  public removeAddress(): void {
    this.HTMLInputElementArray.pop();
    if (this.inputCounter > 0) {
      this.inputCounter--;
    }
  }
<div>
  <button style="margin-top: 10px" (click)="addAddress()">Add address</button>
  <button style="margin-top: 10px" (click)="removeAddress()">
    Remove address
  </button>
</div>

<div *ngFor="let input of HTMLInputElementArray; let i = index">
  <input type="text" [id]="input.id" class="query" />
</div>

This is my use case :

I want to let a user to create dynamically addresses input through a button. I want those dynamically created addresses input to have google maps autocomplete. I've managed to create a single autocomplete address input. Although the input html tag was already in the DOM. I'm using Angular 11 for this project and @angular/google-maps npm package.

This is what I'm doing so far and I can't seem make things work. The input field are correctly created and removed, but the autocomplete doesn't seem to be applied.

Any help would be much appreciated :)

 ngAfterViewInit(): void {
    this.addAddress();
  }

  public addAddress(): void {
    const newInput = document.createElement('input');
    newInput.id = `${this.inputCounter}`;
    this.inputCounter++;
    this.HTMLInputElementArray.push(newInput);
    for (const inputText of this.HTMLInputElementArray) {
      const autocomplete = new google.maps.places.Autocomplete(inputText, {
        componentRestrictions: { country: 'CA' },
        types: ['geocode'],
      });
      google.maps.event.addListener(autocomplete, 'place_changed', () => {
        const place = autocomplete.getPlace();
        this.invokeEvent(place);
      });
    }
  }

  public invokeEvent(place: object): void {
    this.setAddress.emit(place);
  }

  public removeAddress(): void {
    this.HTMLInputElementArray.pop();
    if (this.inputCounter > 0) {
      this.inputCounter--;
    }
  }
<div>
  <button style="margin-top: 10px" (click)="addAddress()">Add address</button>
  <button style="margin-top: 10px" (click)="removeAddress()">
    Remove address
  </button>
</div>

<div *ngFor="let input of HTMLInputElementArray; let i = index">
  <input type="text" [id]="input.id" class="query" />
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文