File

projects/maplander/components/src/lib/services/markers/markers.service.ts

Index

Methods

Constructor

constructor(_propertyTypePipe: PropertyTypePipe, _offeringTypePipe: OfferingTypePipe, _converter: CurrencyConverterService)
Parameters :
Name Type Optional
_propertyTypePipe PropertyTypePipe No
_offeringTypePipe OfferingTypePipe No
_converter CurrencyConverterService No

Methods

buildMarkers
buildMarkers(entities: NewPropertyLite[] | Lead[] | NewPropertyLiteSuggest[], favorites?: Lead[])
Parameters :
Name Type Optional
entities NewPropertyLite[] | Lead[] | NewPropertyLiteSuggest[] No
favorites Lead[] Yes
Returns : Marker[]
buildSpider
buildSpider(list: Marker[])
Parameters :
Name Type Optional
list Marker[] No
Returns : literal type
buildURL
buildURL(config: literal type)
Parameters :
Name Type Optional
config literal type No
Returns : string
Private Static calculateLocationFrom
calculateLocationFrom(location: GeoPt, bearingInRadians: number, distanceInMetres: number)
Parameters :
Name Type Optional
location GeoPt No
bearingInRadians number No
distanceInMetres number No
Returns : GeoPt
Private createPropertyInfoCard
createPropertyInfoCard(entity: NewPropertyLite | Lead | NewPropertyLiteSuggest)
Parameters :
Name Type Optional
entity NewPropertyLite | Lead | NewPropertyLiteSuggest No
Returns : PropertyCardInfo
Private Static getPropertyTypeItemByType
getPropertyTypeItemByType(type: PropertyTypeEnum)
Parameters :
Name Type Optional
type PropertyTypeEnum No
Returns : PropertyTypeItem
markersToCurrency
markersToCurrency(markers: Marker[], toCurrency: Currency, currencies: CER[])
Parameters :
Name Type Optional
markers Marker[] No
toCurrency Currency No
currencies CER[] No
Returns : Marker[]
Private Static propertyLiteOnLeads
propertyLiteOnLeads(propertyId: string, leads: Lead[])
Parameters :
Name Type Optional
propertyId string No
leads Lead[] No
Returns : boolean
validatePropertiesInPoly
validatePropertiesInPoly(properties: Marker[], paths: any[])
Parameters :
Name Type Optional
properties Marker[] No
paths any[] No
Returns : Marker[]
import {Injectable} from '@angular/core';
import {
  Address,
  CER,
  Currency,
  GeoPt,
  Lead,
  LeadTypeEnum,
  NewPropertyLite,
  NewPropertyLiteSuggest,
  OfferingTypeEnum,
  PropertyTypeEnum
} from '@maplander/types';
import {ConstantsComponents} from '../../constantsComponents';
import {Marker} from '../../utils/models/marker';
import {PropertyCardInfo} from '../../utils/models/property-card-info';
import {PropertyTypeItem} from '../../utils/models/property-type-item';
import {OfferingTypePipe} from '../../pipes/offering-type/offering-type.pipe';
import {PropertyTypePipe} from '../../pipes/property-type/property-type.pipe';
import {Utils} from '../../utils';
import {CurrencyConverterService} from '../currency-converter/currency-converter.service';

declare var google: any;

@Injectable()
export class MarkersService {

  constructor(
    private _propertyTypePipe: PropertyTypePipe,
    private _offeringTypePipe: OfferingTypePipe,
    private _converter: CurrencyConverterService
  ) {
  }

  private static getPropertyTypeItemByType(type: PropertyTypeEnum): PropertyTypeItem {
    let item: PropertyTypeItem = null;
    ConstantsComponents.PropertyTypeList.forEach((element) => {
      if (element.value === type) {
        item = element;
      }
    });
    return item;
  }

  private static propertyLiteOnLeads(propertyId: string, leads: Lead[]): boolean {
    let result = false;
    leads.forEach(lead => {
      if (lead.type !== LeadTypeEnum.FAVORITES) {
        return false;
      }
      if (propertyId === lead.propertyId) {
        result = true;
      }
    });
    return result;
  }

  private static calculateLocationFrom(location: GeoPt, bearingInRadians: number, distanceInMetres: number): GeoPt {
    const coordinateLatitudeInRadians = location.latitude * Math.PI / 180;
    const coordinateLongitudeInRadians = location.longitude * Math.PI / 180;
    const distanceComparedToEarth = distanceInMetres / 6378100.0;
    const resultLatitudeInRadians = Math.asin(Math.sin(coordinateLatitudeInRadians) * Math.cos(distanceComparedToEarth) +
      Math.cos(coordinateLatitudeInRadians) * Math.sin(distanceComparedToEarth) * Math.cos(bearingInRadians));
    const resultLongitudeInRadians = coordinateLongitudeInRadians + Math.atan2(Math.sin(bearingInRadians) *
      Math.sin(distanceComparedToEarth) * Math.cos(coordinateLatitudeInRadians), Math.cos(distanceComparedToEarth) -
      Math.sin(coordinateLatitudeInRadians) * Math.sin(resultLatitudeInRadians));
    const newCoordinates: any = {};
    newCoordinates.latitude = resultLatitudeInRadians * 180 / Math.PI;
    newCoordinates.longitude = resultLongitudeInRadians * 180 / Math.PI;
    return newCoordinates;
  }

