I am trying to write content to a google maps control div, my prefered way being to use jquery selector with div id.
The problem is that the map only seems to take the actual element referenced by variable to change it, instead of its ID.
This is exampled by the bottom control ‘testcontrol_2’ able to have its background changed, however ‘testcontrol_1’ referenced by ID remains white.
My question is why am I able to write content to the map controls with an actual html element as a jQuery selector, but not an element id.
Fiddle: https://jsfiddle.net/Tim_H/pgb9z2v1/2/
Code:
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDbFd34Z8SoF6Q9v3RQnkwy5nDDw09tvww"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<style>
.mapframe {
width: 100%;
height: 50em;
}
.mapcontrol {
padding: 1em;
background-color: #ffffff;
}
</style>
</head>
<body>
<div id="mapframe" class="mapframe"></div>
<script>
var map;
var centerControlContainer;
var centerControlId = 'mainCenterControlFrame';
var testControl2;
function mapinit() {
let mapinit = {
center: new google.maps.LatLng(51.509865, -0.118092),
zoom: 10,
minzoom: 15,
maxzoom: 21,
};
map = new google.maps.Map(document.getElementById('mapframe'), mapinit);
google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
GMapready();
});
}
function GMapready() {
centerControlContainer = document.createElement('div');
$(centerControlContainer).addClass('mapControlContainer');
$(centerControlContainer).attr('id', centerControlId);
$(centerControlContainer).html('<div id="testcontrol_1" class="mapcontrol">TestControl1</div>');
testControl2 = document.createElement('div');
$(testControl2).attr('id', 'testcontrol_2');
$(testControl2).html('TestControl2');
$(testControl2).addClass('mapcontrol');
$(centerControlContainer).append(testControl2);
this.map.controls[google.maps.ControlPosition.TOP_CENTER].push(this.centerControlContainer);
$('#testcontrol_1').css('background-color', '#ff0000');
$(testControl2).css('background-color', '#ff0000');
}
mapinit();
</script>
</body>
</html>
2
Answers
Consider the following example: https://jsfiddle.net/Twisty/og7tru4m/
JavaScript
As you suggested, Google JavaScript will not work well with jQuery Objects, yet this is not an issue. When you create jQuery Objects, you can still pass along the underlying HTML Element or DOM Element. There are a few ways to do this.
One way is to use
.get()
(See More), which returns the DOM Element at a specific index.Using
.get()
is preferred.In this way, you can create and manage jQuery Objects and still use them with your Google Maps.
You can also make use of jQuery’s ability to create DOM Elements on the fly.
See More: https://api.jquery.com/jQuery/#jQuery-html-attributes and https://learn.jquery.com/using-jquery-core/jquery-object/
One of the main benefits to jQuery is Chaining.
See More: https://learn.jquery.com/using-jquery-core/working-with-selections/
The
testcontrol_1
isn’t changing because it isn’t part of the DOM until after you call:$('#testcontrol_1').css('background-color', '#ff0000');
.If you do this:
to give the process a change to render it in the DOM, then jquery will be able to find it.
proof of concept fiddle
code snippet: