Loading shop/mobile-ionic/src/app/pages/+invite/+by-location/by-location.page.ts +82 −4 Original line number Diff line number Diff line Loading @@ -3,7 +3,10 @@ import * as _ from 'lodash'; import { every, isEmpty, isNumber } from 'lodash'; import { Component, OnInit, OnDestroy } from '@angular/core'; import { InviteRequestRouter } from '@modules/client.common.angular2/routers/invite-request-router.service'; import { Country } from '@modules/server.common/entities/GeoLocation'; import { Country, getCountryName } from '@modules/server.common/entities/GeoLocation'; import { first } from 'rxjs/operators'; import { InviteRouter } from '@modules/client.common.angular2/routers/invite-router.service'; import { HttpClient, HttpParams } from '@angular/common/http'; Loading Loading @@ -277,7 +280,11 @@ export class ByLocationPage implements OnInit, OnDestroy { private async createInviteRequest(): Promise<InviteRequest> { const device = { id: this.store.deviceId }; // await this.deviceService.device.pipe(first()).toPromise(); const coordinatesObj = await this.getCoordinates(); let coordinatesObj = await this.getCoordinatesByAddress(); if (coordinatesObj == null) { coordinatesObj = await this.getCoordinates(); } if (coordinatesObj != null) { return this.inviteRequestRouter.create({ Loading Loading @@ -353,6 +360,11 @@ export class ByLocationPage implements OnInit, OnDestroy { result = true; } if (!this.country) { this.country = +Country[address.country]; result = true; } this.detectingLocation = false; return result; Loading Loading @@ -383,13 +395,26 @@ export class ByLocationPage implements OnInit, OnDestroy { }, (results, status) => { if (status === google.maps.GeocoderStatus.OK) { const address = results.find((x) => let country; let address = results.find((x) => x.types.includes('street_address') ); if (!address) { address = results.find((x) => x.types.includes('route') ); country = address.address_components[4].short_name; } else { country = address.address_components[5].short_name; } const formattedAddress = { locality: address.address_components[3].short_name, thoroughfare: address.address_components[1].short_name address.address_components[1].short_name, country }; resolve(formattedAddress); Loading @@ -400,4 +425,57 @@ export class ByLocationPage implements OnInit, OnDestroy { ); }); } private getCoordinatesByAddress(): Promise<{ lng: number; lat: number; } | null> { const house = this.house; const streetAddress = this.streetAddress; const city = this.city; const countryName = getCountryName(this.country); if (!streetAddress || !house || !city || !countryName) { return; } const geocoder = new google.maps.Geocoder(); return new Promise(function(resolve, reject) { geocoder.geocode( { address: `${streetAddress} ${house}, ${city}`, componentRestrictions: { country: countryName } }, (results, status) => { if (status === google.maps.GeocoderStatus.OK) { const place: google.maps.GeocoderResult = results[0]; const neededAddressTypes = [ 'country', 'locality', 'route', 'street_number' ]; const existedTypes = place.address_components .map((ac) => ac.types) .reduce((acc, val) => acc.concat(val), []); for (const type of neededAddressTypes) { if (!existedTypes.includes(type)) { resolve(null); return; } } const loc = place.geometry.location; resolve({ lat: loc.lat(), lng: loc.lng() }); } } ); }); } } Loading
shop/mobile-ionic/src/app/pages/+invite/+by-location/by-location.page.ts +82 −4 Original line number Diff line number Diff line Loading @@ -3,7 +3,10 @@ import * as _ from 'lodash'; import { every, isEmpty, isNumber } from 'lodash'; import { Component, OnInit, OnDestroy } from '@angular/core'; import { InviteRequestRouter } from '@modules/client.common.angular2/routers/invite-request-router.service'; import { Country } from '@modules/server.common/entities/GeoLocation'; import { Country, getCountryName } from '@modules/server.common/entities/GeoLocation'; import { first } from 'rxjs/operators'; import { InviteRouter } from '@modules/client.common.angular2/routers/invite-router.service'; import { HttpClient, HttpParams } from '@angular/common/http'; Loading Loading @@ -277,7 +280,11 @@ export class ByLocationPage implements OnInit, OnDestroy { private async createInviteRequest(): Promise<InviteRequest> { const device = { id: this.store.deviceId }; // await this.deviceService.device.pipe(first()).toPromise(); const coordinatesObj = await this.getCoordinates(); let coordinatesObj = await this.getCoordinatesByAddress(); if (coordinatesObj == null) { coordinatesObj = await this.getCoordinates(); } if (coordinatesObj != null) { return this.inviteRequestRouter.create({ Loading Loading @@ -353,6 +360,11 @@ export class ByLocationPage implements OnInit, OnDestroy { result = true; } if (!this.country) { this.country = +Country[address.country]; result = true; } this.detectingLocation = false; return result; Loading Loading @@ -383,13 +395,26 @@ export class ByLocationPage implements OnInit, OnDestroy { }, (results, status) => { if (status === google.maps.GeocoderStatus.OK) { const address = results.find((x) => let country; let address = results.find((x) => x.types.includes('street_address') ); if (!address) { address = results.find((x) => x.types.includes('route') ); country = address.address_components[4].short_name; } else { country = address.address_components[5].short_name; } const formattedAddress = { locality: address.address_components[3].short_name, thoroughfare: address.address_components[1].short_name address.address_components[1].short_name, country }; resolve(formattedAddress); Loading @@ -400,4 +425,57 @@ export class ByLocationPage implements OnInit, OnDestroy { ); }); } private getCoordinatesByAddress(): Promise<{ lng: number; lat: number; } | null> { const house = this.house; const streetAddress = this.streetAddress; const city = this.city; const countryName = getCountryName(this.country); if (!streetAddress || !house || !city || !countryName) { return; } const geocoder = new google.maps.Geocoder(); return new Promise(function(resolve, reject) { geocoder.geocode( { address: `${streetAddress} ${house}, ${city}`, componentRestrictions: { country: countryName } }, (results, status) => { if (status === google.maps.GeocoderStatus.OK) { const place: google.maps.GeocoderResult = results[0]; const neededAddressTypes = [ 'country', 'locality', 'route', 'street_number' ]; const existedTypes = place.address_components .map((ac) => ac.types) .reduce((acc, val) => acc.concat(val), []); for (const type of neededAddressTypes) { if (!existedTypes.includes(type)) { resolve(null); return; } } const loc = place.geometry.location; resolve({ lat: loc.lat(), lng: loc.lng() }); } } ); }); } }