@@ -21,34 +21,17 @@ const logger = getLogger('aurelia-google-maps');
2121
2222declare let google : any ;
2323
24- export interface BaseMarker {
24+ export interface Marker {
2525 icon ?: string ;
2626 label ?: string ;
2727 title ?: string ;
2828 draggable ?: boolean ;
2929 custom ?: any ;
3030 infoWindow ?: { pixelOffset ?: number , content : string , position ?: number , maxWidth ?: number }
31- }
32-
33- export interface AddressMarker extends BaseMarker {
34- address : string ;
35- }
36-
37- export interface LatLongMarker extends BaseMarker {
3831 latitude : number | string ;
3932 longitude : number | string ;
4033}
4134
42- const isAddressMarker = ( marker : Marker ) : marker is AddressMarker => {
43- return ( < AddressMarker > marker ) . address !== undefined ;
44- }
45-
46- const isLatLongMarker = ( marker : Marker ) : marker is LatLongMarker => {
47- return ( < LatLongMarker > marker ) . latitude !== undefined && ( < LatLongMarker > marker ) . longitude !== undefined ;
48- }
49-
50- export type Marker = AddressMarker | LatLongMarker ;
51-
5235@noView ( )
5336@customElement ( 'google-map' )
5437@inject ( Element , TaskQueue , Configure , BindingEngine , EventAggregator , GoogleMapsAPI )
@@ -59,7 +42,6 @@ export class GoogleMaps {
5942 private bindingEngine : BindingEngine ;
6043 private eventAggregator : EventAggregator ;
6144 private googleMapsApi : GoogleMapsAPI ;
62- private validMarkers : LatLongMarker [ ] ;
6345 private _geocoder : any ;
6446
6547 @bindable longitude : number = 0 ;
@@ -105,23 +87,23 @@ export class GoogleMaps {
10587 } ) ;
10688 } ) ;
10789
108- this . eventAggregator . subscribe ( 'startMarkerHighlight' , function ( data : any ) {
90+ this . eventAggregator . subscribe ( 'startMarkerHighlight' , function ( data : any ) {
10991 let mrkr : any = self . _renderedMarkers [ data . index ] ;
11092 mrkr . setIcon ( mrkr . custom . altIcon ) ;
11193 mrkr . setZIndex ( ( < any > window ) . google . maps . Marker . MAX_ZINDEX + 1 ) ;
11294 } ) ;
11395
114- this . eventAggregator . subscribe ( 'stopMarkerHighLight' , function ( data : any ) {
96+ this . eventAggregator . subscribe ( 'stopMarkerHighLight' , function ( data : any ) {
11597 let mrkr : any = self . _renderedMarkers [ data . index ] ;
116- mrkr . setIcon ( mrkr . custom . defaultIcon ) ;
98+ mrkr . setIcon ( mrkr . custom . defaultIcon ) ;
11799 } ) ;
118100
119- this . eventAggregator . subscribe ( 'panToMarker' , function ( data : any ) {
101+ this . eventAggregator . subscribe ( 'panToMarker' , function ( data : any ) {
120102 self . map . panTo ( self . _renderedMarkers [ data . index ] . position ) ;
121103 self . map . setZoom ( 17 ) ;
122104 } ) ;
123105
124- this . eventAggregator . subscribe ( `clearMarkers` , function ( ) {
106+ this . eventAggregator . subscribe ( `clearMarkers` , function ( ) {
125107 this . clearMarkers ( ) ;
126108 } ) ;
127109 }
@@ -131,7 +113,7 @@ export class GoogleMaps {
131113 return ;
132114 }
133115
134- this . _renderedMarkers . forEach ( function ( marker : any ) {
116+ this . _renderedMarkers . forEach ( function ( marker : any ) {
135117 marker . setMap ( null ) ;
136118 } ) ;
137119
@@ -158,7 +140,6 @@ export class GoogleMaps {
158140 mapTypeId : mapTypeId
159141 } ) ;
160142
161-
162143 this . map = new ( < any > window ) . google . maps . Map ( this . element , options ) ;
163144 if ( this . mapLoaded ) {
164145 this . mapLoaded ( this . map ) ;
@@ -224,7 +205,7 @@ export class GoogleMaps {
224205 *
225206 */
226207 renderMarker ( marker : Marker ) : Promise < void > {
227- let markerLatLng = new ( < any > window ) . google . maps . LatLng ( parseFloat ( < string > ( < LatLongMarker > marker ) . latitude ) , parseFloat ( < string > ( < LatLongMarker > marker ) . longitude ) ) ;
208+ let markerLatLng = new ( < any > window ) . google . maps . LatLng ( parseFloat ( < string > marker . latitude ) , parseFloat ( < string > marker . longitude ) ) ;
228209
229210 return this . _mapPromise . then ( ( ) => {
230211 // Create the marker
@@ -298,69 +279,6 @@ export class GoogleMaps {
298279 } ) ;
299280 }
300281
301- /**
302- * Geocodes an address, once the Google Map script
303- * has been properly loaded and promise instantiated.
304- *
305- * @param address string
306- * @param geocoder any
307- *
308- */
309- geocodeAddress ( address : string ) {
310- this . geocode ( address ) . then ( firstResult => {
311- this . setCenter ( firstResult . geometry . location ) ;
312- this . createMarker ( {
313- map : this . map ,
314- position : firstResult . geometry . location
315- } ) . then ( ( createdMarker : any ) => {
316- this . _locationByAddressMarkers . push ( createdMarker ) ;
317- this . eventAggregator . publish ( LOCATIONADDED , Object . assign ( createdMarker , { placeId : firstResult . place_id } ) ) ;
318- } ) ;
319- } ) . catch ( console . info ) ;
320- }
321-
322- /**
323- * Geocodes Address and returns the coordinates once the google map has been properly initialized
324- *
325- * @param marker string
326- *
327- */
328- addressMarkerToMarker ( marker : AddressMarker ) : Promise < void | BaseMarker > {
329- return this . geocode ( marker . address ) . then ( firstResults => {
330- return {
331- ... marker ,
332- latitude : firstResults . geometry . location . lat ( ) ,
333- longitude : firstResults . geometry . location . lng ( ) ,
334- } ;
335- } ) . catch ( console . info ) ;
336- }
337-
338- /**
339- * Geocodes Address and returns the firstresults object after google maps has initialized
340- *
341- * @param address string
342- *
343- */
344- private geocode ( address : string ) : Promise < any > {
345- return this . _mapPromise . then ( ( ) => {
346- return new Promise ( ( resolve , reject ) => {
347- this . geocoder . geocode ( { 'address' : address } , ( results : any , status : string ) => {
348- if ( status !== ( < any > window ) . google . maps . GeocoderStatus . OK ) {
349- reject ( new Error ( `Failed to geocode address '${ address } ' with status: ${ status } ` ) ) ;
350- }
351- resolve ( results [ 0 ] ) ;
352- } ) ;
353- } ) ;
354- } ) ;
355- }
356-
357- private get geocoder ( ) {
358- if ( ! this . _geocoder ) {
359- this . _geocoder = new ( < any > window ) . google . maps . Geocoder ;
360- }
361- return this . _geocoder ;
362- }
363-
364282 /**
365283 * Get Current Position
366284 *
@@ -462,26 +380,8 @@ export class GoogleMaps {
462380
463381 // Render all markers again
464382 this . _mapPromise . then ( ( ) => {
465- Promise . all < LatLongMarker > (
466- newValue . map ( marker => {
467- if ( isAddressMarker ( marker ) && ! isLatLongMarker ( marker ) ) {
468- return this . addressMarkerToMarker ( marker ) ;
469- } else {
470- }
471- return marker ;
472- } )
473- ) . then ( validMarkers => {
474- // Addresses that fail to parse return undefined (because the error is caught earlier in the promise chain)
475- this . validMarkers = validMarkers . filter ( marker => typeof marker !== 'undefined' ) ;
476- return Promise . all ( this . validMarkers . map ( this . renderMarker . bind ( this ) ) ) ;
477- } ) . then ( ( ) => {
478- /**
479- * We queue up a task to update the bounds, because in the case of multiple bound properties changing all at once,
480- * we need to let Aurelia handle updating the other properties before we actually trigger a re-render of the map
481- */
482- this . taskQueue . queueTask ( ( ) => {
483- this . zoomToMarkerBounds ( ) ;
484- } ) ;
383+ let markerPromises = newValue . map ( marker => {
384+ return this . renderMarker ( marker ) ;
485385 } ) ;
486386
487387 // Wait until all of the renderMarker calls have been resolved
0 commit comments