Here’s what I have at the moment. Let me know if this is what you need. Currently, the following code allows for the following:
- Ability to retrieve long/lat coordinates from an address
- Map line items with coordinates to a Google Map
- When a map icon is clicked, it sends back data to Wix Code, which in my case I have used to populate a separate side menu corresponding to hte clicked item.
Page Code:
function mapItems (projItems) {
let itemsCount = projItems.length;
let locations = [];
let requests = 0;
for (var i = 0; i < itemsCount; i++) {
requests++;
locations.push({
"info": projItems[i]["oppTitle"],
"lat": projItems[i]["lat"],
"long":projItems[i]["long"],
"count": projItems[i]["_id"],
});
if (requests === itemsCount) {
$w("#html2").postMessage(locations);
}
}
}
HTML Code:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Google Maps APIs</title>
<script src="/path/to/markerwithlabel.js" type="text/javascript"></script>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div id="map"></div>
<script src="script.js"></script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key="APIKEY"&callback=initMap"></script>
</body>
</html>
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
<script>
window.onmessage = (event) => {
if (event.data) {
let receivedData = event.data;
initMap(receivedData);
}
};
function initMap(receivedData) {
var locations = receivedData;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: new google.maps.LatLng(35.223603, -80.836319),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow({});
// var marker, i;
// Upper Bound
var markerIcon = {
url: 'http://image.flaticon.com/icons/svg/252/252025.svg',
scaledSize: new google.maps.Size(40, 40),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(32,65)
};
var markerLabel = " "
// Lower Bound
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
map: map,
animation: google.maps.Animation.DROP,
icon: markerIcon,
position: new google.maps.LatLng(locations[i].lat, locations[i].long),
label: {
text: markerLabel,
fontSize: "16px",
fontWeight: "bold"
},
labelAnchor: new google.maps.Point(18, 12),
labelClass: "my-custom-class-for-label"
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i].info);
// infowindow.open(map, marker);
window.parent.postMessage(locations[i].count, "*");
}
})(marker, i));
}
}
</script>
You’ll also need to make sure the addresses in the dataset have longitude and latitude, if not here is the back-end code the take an address and find the longitude and latitude.
export function getLocationForAddress(address) {
var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=AIzaSyBFSeA4RlkyHhUudy7bQHwKEHrcc9WHDO8";
// console.log(url);
return fetch (url, {method: 'get'})
.then( (httpResponse) => {
if (httpResponse.ok) {
// console.log("HTTP OK");
return httpResponse.json();
}
});
}