Я создал круг на картах Google, а затем получил его границы, которые возвращают квадратные ограничивающие прямоугольники.
function getBounds(latLng, radius) {
/*
* Params:
* latLng: google maps LatLng object or object literal // example: { lat: -34.397, lng: 150.644 }
* radius: Number(int) in Miters. For example 1000 will be 1 sq Kilometer
* Returns:
* object literal in the following format:
* {
northEast: {
lat: (float),
lng: (float),
},
southWest: {
lat: (float),
lng: (float),
},
}
* */
const circle = new google.maps.Circle({
center: latLng,
radius: radius
});
const bounds = circle.getBounds();
const northEast = bounds.getNorthEast();
const southWest = bounds.getSouthWest();
return {
northEast: {
lat: northEast.lat(),
lng: northEast.lng(),
},
southWest: {
lat: southWest.lat(),
lng: southWest.lng(),
},
};
}
Я взял библиотеку, чтобы добавить короткий фрагмент кода, чтобы вы могли убедиться, что это правильный ответ для того, что вы искали.После нажатия на карту вы увидите маркер и треугольник вокруг него.
Примечание : скопируйте код и добавьте свой ключ API карты Google. Без вашего ключа API карта не загрузится.
let map;
let marker;
let rectangle;
function initMap() {
const latLng = {
lat: -34.397,
lng: 150.644
};
const radius = 1000;
map = new google.maps.Map(document.getElementById('map'), {
center: latLng,
zoom: 11,
});
google.maps.event.addListener(map, 'click', function(event) {
const location = event.latLng;
const bounds = getBounds(location, 1000);
console.log(bounds);
addRectangle(bounds);
addMarker(location);
});
}
function addMarker(location) {
if (marker) {
marker.setMap(null);
}
marker = new google.maps.Marker({
position: location,
map: map,
});
}
function addRectangle(bounds) {
if (rectangle) {
rectangle.setMap(null);
}
rectangle = new google.maps.Rectangle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
bounds: {
north: bounds.northEast.lat,
south: bounds.southWest.lat,
east: bounds.northEast.lng,
west: bounds.southWest.lng,
},
});
}
function getBounds(latLng, radius) {
/*
* Params:
* latLng: google maps LatLng object or object literal // example: { lat: -34.397, lng: 150.644 }
* radius: Number(int) in Miters. For example 1000 will be 1 sq Kilometer
* Returns:
* object literal in the following format:
* {
northEast: {
lat: (float),
lng: (float),
},
southWest: {
lat: (float),
lng: (float),
},
}
* */
const circle = new google.maps.Circle({
center: latLng,
radius: radius
});
const bounds = circle.getBounds();
const northEast = bounds.getNorthEast();
const southWest = bounds.getSouthWest();
return {
northEast: {
lat: northEast.lat(),
lng: northEast.lng(),
},
southWest: {
lat: southWest.lat(),
lng: southWest.lng(),
},
};
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>
<div id="map"></div>