  buildURL(config: {
    propertyId: string,
    offering: OfferingTypeEnum,
    propertyType: PropertyTypeEnum,
    address: Address
  }): string {
    let url = 'property/';
    url += `${this._propertyTypePipe.transform(config.propertyType, true)}-`;
    url += `${this._offeringTypePipe.transform(config.offering, ' ').replace(/ /g, '')}-`;
    url += config.address.colony ? `${config.address.colony}-` : '';
    url += config.address.city ? `${config.address.city}-` : '';
    url += config.propertyId;
    url = Utils.removeDiacritics(url).replace(/ /g, '-').toLowerCase();
    return url;
  }

  validatePropertiesInPoly(properties: Marker[], paths: any[]): Marker[] {
    const polygon = new google.maps.Polygon({paths: paths});
    if (properties && properties.length > 0) {
      properties.map((el: Marker) => {
        const customLatLng = new google.maps.LatLng(el.location.latitude, el.location.longitude);
        if (google.maps.geometry.poly.containsLocation(customLatLng, polygon)) {
          el.showPolygon = true;
          return el;
        } else {
          el.showPolygon = false;
          return el;
        }
      });
      properties = properties.filter((e) => {
        if (e.showPolygon) {
          return e;
        }
      });
      return properties;
    } else {
      return [];
    }
  }

  markersToCurrency(markers: Marker[], toCurrency: Currency, currencies: CER[]): Marker[] {
    markers = markers.map<Marker>(item => {
      if (Currency[item.info.currency] !== toCurrency) {
        item.info.price = this._converter.number(item.info.price, currencies, Currency[item.info.currency], toCurrency);
        item.info.currency = toCurrency.toString();
      }
      return item;
    });
    return markers;
  }

  buildMarkers(entities: NewPropertyLite[] | Lead[] | NewPropertyLiteSuggest[], favorites?: Lead[]): Marker[] {
    const markers: Marker[] = [];
    entities.forEach((entity) => {
      if (entity instanceof Lead) {
        markers.push({
          location: entity.pAddress.location,
          hover: false,
          visited: false,
          repeat: false,
          type: MarkersService.getPropertyTypeItemByType(entity.pType),
          info: this.createPropertyInfoCard(entity),
          isFavorite: entity.type === LeadTypeEnum.FAVORITES
        });
      } else {
        const m: Marker = {
          location: entity.address.location,
          hover: false,
          visited: false,
          repeat: false,
          type: MarkersService.getPropertyTypeItemByType(entity.type),
          info: this.createPropertyInfoCard(entity),
          isFavorite: false
        };
        if (entity instanceof NewPropertyLite) {
          m.isFavorite = favorites && favorites.length > 0 ? MarkersService.propertyLiteOnLeads(entity.id, favorites) : false;
        } else {
          m.isFavorite = favorites && favorites.length > 0 ? MarkersService.propertyLiteOnLeads(entity.propertyId, favorites) : false;
        }
        markers.push(m);
      }
    });
    return markers;
  }

  buildSpider(list: Marker[]): { markers: Marker[], polyLines: any[] } {
    const response = {markers: [], polyLines: []};
    list.map((el) => {
      const size = list.filter(item => {
        return el.location.latitude === item.location.latitude && el.location.longitude === item.location.longitude;
      });
      const distance = 12 * size.length / 2.0;
      const radiansBetweenAnnotations = (Math.PI * 2) / size.length;
      if (!el.repeat && size.length > 1) {
        const locationShare: any = Object.assign({}, el.location);
        for (let i = 0; i < size.length; i++) {
          const heading = radiansBetweenAnnotations * i;
          size[i].repeat = true;
          const newLocation = MarkersService.calculateLocationFrom(locationShare, heading, distance);
          response.polyLines.push({
            path: [
              {
                latitude: Number(locationShare.latitude),
                longitude: Number(locationShare.longitude)
              },
              {
                latitude: Number(newLocation.latitude),
                longitude: Number(newLocation.longitude)
              }]
          });
          size[i].location = newLocation;
        }
      }
      return el;
    });
    response.markers = list;
    return response;
  }

  private createPropertyInfoCard(entity: NewPropertyLite | Lead | NewPropertyLiteSuggest): PropertyCardInfo {
    let info: PropertyCardInfo = null;
    if (entity instanceof NewPropertyLite) {
      info = {
        address: new Address(entity.address || null),
        authUsers: entity.authUsers || '',
        currency: entity.currency,
        propertyId: entity.id,
        image: entity.image,
        offering: entity.offering,
        price: entity.price,
        type: entity.type,
        url: this.buildURL({
          address: entity.address,
          offering: entity.offering,
          propertyType: entity.type,
          propertyId: entity.id
        }),
        status: entity.status
      };
    } else if (entity instanceof Lead) {
      info = {
        address: new Address(entity.pAddress || null),
        authUsers: '',
        currency: entity.pCurrency,
        propertyId: entity.propertyId,
        image: entity.pImage,
        offering: entity.pOffering,
        price: entity.pPrice,
        type: entity.pType,
        url: this.buildURL({
          address: entity.pAddress,
          offering: entity.pOffering,
          propertyType: entity.pType,
          propertyId: entity.propertyId
        }),
        status: null
      };
    } else if (entity instanceof NewPropertyLiteSuggest) {
      info = {
        address: new Address(entity.address || null),
        authUsers: '',
        currency: entity.currency,
        propertyId: entity.propertyId,
        image: entity.image,
        offering: entity.offering,
        price: entity.price,
        type: entity.type,
        url: this.buildURL({
          address: entity.address,
          offering: entity.offering,
          propertyType: entity.type,
          propertyId: entity.propertyId
        }),
        status: null
      };
    }
    return info;
  }
}

result-matching ""

    No results matching ""