Переменная circle
должна быть определена в глобальном контексте, чтобы быть доступной в функциях прослушивания щелчков HTML.
// in global context.
var circle;
function fillMap(map, lat, lon) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lon),
map: map,
label: "My Marker"
});
circle = new google.maps.Circle({
strokeWeight: 1,
fillOpacity: 0.0,
map: map,
visible: false,
center: new google.maps.LatLng(lat, lon),
radius: 10000
});
подтверждение концепции скрипта
![screenshot of resulting map with circle](https://i.stack.imgur.com/pT43J.png)
var circle;
function fillMap(map, lat, lon) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lon),
map: map,
label: "My Marker"
});
circle = new google.maps.Circle({
strokeWeight: 1,
fillOpacity: 0.0,
map: map,
visible: false,
center: new google.maps.LatLng(lat, lon),
radius: 10000
});
map.fitBounds(circle.getBounds());
var infoWindow = new google.maps.InfoWindow({
position: marker.getPosition()
});
var theCircleButton = "<input type='button' onClick='circleVisible(circle)' class='btn' value='Show Circle'></input>";
infoWindow.setContent("<div style='text-align:center;'>" + theCircleButton + "</div>");
google.maps.event.addListener(marker, "click", function() {
infoWindow.open(map, marker);
});
}
function circleVisible(circle) {
circle.setVisible(true);
}
function circleInvisible(circle) {
circle.setVisible(false);
}
function initMap() {
// Latitude and Longitude for the centre of Australia
var centreAusLat = -28.080606;
var centreAusLon = 133.104225;
var zoomLevelAus = 4;
// the options used to create the Google Maps object
var mapOptions = {
// roughly the centre of Australia
center: new google.maps.LatLng(centreAusLat, centreAusLon),
// zoom level set to see all of Australia
zoom: zoomLevelAus,
// show a satellite image based map
mapTypeId: google.maps.MapTypeId.SATELLITE,
};
// create the Google Maps object
var map = new google.maps.Map(
document.getElementById('map-canvas'), mapOptions
);
fillMap(map, centreAusLat, centreAusLon);
}
html,
body,
#map-canvas {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map-canvas"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap" async defer></script>