我想在X米的中心点(LatLng)周围创建一个正方形(LatLngBounds)。
答案 0 :(得分:10)
使用L.LatLng.toBounds()
method,例如
var center = L.latLng(40,-3);
var bounds = center.toBounds(500);
或
var bounds = L.latLng(40,-3).toBounds(500)
答案 1 :(得分:2)
以下解决方案演示了如何绘制这样的正方形:
实施例
function drawSquare(map, center, properties, sideLengthInMeters){
var circle = L.circle(center, sideLengthInMeters/2).addTo(map);
var bounds = circle.getBounds();
map.removeLayer(circle); //hide circle
var rect = L.rectangle(bounds, properties).addTo(map);
return rect;
}
<强>演示强>
var center = [59.33,18.02];
// Create the map
var map = L.map('map').setView(center, 10);
// Set up the OSM layer
L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
{ maxZoom: 18 }).addTo(map);
L.marker(center).addTo(map);
var sideInMeters = 10000;
drawSquare(map,center,{ color: 'blue', weight: 1 }, sideInMeters);
function drawSquare(map, center, properties, sideLengthInMeters){
var circle = L.circle(center, sideLengthInMeters/2).addTo(map);
var bounds = circle.getBounds();
map.removeLayer(circle); //hide circle
var rect = L.rectangle(bounds, properties).addTo(map);
return rect;
}
#map {
height: 400px;
}
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="app.js"></script>
<div id="map"></div>