Skip to content

Commit

Permalink
show pin with icon
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew44-mappable committed Apr 2, 2024
1 parent 529b903 commit 2e11856
Show file tree
Hide file tree
Showing 12 changed files with 462 additions and 15 deletions.
48 changes: 48 additions & 0 deletions example/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,51 @@ const BOUNDS = [

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const LOCATION = {bounds: BOUNDS};

// Function for generating a pseudorandom number
const seed = (s) => () => {
s = Math.sin(s) * 10000;
return s - Math.floor(s);
};
const rnd = seed(1000); // () => Math.random()

// Generating random coordinates of a point [lng, lat] in a given boundary
const getRandomPointCoordinates = () => [
BOUNDS[0][0] + (BOUNDS[1][0] - BOUNDS[0][0]) * rnd(),
BOUNDS[1][1] + (BOUNDS[0][1] - BOUNDS[1][1]) * rnd()
];

const icons = [
'airport',
'attraction',
'auto',
'aviary',
'baby_shop',
'banks',
'barbeque',
'bars',
'beach',
'bench',
'bike',
'bike_rent',
'boat_station',
'bookstore',
'buddhism',
'building',
'bus',
'cafe',
'car_park',
'catholic_church',
'cemetery',
'childrens_playground',
'cinemas'
];

const colors = ['#F09A75', '#5EBD8C', '#E096D0'];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const markers = icons.map((iconName) => ({
coordinates: getRandomPointCoordinates(),
name: iconName,
color: colors[Math.floor(rnd() * colors.length)]
}));
5 changes: 3 additions & 2 deletions example/vanilla.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
map.addChild((defaultFeatures = new MMapDefaultFeaturesLayer()));
map.addChild(new MMapControls({position: 'right'}).addChild(new MMapZoomControl({})));

const coordinates = [55.289051, 25.229907];
map.addChild(new MMapDefaultMarker({coordinates, color: '#F09A75', name: 'auto'}));
markers.forEach(({coordinates, name, color}) => {
map.addChild(new MMapDefaultMarker({coordinates, color, name}));
});
}
</script>

Expand Down
11 changes: 9 additions & 2 deletions src/MMapDefaultMarker/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
border-radius: 50%;
}

.mappable--point svg {
transform: translate(calc(-50% + 2px), calc(-100% + 2px));
.mappable--pin {
position: absolute;
transform: translate(calc(-50% + 2px), calc(-100%));
}

.mappable--icon {
color: #fffffffc;
position: absolute;
transform: translate(calc(-24px - 10px),10px);
}
22 changes: 20 additions & 2 deletions src/MMapDefaultMarker/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {MMapMarker, MMapMarkerProps} from '@mappable-world/mappable-types';
import {IconName} from '../icons/types/icons';
import {IconName} from '../icons/icon-name';
import {icons} from '../icons';
import pin from './pin.svg';
import './index.css';

Expand All @@ -11,25 +12,42 @@ export type MMapDefaultMarkerProps = MMapMarkerProps & {
export class MMapDefaultMarker extends mappable.MMapComplexEntity<MMapDefaultMarkerProps> {
private _marker: MMapMarker;
private _markerElement: HTMLElement;
private _pin: HTMLElement;
private _icon: HTMLElement;

constructor(props: MMapDefaultMarkerProps) {
super(props);
}

protected _onAttach(): void {
this._markerElement = document.createElement('mappable');
this._pin = document.createElement('mappable');
this._icon = document.createElement('mappable');

this._markerElement.classList.add('mappable--point');
this._markerElement.innerHTML = pin;
this._markerElement.style.color = this._props.color;
this._markerElement.style.backgroundColor = this._props.color;

this._pin.classList.add('mappable--pin');
this._pin.innerHTML = pin;

this._icon.classList.add('mappable--icon');
this._icon.innerHTML = icons[this._props.name].normal;

this._pin.appendChild(this._icon);
this._markerElement.appendChild(this._pin);

this._marker = new mappable.MMapMarker(this._props, this._markerElement);
this.addChild(this._marker);
}

protected _onUpdate(propsDiff: Partial<MMapDefaultMarkerProps>): void {
if (propsDiff.color !== undefined) {
this._markerElement.style.color = this._props.color;
this._markerElement.style.backgroundColor = this._props.color;
}
if (propsDiff.name !== undefined) {
this._icon.innerHTML = icons[propsDiff.name].normal;
}
this._marker.update(this._props);
}
Expand Down
2 changes: 1 addition & 1 deletion src/MMapDefaultMarker/pin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions src/icons/icon-name.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** Don't edit manually. Generated by script: ./tools/icons/generate-types.ts */
type IconName =
export type IconName =
| 'airport'
| 'attraction'
| 'auto'
Expand Down Expand Up @@ -120,5 +120,3 @@ type IconName =
| 'waterfall'
| 'wc'
| 'zoo';

export {IconName};
1 change: 1 addition & 0 deletions src/icons/icon-size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type IconSize = 'normal' | 'small';
Loading

0 comments on commit 2e11856

Please sign in to comment.