I need to draw multiple shapes on a map upon initialization and then add event listeners to see if the shapes have been modified or clicked on.
If I draw a single shape, the below code works properly:
// 1
var polygon = new google.maps.Polygon({
id: '1',
paths: [
{lat: 38.56080094343725, lng: -121.74302101135254},
{lat: 38.56106940317395, lng: -121.76750421524048},
{lat: 38.57249479108347, lng: -121.76737546920776},
{lat: 38.57244446296193, lng: -121.75782680511475},
{lat: 38.57595057110021, lng: -121.75774097442627},
{lat: 38.57583314441062, lng: -121.74778461456299}
],
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: '#FF0000',
fillOpacity: 0.1
});
polygon.setMap(map);
google.maps.event.addListener(polygon, 'click', function() {
console.log(polygon.id);
});
google.maps.event.addListener(polygon.getPath(), "insert_at", getPath);
google.maps.event.addListener(polygon.getPath(), "remove_at", getPath);
google.maps.event.addListener(polygon.getPath(), "set_at", getPath);
function getPath() {
var shapeCoords = '';
for (var i =0; i < polygon.getPath().getLength(); i++) {
var xy = polygon.getPath().getAt(i);
shapeCoords += '{lat: ' + xy.lat() + ', lng: ' + xy.lng() + '},\n';
}
console.log(shapeCoords);
console.log(polygon.id);
}
When I draw any additional shapes, I have to duplicate all of the click events and change the variable name (e.g. to polygon2
).
// 2
var polygon2 = new google.maps.Polygon({
id: '2',
paths: [
{lat: 38.56075060712497, lng: -121.76754713058472},
{lat: 38.5605157038683, lng: -121.74289226531982},
{lat: 38.55172305844243, lng: -121.7401671409607},
{lat: 38.550665854985745, lng: -121.74636840820312},
{lat: 38.54645366782474, lng: -121.745445728302},
{lat: 38.54636975720739, lng: -121.74585342407227},
{lat: 38.546302628643, lng: -121.74879312515259},
{lat: 38.54655436043634, lng: -121.76778316497803}
],
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: '#FF0000',
fillOpacity: 0.1
});
polygon2.setMap(map);
google.maps.event.addListener(polygon2, 'click', function() {
console.log(polygon2.id);
});
google.maps.event.addListener(polygon2.getPath(), "insert_at", getPath2);
google.maps.event.addListener(polygon2.getPath(), "remove_at", getPath2);
google.maps.event.addListener(polygon2.getPath(), "set_at", getPath2);
function getPath2() {
var shapeCoords = '';
for (var i =0; i < polygon2.getPath().getLength(); i++) {
var xy = polygon2.getPath().getAt(i);
shapeCoords += '{lat: ' + xy.lat() + ', lng: ' + xy.lng() + '},\n';
}
console.log(shapeCoords);
console.log(polygon2.id);
}
Is there any way to use the same getPath function and/or event listeners so I don't need to duplicate the same code for each shape?