The documentation for the Google Maps JavaScript API v3 Utilities mentions that MapLabel is included in packages that have not been officially published and are not actively maintained. If you want to prioritize its inclusion, you can open an issue on the GitHub link provided.
An alternative solution is to use a Marker object with an invisible image as an icon and then utilize the label property to display the desired label within your polygon. This gives the appearance of the label belonging to the polygon itself.
Below is a code snippet demonstrating this workaround, along with some sample code modified for the drawPolygon function. The image used ("https://ibb.co/cJpsnpb") serves as an invisible marker:
drawPolygon() {
const lat_lng = { lat: 66.7225378417214, lng: -155.04016191014387 };
const map = new google.maps.Map(
document.getElementById('map') as HTMLElement,
{
center: lat_lng,
zoom: 7,
mapTypeControl: true,
mapTypeControlOptions: {
mapTypeIds: ['roadmap'],
},
}
);
var infowindow = new google.maps.InfoWindow();
this.mapIcons.data.forEach((poly, i) => {
let polygonsMap = new google.maps.Polygon({
map: map,
paths: poly.polygons,
strokeColor: '#FF8C00',
fillColor: '#FF8C00',
strokeOpacity: 0.8,
strokeWeight: 2,
});
google.maps.event.addListener(polygonsMap, 'click', function (event) {
infowindow.setContent(poly.locator);
infowindow.setPosition(event.latLng);
infowindow.open(map);
});
polygonsMap.setMap(map);
var bounds = new google.maps.LatLngBounds();
for (let i = 0; i < poly.polygons.length; i++) {
bounds.extend(poly.polygons[i]);
}
var myLatlng = bounds.getCenter();
const image =
"https://ibb.co/cJpsnpb";
const beachMarker = new google.maps.Marker({
position: myLatlng,
label: {
text: poly.name,
color: 'black',
fontSize: "48px"
},
map,
icon: image,
});
});
}