Pre*_*rem 7 javascript google-maps google-maps-api-3 google-maps-static-api
我正在尝试拍摄由谷歌地图上绘制的矩形包围的区域的快照.是否可以拍摄矩形下方区域的快照?我搜索了答案,但找不到任何有用的信息.
我试图通过指定地图中心,缩放级别,图像宽度和图像高度,使用静态地图API获取矩形区域的快照.如 https://maps.googleapis.com/maps/api/staticmap?center=CENTER OF THE RECTANGLE&zoom = ZOOM LEVEL of 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);
Run Code Online (Sandbox Code Playgroud)
现在我使用此URL下载了图像,
" https://maps.googleapis.com/maps/api/staticmap?center="+ centre.lat()+","+ centre.lng()+"&zoom ="+ zoom +"&size ="+ width +"x"+高度+"&maptype = satellite&key = API_KEY"
我得到了图像,但图像不包括矩形所包围的整个区域(它太小).我做错了什么?有没有其他方法可以做到这一点?
您代码中的主要错误是静态地图 API 的width和height参数必须以像素为单位。目前,您以米为单位计算距离,并将其传递到静态地图 URL(而不是像素)中。
我根据您的示例代码创建了一个示例,并且能够创建正确的静态地图 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";
});
}Run Code Online (Sandbox Code Playgroud)
#map {
height: 100%;
}
html, body {
height: 95%;
margin: 0;
padding: 0;
}Run Code Online (Sandbox Code Playgroud)
<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> Run Code Online (Sandbox Code Playgroud)
此示例也可在 jsbin 中找到:http ://jsbin.com/humuko/edit?html,output
我希望这有帮助!
| 归档时间: |
|
| 查看次数: |
1074 次 |
| 最近记录: |