我正在尝试拍摄谷歌地图上绘制的矩形所包围区域的快照。是否可以拍摄矩形下方区域的快照?我搜索了答案,但找不到任何有用的信息。
我尝试使用静态地图API通过指定地图中心,缩放级别,图像宽度和图像高度来获取矩形区域的快照。喜欢 https://maps.googleapis.com/maps/api/staticmap?center=CENTER的矩形& zoom = ZOOM LEVEL of MAP& size = WIGHTH 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);
现在我使用此网址
下载了图片“https://maps.googleapis.com/maps/api/staticmap?center=”+ centre.lat()+“,”+ centre.lng()+“& zoom =”+ zoom +“& size =”+ width +“x”+ height +“& maptype = satellite& key = API_KEY”
我得到了图像,但图像不包括矩形所包围的整个区域(它太小)。我做错了什么?还有其他方法吗?
答案 0 :(得分:4)
您在代码中遇到的主要错误是Static Maps API的width
和height
参数必须以像素为单位。目前,您计算以米为单位的距离,并将其传递给静态地图网址而不是像素。
我根据您的示例代码创建了一个示例,并且能够创建正确的静态地图URL。请看看我的例子。 distanceInPx
函数是最重要的。使用绘图管理器创建矩形,然后单击“显示静态地图”链接。另请注意,静态地图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
中找到我希望这有帮助!