gpt4 book ai didi

javascript - 在谷歌地图javascript下拍摄特定区域的快照

转载 作者:数据小太阳 更新时间:2023-10-29 04:45:16 27 4
gpt4 key购买 nike

我正在尝试拍摄由在谷歌地图上绘制的矩形包围的区域的快照。是否可以拍摄矩形下方区域的快照?我搜索了答案,但找不到任何有用的信息。

Rectangle drawn on the map

我尝试通过指定 map 中心、缩放级别、图像宽度和图像高度,使用静态 map API 拍摄矩形下方区域的快照。像 https://maps.googleapis.com/maps/api/staticmap?center=CENTER OF THE RECTANGLE&zoom=ZOOM LEVEL OF THE MAP&size=WIDTH AND HEIGHT OF THE RECTANGLE&maptype=satellite&key=API KEY

这是我试过的代码,

var zoom = map.zoom;
var centre = rectangle.getBounds().getCenter(); //rectangle is the shape drawn on the map
var spherical = google.maps.geometry.spherical;
bounds = rectangle.getBounds(); //rectangle is the shape drawn on the map
cor1 = bounds.getNorthEast();
cor2 = bounds.getSouthWest();
cor3 = new google.maps.LatLng(cor2.lat(), cor1.lng());
cor4 = new google.maps.LatLng(cor1.lat(), cor2.lng());
width = spherical.computeDistanceBetween(cor1,cor3);
height = spherical.computeDistanceBetween( cor1, cor4);

现在我使用这个 URL 下载了图片,

"https://maps.googleapis.com/maps/api/staticmap?center= "+ center.lat() + ","+ center.lng() + "&zoom="+ zoom + "&size="+ width + "x"+ height + "&maptype=satellite&key= API_KEY”

原始网址:“https://maps.googleapis.com/maps/api/staticmap?center=40.804197008355914,-74.11213619168848&zoom=20&size=26x37&maptype=satellite&key=API_KEY

Output image I got

My expected output

我得到了图像,但是图像不包括矩形包围的整个区域(它太小了)。我做错了什么?还有其他方法吗?

最佳答案

代码中的主要错误是静态 map API 的 widthheight 参数必须以像素为单位。目前,您以米为单位计算距离,并将其传递到静态 map URL 而不是像素。

我根据您的示例代码创建了一个示例,并且能够创建正确的静态 map URL。请看看我的例子。 distanceInPx 函数是最重要的。使用绘图管理器创建矩形并单击显示静态 map 链接。另请注意,静态 map API 在标准计划中限制为 640x640 像素的图像大小,因此您无法为尺寸更大的矩形创建链接。

function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 28.45765, lng: -16.363564},
zoom: 21,
mapTypeId: google.maps.MapTypeId.SATELLITE
});

var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.RECTANGLE,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: ['rectangle']
}
});
drawingManager.setMap(map);

google.maps.event.addListener(drawingManager, "rectanglecomplete", function(rectangle){
function distanceInPx(pos1, pos2) {
var p1 = map.getProjection().fromLatLngToPoint(pos1);
var p2 = map.getProjection().fromLatLngToPoint(pos2);

var pixelSize = Math.pow(2, -map.getZoom());

var d = Math.sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y))/pixelSize;

return Math.round(d);
}

var zoom = map.zoom;
var centre = rectangle.getBounds().getCenter(); //rectangle is the shape drawn on the map
var spherical = google.maps.geometry.spherical;
bounds = rectangle.getBounds(); //rectangle is the shape drawn on the map
var cor1 = bounds.getNorthEast();
var cor2 = bounds.getSouthWest();
var cor3 = new google.maps.LatLng(cor2.lat(), cor1.lng());
var cor4 = new google.maps.LatLng(cor1.lat(), cor2.lng());

var width = distanceInPx(cor1, cor4);
var height = distanceInPx(cor1, cor3);

var imgUrl = "https://maps.googleapis.com/maps/api/staticmap?center=" +
centre.lat() + "," + centre.lng() + "&zoom=" + zoom +
"&size=" + width + "x" + height + "&maptype=satellite&key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU";

var aElem = document.getElementById("staticLink");
aElem.setAttribute("href", imgUrl);
aElem.style.display="block";

});

}
#map {
height: 100%;
}
html, body {
height: 95%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<a id="staticLink" href="#" target="_blank" title="Show static map" style="display:none;">Show static map</a>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&libraries=drawing&callback=initMap"
async defer></script>

这个例子也可以在 jsbin 中找到:http://jsbin.com/humuko/edit?html,output

希望对您有所帮助!

关于javascript - 在谷歌地图javascript下拍摄特定区域的快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47747667/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com