Google地方信息自动填充+ Angular2 [英] Google Places Autocomplete + Angular2

查看:112
本文介绍了Google地方信息自动填充+ Angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的Google Places Autocomplete Angular2指令,但问题是无法获得任何预测(响应始终为空?!)...

I am building a simple Google Places Autocomplete Angular2 directive but the problem is that cannot get any prediction (the response is always empty?!)...

作为概念验证,我创建了最简单的代码段作为参考:

As a proof of concept I created the simplest possible code snippet as a reference:

<!DOCTYPE html>
<html>

<head>
    <script src="https://maps.googleapis.com/maps/api/js?key=[MY_API_KEY]&libraries=places" async defer></script>
</head>

<body>
    <button type="button" onclick="go()">go</button>
    <input id="autocomplete" type="text"></input>
    <script>
    var autocomplete = null;

    function go() {
        autocomplete = new google.maps.places.Autocomplete(
            (document.getElementById('autocomplete')), {
                types: ['geocode']
            });
    }
    </script>
</body>

</html>

上面的代码有效-单击运行"按钮后,我可以得到预测.

The above code works - I can get predictions after clicking Go button.

现在,Angular2场景:

Now, Angular2 scenario:

我的_Layout.cshtml文件的头部具有以下标记:

My _Layout.cshtml file has the following tag in head section:

<script src="https://maps.googleapis.com/maps/api/js?key=[MY_API_KEY]&libraries=places" async defer></script>

我的指令:


import { Directive, ElementRef, Input } from '@angular/core';

declare var google: any;

@Directive({ selector: '[googleplaces]' })
export class GooglePlacesDirective {

    autocomplete: any;

    constructor(private el: ElementRef) {

    }

    ngAfterContentInit() {
        this.autocomplete = new google.maps.places.Autocomplete(
            (this.el.nativeElement),
            { types: ['geocode', 'cities'] });
    }
}

简单的Angular组件:

And simple Angular component:

<form [formGroup]="companyForm">
   
    .
    .
    .

    <div class="form-group">
        <label for="Location">Location</label>
        <input type="text" 
               class="form-control" 
               id="Location" 
               fromControlName="Location" 
               googleplaces>
    </div>
</form>

方案2(角度)不起作用.事实:

The Scenario 2 (angular) doesn't work. The facts:

  • 初始化自动完成功能(它具有所有预期的属性/方法,占位符是输入位置",等等...)
  • 自动完成功能不返回任何类型化搜索字符串的预测(它仅返回"/**/ xdc ._ le3zv3&& xdc ._ le3zv3([ 4])")
  • autocomplete is initialized (it has all expected properties/methods, placeholder is "Enter a location", etc...)
  • autocomplete doesn't return any prediction for typed search string (it returns only "/**/xdc._le3zv3 && xdc._le3zv3( [4] )")

此外,Google API控制台会说一切都应该保持原样?!这是屏幕截图:

Also, Google API Console says everything is as it should be?! Here is the screenshot:

这里可能有什么问题?谢谢...

What can be in question here? Thanks...

推荐答案

import {Directive, ElementRef, EventEmitter, Output} from '@angular/core';
import {NgModel} from '@angular/forms';

declare var google:any;

@Directive({
  selector: '[Googleplace]',
  providers: [NgModel],
  host: {
    '(input)' : 'onInputChange()'
  }
})
export class GoogleplaceDirective {

   @Output() setAddress: EventEmitter<any> = new EventEmitter();
  modelValue:any;
  autocomplete:any;
  private _el:HTMLElement;


  constructor(el: ElementRef,private model:NgModel) {
    this._el = el.nativeElement;
    this.modelValue = this.model;
    var input = this._el;

    this.autocomplete = new google.maps.places.Autocomplete(input, {});
    google.maps.event.addListener(this.autocomplete, 'place_changed', ()=> {
      var place = this.autocomplete.getPlace();
      this.invokeEvent(place);

    });
  }

  invokeEvent(place:Object) {
    this.setAddress.emit(place);
  }

  onInputChange() {
    console.log(this.model);
  }
}

要使用

<input type="text" class="form-control" placeholder="Location" name="Location" [(ngModel)]="address" #LocationCtrl="ngModel"
        Googleplace (setAddress)="getAddressOnChange($event,LocationCtrl)">

这篇关于Google地方信息自动填充+ Angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