Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added script to render the map in the frontend
- Loading branch information
David Aguilera
committed
Feb 28, 2019
1 parent
b892411
commit 9bd9c82
Showing
3 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,72 @@ | |||
const { | |||
LatLng, | |||
Marker, | |||
Map, | |||
} = window.google.maps; | |||
|
|||
function init() { | |||
|
|||
[ ...document.querySelectorAll( '.nelio-maps-google-map-wrapper:not( .nelio-maps-ready )' ) ].map( el => initGoogleMap( el ) ); | |||
|
|||
}//end init() | |||
|
|||
function initGoogleMap( el ) { | |||
|
|||
el.classList.add( 'nelio-maps-ready' ); | |||
|
|||
const marker = extractMarkerPositionIfAny( el ); | |||
const map = new Map( el, extractMapOptions( el ) ); | |||
|
|||
if ( marker ) { | |||
new Marker( { map, clickable: false, position: new LatLng( marker.lat, marker.lng ) } ); | |||
}//end if | |||
|
|||
}//end initGoogleMap() | |||
|
|||
function extractMapOptions( el ) { | |||
|
|||
const lat = parseFloat( el.getAttribute( 'data-lat' ) ); | |||
const lng = parseFloat( el.getAttribute( 'data-lng' ) ); | |||
|
|||
const isDraggable = 'true' === el.getAttribute( 'data-is-draggable' ); | |||
const showZoomButtons = 'true' === el.getAttribute( 'data-show-zoom-buttons' ); | |||
const showMapTypeButton = 'true' === el.getAttribute( 'data-show-map-type-button' ); | |||
const showFullscreenButton = 'true' === el.getAttribute( 'data-show-fullscreen-button' ); | |||
|
|||
let styles = ''; | |||
try { | |||
styles = JSON.parse( el.getAttribute( 'data-styles' ) ); | |||
} catch ( e ) { } | |||
|
|||
return { | |||
center: new LatLng( lat, lng ), | |||
draggableCursor: ! isDraggable ? 'default' : undefined, | |||
fullscreenControl: showFullscreenButton, | |||
gestureHandling: isDraggable ? 'cooperative' : 'none', | |||
mapTypeControl: showMapTypeButton, | |||
streetViewControl: false, | |||
styles: styles, | |||
zoom: parseInt( el.getAttribute( 'data-zoom' ), 10 ), | |||
zoomControl: showZoomButtons, | |||
}; | |||
|
|||
}//end extractMapOptions() | |||
|
|||
function extractMarkerPositionIfAny( el ) { | |||
|
|||
const marker = el.querySelector( '.marker' ); | |||
if ( ! marker ) { | |||
return false; | |||
}//end if | |||
|
|||
const lat = marker.getAttribute( 'data-lat' ) || false; | |||
const lng = marker.getAttribute( 'data-lng' ) || false; | |||
if ( ! lat || ! lng ) { | |||
return false; | |||
}//end if | |||
|
|||
return { lat, lng }; | |||
|
|||
}//end extractMarkerPositionIfAny() | |||
|
|||
init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters