ngx-google-place-autocomplete
Version:
* A simple and lightweight google place autocomplete directive for Angular2
135 lines (128 loc) • 4.84 kB
JavaScript
import { Injectable, Directive, ElementRef, Output, EventEmitter, NgZone, NgModule, defineInjectable } from '@angular/core';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgxGooglePlaceAutocompleteService {
constructor() { }
}
NgxGooglePlaceAutocompleteService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
NgxGooglePlaceAutocompleteService.ctorParameters = () => [];
/** @nocollapse */ NgxGooglePlaceAutocompleteService.ngInjectableDef = defineInjectable({ factory: function NgxGooglePlaceAutocompleteService_Factory() { return new NgxGooglePlaceAutocompleteService(); }, token: NgxGooglePlaceAutocompleteService, providedIn: "root" });
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgxGooglePlaceAutocompleteDirective {
/**
* @param {?} elRef
* @param {?} zone
*/
constructor(elRef, zone) {
this.zone = zone;
this.onSelect = new EventEmitter();
this.element = elRef.nativeElement;
}
/**
* @return {?}
*/
ngOnInit() {
/** @type {?} */
const autocomplete = new google.maps.places.Autocomplete(this.element);
google.maps.event.addListener(autocomplete, 'place_changed', (/**
* @return {?}
*/
() => {
this.zone.run((/**
* @return {?}
*/
() => {
/** @type {?} */
const place = this.getFormattedAddress(autocomplete.getPlace());
if (place) {
this.onSelect.emit(place);
}
}));
}));
}
/**
* @param {?} place
* @return {?}
*/
getFormattedAddress(place) {
/** @type {?} */
const location_obj = {
fullAddress: null,
city: null,
state: null,
streetNumber: null,
streetName: null,
country: null,
postalCode: null
};
for (let i in place.address_components) {
/** @type {?} */
const item = place.address_components[i];
location_obj['fullAddress'] = place.formatted_address;
if (item['types'].indexOf('locality') > -1) {
location_obj['city'] = item['long_name'];
}
else if (item['types'].indexOf('administrative_area_level_1') > -1) {
location_obj['state'] = item['short_name'];
}
else if (item['types'].indexOf('street_number') > -1) {
location_obj['streetNumber'] = item['short_name'];
}
else if (item['types'].indexOf('route') > -1) {
location_obj['streetName'] = item['long_name'];
}
else if (item['types'].indexOf('country') > -1) {
location_obj['country'] = item['long_name'];
}
else if (item['types'].indexOf('postal_code') > -1) {
location_obj['postalCode'] = item['short_name'];
}
}
return location_obj;
}
}
NgxGooglePlaceAutocompleteDirective.decorators = [
{ type: Directive, args: [{
selector: '[ngx-google-place]'
},] }
];
/** @nocollapse */
NgxGooglePlaceAutocompleteDirective.ctorParameters = () => [
{ type: ElementRef },
{ type: NgZone }
];
NgxGooglePlaceAutocompleteDirective.propDecorators = {
onSelect: [{ type: Output }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgxGooglePlaceAutocompleteModule {
}
NgxGooglePlaceAutocompleteModule.decorators = [
{ type: NgModule, args: [{
declarations: [NgxGooglePlaceAutocompleteDirective],
exports: [NgxGooglePlaceAutocompleteDirective]
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { NgxGooglePlaceAutocompleteService, NgxGooglePlaceAutocompleteDirective, NgxGooglePlaceAutocompleteModule };
//# sourceMappingURL=ngx-google-place-autocomplete.js.map