Well, I know for a fact I am doing this incorrectly & it's going to be painfully obvious. I can see why (I think) but to be honest I just don’t “get it”.
I’m trying to style a Google map using Smart Map & JavaScript, but keep getting the JS error:
Uncaught ReferenceError: smartMap is not defined(…)
This is what my template looks like:
{# Google Map of location. #}
{% set options = {
height: 500,
width: '100%',
zoom: 15,
markerOptions: {
},
infoWindowOptions: {
maxWidth: 200
}
} %}
<div id="mapCanvas">
{{ craft.smartMap.map(product.address, options) }}
</div>
<script src="/assets/js/app.min.js"></script>
</body>
Within app.min.js I am doing this:
if ($('#mapCanvas').length) {
var styles = [{
"featureType": "all",
"elementType": "geometry",
"stylers": [{"visibility": "simplified"}, {"hue": "#ff7700"}]
},
...
smartMap.map['smartmap-mapcanvas-1'].setOptions({styles: styles});
}
I've been reading this post which literally has the answer to my problem I believe. I just don't understand how to tell the system where/what smartMap is.
Thank you for any suggestions!
EDIT
Here is what the rendered page looks like:
<script src="/assets/js/app.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript" src="http://mysite.dev/cpresources/smartmap/js/smartmap.js?d=1449217510"></script>
EDIT 2
Just for future reference in case someone else comes across this thread. It ended up being the way I was initializing my js file.
// This executes before we're ready
(function () {
'use strict';
...
})();
// This works
$(function () {
...
});
smartmap/js/smartmap.jsget loaded? Is it loading before, or afterassets/js/app.min.js? – Lindsey D Oct 29 '16 at 03:43