我需要通过 Google map 提供的标准卫星图像显示几百到也许一千张高分辨率航空照片。这些图像在地理上是分散的,因此我决定将图 block 服务器实现为通用 asp.net 处理程序(*.ashx 文件)。我将根据 Google 开发者网站上显示的 map 来描述问题,网址为:
https://developers.google.com/maps/documentation/javascript/examples/maptype-overlay
一切或多或少都正常,但我遇到以下两个问题:
1) 选择“卫星” map 类型后,将鼠标悬停在该按钮上会生成一个带有名为“标签”复选框的下拉列表。如何在标题为“航空照片”的下拉列表中添加另一个复选框,以打开/关闭叠加层?我是否必须对利用 Google map 实现细节的 JQuery 黑客进行硬编码,还是可以通过 API 来完成此任务?
2) 如果指定的图 block 不存在,我的 *.ashx 处理程序将返回图像或状态 204(无内容)。问题是 204 结果没有缓存,所以每次我缩小并返回到同一位置时,我的服务器都会重新访问客户端应该已经知道不存在的所有图 block 。我没有看到它记录了图 block 服务器应该为这样的“空”图 block 返回什么,以便客户端可以缓存结果。如果没有特定位置的 map 图 block ,我应该返回什么?
谢谢。
最佳答案
鉴于此问题缺乏回应,很明显稀疏切片服务器是一种不常见的做法。以下是这两个问题的解决方案(无论它可能多么hacky):
1) 如何向“卫星”下拉列表添加复选框以切换 map 图层?不幸的是,没有支持的方法来做到这一点,所以我想出了以下令人难以置信的黑客代码:
// Create a function to select the "Labels" checkbox
var getLabelButton = function() {
return $('.gm-style-mtc:last > div:last > div:last');
};
// Clone the "Labels" checkbox used to show/hide the hybrid map overlay
var labelBtn = getLabelButton();
var labelClone = labelBtn.clone();
// Change the display and hover text for the new button
labelClone.prop('title', Localizer.GetString('CustomImagery.Description'));
labelClone.find('label').html(Localizer.GetString('CustomImagery.Name'));
// Highlight the button when the client hovers the mouse over it
var checkbox = labelClone.children('span');
var prevBackColor = labelClone.css('background-color');
var prevBorderColor = checkbox .css('border-color');
labelClone.hover(function() {
labelClone.css('background-color', '#EBEBEB');
checkbox .css('border-color' , '#666');
}, function() {
labelClone.css('background-color', prevBackColor);
checkbox .css('border-color' , prevBorderColor);
});
// Set the checkmark image source to be the correct value, instead of transparent
var checkmark = checkbox .children('div');
var checkmarkImg = checkmark.children('img');
checkmarkImg.attr('src', 'https://maps.gstatic.com/mapfiles/mv/imgs8.png');
// Attach the new checkbox after the Labels checkbox
labelBtn.after(labelClone);
// Create a method to determine if the selected map type supports custom imagery
var mapTypesSupportingCustomImagery = [
google.maps.MapTypeId.SATELLITE,
google.maps.MapTypeId.HYBRID
];
var isImagerySupportedOnSelectedMapType = function() {
var mapTypeId = googleMap.getMapTypeId();
return (0 <= mapTypesSupportingCustomImagery.indexOf(mapTypeId));
};
// Show the checkmark and imagery if the initial map type supports it
if (isImagerySupportedOnSelectedMapType()) {
checkmark.css('display', '');
googleMap.overlayMapTypes.push(tileServer);
}
// Show/hide the checkmark and imagery when the user clicks on the checkbox
labelClone.on('click', function() {
var showImagery = (checkmark.css('display') === 'none');
if (showImagery) {
checkmark.css('display', '');
googleMap.overlayMapTypes.push(tileServer);
} else {
checkmark.css('display', 'none');
var tileServerIndex = googleMap.overlayMapTypes.indexOf(tileServer);
googleMap.overlayMapTypes.removeAt(tileServerIndex);
}
});
// Create a function that returns whether the custom imagery should be displayed
var displayCustomImagery = function() {
return (isImagerySupportedOnSelectedMapType() && checkmark.css('display') != 'none');
};
// Add an event listener to add the tile server when displaying satellite view
google.maps.event.addListener(googleMap, 'maptypeid_changed', function() {
var tileServerIndex = googleMap.overlayMapTypes.indexOf(tileServer);
if (displayCustomImagery()) {
if (tileServerIndex < 0) {
googleMap.overlayMapTypes.push(tileServer);
}
} else if (0 <= tileServerIndex) {
googleMap.overlayMapTypes.removeAt(tileServerIndex);
}
});
在上面的代码中,googleMap 是 map 对象,tileServer 是我对 google.maps.ImageMapType 对象的实现。
2)我应该返回什么来代表空图 block ?
我对这个问题的解决方案相当清晰。我只是列出了服务器上所有图 block 的文件名,这些文件名是所请求的图 block 的 Morton 编号的 base-4 编码。然后我将此列表作为从字符串到 bool 值的字典发送给客户端(始终为真)。客户端在发出请求之前只需检查服务器是否包含 map 图 block ,因此服务器不必担心返回什么(如果发出无效请求,我将其保留为返回 204 错误)。 getTileUrl 方法中获取图 block 名称的 JavaScript 如下:
function(coord, zoom) {
// Return null if the zoom level is not supported
// NOTE: This should not be necessary, but minZoom and
// maxZoom parameters are ignored for image map types
if (zoom < minZoom || maxZoom < zoom) {
return null;
}
// Get the name of the map tile being requested
var tileName = '';
var y = coord.y << 1;
for (var shift = zoom - 1; 0 <= shift; --shift) {
var digit = (coord.x >>> shift) & 1;
digit |= ( y >>> shift) & 2;
tileName += digit;
}
// Return if the map tile being requested does not exist
if (!mapTiles[tileName]) {
return null;
}
// Return the url to the tile server
...
}
关于javascript - 自定义 map 图 block 叠加问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28099655/