Skip to content

Commit

Permalink
feat(SebmGoogleMapMarker): support opacity
Browse files Browse the repository at this point in the history
Closes #523
Closes #522
  • Loading branch information
TSHiYK authored and sebholstein committed Aug 9, 2016
1 parent 468a1a8 commit 8ed927b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/core/directives/google-map-marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let markerId = 0;
selector: 'sebm-google-map-marker',
inputs: [
'latitude', 'longitude', 'title', 'label', 'draggable: markerDraggable', 'iconUrl',
'openInfoWindow', 'fitBounds'
'openInfoWindow', 'fitBounds', 'opacity'
],
outputs: ['markerClick', 'dragEnd']
})
Expand Down Expand Up @@ -78,6 +78,11 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges, AfterContentIn
*/
openInfoWindow: boolean = true;

/**
* The marker's opacity between 0.0 and 1.0.
*/
opacity: number = 1;

/**
* This event emitter gets emitted when the user clicks on the marker.
*/
Expand Down Expand Up @@ -129,6 +134,9 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges, AfterContentIn
if (changes['iconUrl']) {
this._markerManager.updateIcon(this);
}
if (changes['opacity']) {
this._markerManager.updateOpacity(this);
}
}

private _addEventListeners() {
Expand All @@ -140,10 +148,11 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges, AfterContentIn
});
this._observableSubscriptions.push(cs);

const ds = this._markerManager.createEventObservable<mapTypes.MouseEvent>('dragend', this)
.subscribe((e: mapTypes.MouseEvent) => {
this.dragEnd.emit({coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}});
});
const ds =
this._markerManager.createEventObservable<mapTypes.MouseEvent>('dragend', this)
.subscribe((e: mapTypes.MouseEvent) => {
this.dragEnd.emit(<MouseEvent>{coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}});
});
this._observableSubscriptions.push(ds);
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/services/google-maps-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Marker extends MVCObject {
setLabel(label: string|MarkerLabel): void;
setDraggable(draggable: boolean): void;
setIcon(icon: string): void;
setOpacity(opacity: number): void;
getLabel(): MarkerLabel;
}

Expand All @@ -37,6 +38,7 @@ export interface MarkerOptions {
label?: string|MarkerLabel;
draggable?: boolean;
icon?: string;
opacity?: number;
}

export interface MarkerLabel {
Expand Down
7 changes: 6 additions & 1 deletion src/core/services/managers/marker-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ export class MarkerManager {
return this._markers.get(marker).then((m: Marker) => m.setIcon(marker.iconUrl));
}

updateOpacity(marker: SebmGoogleMapMarker): Promise<void> {
return this._markers.get(marker).then((m: Marker) => m.setOpacity(marker.opacity));
}

addMarker(marker: SebmGoogleMapMarker) {
const markerPromise = this._mapsWrapper.createMarker({
position: {lat: marker.latitude, lng: marker.longitude},
label: marker.label,
draggable: marker.draggable,
icon: marker.iconUrl
icon: marker.iconUrl,
opacity: marker.opacity
});
this._markers.set(marker, markerPromise);
}
Expand Down
35 changes: 33 additions & 2 deletions test/services/managers/marker-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export function main() {
position: {lat: 34.4, lng: 22.3},
label: 'A',
draggable: false,
icon: undefined
icon: undefined,
opacity: 1
});
}));
});
Expand Down Expand Up @@ -75,13 +76,43 @@ export function main() {
position: {lat: 34.4, lng: 22.3},
label: 'A',
draggable: false,
icon: undefined
icon: undefined,
opacity: 1
});
const iconUrl = 'http://angular-maps.com/icon.png';
newMarker.iconUrl = iconUrl;
return markerManager.updateIcon(newMarker).then(
() => { expect(markerInstance.setIcon).toHaveBeenCalledWith(iconUrl); });
})));
});

describe('set marker opacity', () => {
it('should update that marker via setOpacity method when the markerOpacity changes',
async(inject(
[MarkerManager, GoogleMapsAPIWrapper],
(markerManager: MarkerManager, apiWrapper: GoogleMapsAPIWrapper) => {
const newMarker = new SebmGoogleMapMarker(markerManager);
newMarker.latitude = 34.4;
newMarker.longitude = 22.3;
newMarker.label = 'A';

const markerInstance: Marker =
jasmine.createSpyObj('Marker', ['setMap', 'setOpacity']);
(<any>apiWrapper.createMarker).and.returnValue(Promise.resolve(markerInstance));

markerManager.addMarker(newMarker);
expect(apiWrapper.createMarker).toHaveBeenCalledWith({
position: {lat: 34.4, lng: 22.3},
label: 'A',
draggable: false,
icon: undefined,
opacity: 1
});
const opacity = 0.4;
newMarker.opacity = opacity;
return markerManager.updateOpacity(newMarker).then(
() => { expect(markerInstance.setOpacity).toHaveBeenCalledWith(opacity); });
})));
});
});
}

0 comments on commit 8ed927b

Please sign in to comment.