File
Implements
Metadata
selector |
lib-location |
styleUrls |
./location.component.scss |
templateUrl |
./location.component.html |
Methods
Private
configGoogleMaps
|
configGoogleMaps()
|
|
|
Static
formatAddress
|
formatAddress(address: Address, keepVisibility?: boolean)
|
|
Parameters :
Name |
Type |
Optional |
address |
Address
|
No
|
keepVisibility |
boolean
|
Yes
|
|
ngAfterViewInit
|
ngAfterViewInit()
|
|
|
onMarkerDragEnd
|
onMarkerDragEnd(event: MouseEvent)
|
|
Parameters :
Name |
Type |
Optional |
event |
MouseEvent
|
No
|
|
onPlaceChanged
|
onPlaceChanged(ev: SearchBoxEvent)
|
|
Parameters :
Name |
Type |
Optional |
ev |
SearchBoxEvent
|
No
|
|
onSearchBoxStart
|
onSearchBoxStart(ev: SearchBoxDirective)
|
|
Parameters :
Name |
Type |
Optional |
ev |
SearchBoxDirective
|
No
|
|
Private
reverseGeoCode
|
reverseGeoCode(location: GeoPt)
|
|
Parameters :
Name |
Type |
Optional |
location |
GeoPt
|
No
|
|
Private
updateMap
|
updateMap()
|
|
|
Private
_result
|
Type : SearchBoxEvent
|
|
Private
_searchBox
|
Type : SearchBoxDirective
|
|
Public
mapOptions
|
Type : any
|
|
import {AfterViewInit, Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {MouseEvent} from '@agm/core/map-types';
import {SearchBoxDirective} from '../../directives/search-box/search-box.directive';
import {AddressService} from '../address/address.service';
import {DialogResponseType, Address, DialogResponse, GeoPt, LocationOptions, SearchBoxEvent} from '@maplander/types';
import {Utils} from '../../../utils/utils';
@Component({
selector: 'lib-location',
templateUrl: './location.component.html',
styleUrls: ['./location.component.scss']
})
export class LocationComponent implements OnInit, AfterViewInit {
public mapOptions: any;
public clear: boolean;
private _searchBox: SearchBoxDirective;
private _result: SearchBoxEvent;
constructor(
@Inject(MAT_DIALOG_DATA) private _data: LocationOptions,
private _dialogRef: MatDialogRef<LocationComponent>,
private _addressDialog: AddressService
) {
this._result = {
address: new Address(),
address_formatted: ''
};
}
static formatAddress(address: Address, keepVisibility?: boolean): string {
/**
* - Address visible
* Street #Ext Int. Lt Mz, Colony, PostalCode, Township, City, State, Country
* - Address hidden
* Colony, PostalCode, Township, City, State, Country
* */
let result = '';
result += address.street ? `${address.street} ` : '';
result += address.outdoorNumber ? `#${address.outdoorNumber} ` : '';
result += address.interiorNumber ? `Int. ${address.interiorNumber} ` : '';
result += address.mz ? `Mz ${address.mz} ` : '';
result += address.lt ? `Lt ${address.lt} ` : '';
if (keepVisibility && !address.visible) {
result = '';
}
result += address.colony ? `${address.colony}, ` : '';
result += address.postalCode ? `${address.postalCode}, ` : '';
result += address.township ? `${address.township}, ` : '';
result += address.city ? `${address.city}, ` : '';
result += address.state ? `${address.state}, ` : '';
result += address.country ? `${address.country}` : '';
return result;
}
ngOnInit() {
this.configGoogleMaps();
}
ngAfterViewInit(): void {
if (this._data && this._data.location && this._data.location.latitude !== 0 && this._data.location.longitude !== 0) {
this._result.address.location = this._data.location;
this.mapOptions.center.lat = this._data.location.latitude;
this.mapOptions.center.lng = this._data.location.longitude;
} else {
this._result.address.location = {
latitude: Number(this.mapOptions.center.lat),
longitude: Number(this.mapOptions.center.lng)
};
}
}
onMarkerDragEnd(event: MouseEvent): void {
this.reverseGeoCode({latitude: event.coords.lat, longitude: event.coords.lng});
}
onSearchBoxStart(ev: SearchBoxDirective): void {
this._searchBox = ev;
this.reverseGeoCode(this._result.address.location);
}
onPlaceChanged(ev: SearchBoxEvent): void {
this._result = ev;
this.updateMap();
}
gps(): void {
this._searchBox.gps();
}
clearInput(): void {
this.clear = false;
this._searchBox.clearInput();
}
accept(): void {
this._addressDialog.open(this._result.address).afterClosed().subscribe((result: DialogResponse<Address>) => {
if (result && result.code === DialogResponseType.ACCEPT) {
this._result.address = result.data;
this._result.address_formatted = LocationComponent.formatAddress(result.data);
this._dialogRef.close({code: DialogResponseType.ACCEPT, data: this._result} as DialogResponse<SearchBoxEvent>);
}
});
}
public close(): void {
this._dialogRef.close({code: DialogResponseType.CANCEL} as DialogResponse<SearchBoxEvent>);
}
private updateMap(): void {
this.mapOptions.center.lat = this._result.address.location.latitude;
this.mapOptions.center.lng = this._result.address.location.longitude;
this.clear = true;
this._searchBox.setValueInput(this._result.address_formatted);
}
private reverseGeoCode(location: GeoPt): void {
this._searchBox.reverseGeoCode(location).subscribe((ev: SearchBoxEvent) => {
this._result = ev;
this.clear = true;
this._searchBox.setValueInput(this._result.address_formatted);
}, () => {
this._searchBox.setValueInput('');
});
}
private configGoogleMaps(): void {
this.mapOptions = Utils.getDefaultConfigGoogleMaps();
this.mapOptions.zoom = 16;
if (this._data.dark) {
this.mapOptions.styles = Utils.getDarkStylesGoogleMaps();
} else {
this.mapOptions.styles = Utils.getLightStylesGoogleMaps();
}
}
}
<div class="location">
<h1 mat-dialog-title>Ubicar dirección en el mapa</h1>
<mat-dialog-content>
<div class="search_place">
<input class="input_search_place" title="Search box" libSearchBox="" (start)="onSearchBoxStart($event)"
(placeChanged)="onPlaceChanged($event)"
placeholder="Buscar calle, colonia o ciudad" #input>
<div class="right">
<button mat-icon-button="" *ngIf="clear" (click)="clearInput()">
<mat-icon>close</mat-icon>
</button>
<button mat-icon-button="" *ngIf="!clear">
<mat-icon>search</mat-icon>
</button>
</div>
</div>
<agm-map
[latitude]="mapOptions.center.lat"
[longitude]="mapOptions.center.lng"
[gestureHandling]="mapOptions.gestureHandling"
[streetViewControl]="mapOptions.streetViewControl"
[styles]="mapOptions.styles"
[zoom]="mapOptions.zoom"
[zoomControl]="mapOptions.zoomControl">
<agm-marker
[latitude]="mapOptions.center.lat"
[longitude]="mapOptions.center.lng"
[markerDraggable]="true"
(dragEnd)="onMarkerDragEnd($event)"></agm-marker>
</agm-map>
<button mat-mini-fab color="primary" class="btn-gps" (click)="gps()">
<mat-icon>my_location</mat-icon>
</button>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-flat-button color="primary" (click)="close()">CANCELAR</button>
<button mat-flat-button color="primary" (click)="accept()">ACEPTAR</button>
</mat-dialog-actions>
</div>
::ng-deep .dialog_location {
width: 40%;
height: 80%;
@media screen and (max-width: 600px){
min-width: 100% !important;
height: 100% !important;
}
mat-dialog-container {
width: 100%;
height: 100%;
}
}
:host {
display: block;
width: 100%;
height: 100%;
}
.location {
width: 100%;
height: 100%;
mat-dialog-content {
width: 100%;
height: calc(100% - 120px);
max-height: 100%;
margin: 0;
padding: 0;
position: relative;
}
mat-dialog-actions {
justify-content: flex-end;
}
.search_place {
width: 100%;
height: 52px;
position: relative;
border-radius: 4px 4px 0 0;
border-bottom: 1px solid rgba(0, 0, 0, .4);
padding: 8px 0;
box-sizing: border-box;
margin-bottom: 16px;
input {
width: 100%;
box-sizing: border-box;
padding: 6px 48px 6px 0;
border: none;
border-radius: inherit;
font-size: 18px;
}
input:focus {
outline: none;
}
.left {
position: absolute;
top: 12px;
left: 12px;
}
.right {
position: absolute;
top: 3px;
right: 8px;
}
}
agm-map {
width: 100%;
height: calc(100% - 57px - 16px);
}
.btn-gps {
position: absolute;
right: 60px;
bottom: 28px;
box-shadow: none;
}
}
Legend
Html element with directive