How can Google Map be included in a component using sightly? | Community
Skip to main content
New Participant
October 16, 2015
Solved

How can Google Map be included in a component using sightly?

  • October 16, 2015
  • 1 reply
  • 1248 views

I have assets stored in AEM that have lat/long attributes. I would like to create a component that lets users select one of these assets and then it will create a google map with marker for that lat/long. 

To accomplish this I have created a component that has googlemap.html in apps/foo/components/content/googlemap/googlemap.html with the following code

<div  data-sly-use.ev="Google" id="${ev.googleClass || ''}" > ${ev.googleClass @ context='unsafe'} </div> <style> #map-canvas { width: 500px; height: 400px; } </style> <script src="https://maps.googleapis.com/maps/api/js"></script> <script> function initialize() { var mapCanvas = document.getElementById('map-canvas'); var myLatlng = new google.maps.LatLng(-25.363882,131.044922); var mapOptions = { center: myLatlng, zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(mapCanvas, mapOptions) var marker = new google.maps.Marker({ position: myLatlng, map: map, title: 'Hello World!' }); } google.maps.event.addDomListener(window, 'load', initialize); </script>

In my Java class, Google.java, I have the getGoogleClass() method that looks like this:

public String getGoogleClass() { if (htmloutput == null){ return ""; } return "map-canvas"; }

htmloutput is assigned a value in the activate() method so that when the user selects the asset, I grab its lat/long attributes. 

Problem I'm having is that the map does not show up after I select the asset. It only shows up if I reload the entire page. I believe this might be happening because when the page initially loads up, the getGoogleClass() returns nothing so document.getElementById('map-canvas') is empty. Is there a way to do this better such that the map shows up when the asset is selected the first time? 

 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by leeasling

When selecting the asset, is the author using the dialog?  It sounds like you could simply add an event listener to the edit configuration to refresh the page after editing.

https://docs.adobe.com/docs/en/cq/5-5/developing/components/edit_config.html#cq:listeners

1 reply

leeaslingAccepted solution
New Participant
October 16, 2015

When selecting the asset, is the author using the dialog?  It sounds like you could simply add an event listener to the edit configuration to refresh the page after editing.

https://docs.adobe.com/docs/en/cq/5-5/developing/components/edit_config.html#cq:listeners