我使用谷歌Mapjavascript绘制任何形状,在Map上,然后下载绘图部分使用downloadmap javascript功能。一切工作完美,但问题是绘制的部分是不完整的,下面的例子,它应该加入所有的角落,但它没有。它发生在几乎任何形状,我创建。
var map;
var drawingManager;
var selectedShape;
var shapes = [];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0, lng: 0},
zoom: 2
});
drawingManager = new google.maps.drawing.DrawingManager({
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: ['polygon']
}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
if (e.type != google.maps.drawing.OverlayType.MARKER) {
// Switch back to non-drawing mode after drawing is complete
drawingManager.setDrawingMode(null);
// Add shape to the array of shapes
var newShape = e.overlay;
newShape.type = e.type;
shapes.push(newShape);
// Set selected shape
selectedShape = newShape;
// Calculate area of the shape
var area = google.maps.geometry.spherical.computeArea(selectedShape.getPath());
$('#area').text('Area: ' + area.toFixed(2) + ' sq m');
// Add event listeners to the shape
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
}
});
// Set up event listeners on the clear button
$('#clear').click(function() {
clearSelection();
clearShapes();
$('#area').text('');
});
// Set up event listeners on the download button
$('#download').click(function() {
downloadMap();
});
// Wait for map to be fully loaded before triggering downloadMap function
google.maps.event.addListenerOnce(map, 'idle', function() {
downloadMap();
});
}
function setSelection(shape) {
clearSelection();
selectedShape = shape;
selectedShape.setEditable(true);
}
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
}
function clearShapes() {
for (var i = 0; i < shapes.length; i++) {
shapes[i].setMap(null);
}
shapes = [];
}
function downloadMap() {
var center = map.getCenter();
var zoom = map.getZoom();
var scale = 2;
var size = map.getDiv().clientWidth + 'x' + map.getDiv().clientHeight;
var mapTypeId = map.getMapTypeId();
var url = 'https://maps.googleapis.com/maps/api/staticmap?key=KEY¢er=' + center.lat() + ',' + center.lng() + '&zoom=' + zoom + '&size=' + size + '&scale=' + scale + '&maptype=' + mapTypeId;
// Add polygons to the URL
for (var i = 0; i < shapes.length; i++) {
if (shapes[i].type === google.maps.drawing.OverlayType.POLYGON) {
var path = shapes[i].getPath();
var polygons = [];
for (var j = 0; j < path.getLength(); j++) {
polygons.push(path.getAt(j).toUrlValue());
}
if (polygons.length > 0) {
url += '&path=' + polygons.join('|');
}
}
}
fetch(url)
.then(response => response.blob())
.then(blob => {
const form = new FormData();
form.append('image', blob, 'map.png');
// Send image data to server to save it
fetch('save_map.php', {
method: 'POST',
body: form
})
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.error(error));
})
.catch(error => console.error(error));
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=drawing"
<div id="map"></div>
<div id="area"></div>
<button id="clear">Clear</button>
<button id="download">Download</button>
这里是我的save_map.php
PHP代码
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$image = $_FILES['image'];
$directory = 'maps';
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
$filename = $directory . '/map_' . time() . '.png';
if (move_uploaded_file($image['tmp_name'], $filename)) {
echo 'Map saved successfully.';
} else {
echo 'Error saving map.';
}
} else {
echo 'Invalid request.';
}
?>
1条答案
按热度按时间wwwo4jvm1#
如果在您尝试导出为图像时多边形未闭合,则可能需要在迭代整个点集合后添加在数组末尾单击的第一个位置。
所以,当你建立网址修改这个:
到这个