带孔的世界地图多边形(谷歌地图) [英] Polygon of world map with a hole (google maps)
问题描述
我正在尝试绘制一个带孔的矩形多边形.我的问题是我无法创建一个覆盖整个世界的多边形.多边形被反转,因此只选择一条线而不是整个世界.
I am trying to draw a rectangle polygon with a hole. My problem is that I cannot create a polygon that covers the whole world. The polygon is inverted so that only a single line is selected instead of the whole world.
下面是我能够做出的最大选择的示例.例如,如果我尝试将 0(在 new google.maps.LatLng(-85.1054596961173,0) 行中)更改为任何其他值,则使用反向选择.
Below an example of the maximum selection I am able to make. For example if I try to alter 0 (in the line new google.maps.LatLng(-85.1054596961173,0)) to any other value the inverted selection is used.
也许我在这里遗漏了一些明显的东西,但我似乎无法让它工作.
Maybe I am missing something obvious here, but I can't seem to get it working.
function initialize() {
var mapOptions = {
zoom: 1,
center: new google.maps.LatLng(24.886436490787712, -70.2685546875),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var worldCoords = [
new google.maps.LatLng(-85.1054596961173, -180),
new google.maps.LatLng(85.1054596961173, 180),
new google.maps.LatLng(85.1054596961173, 180),
new google.maps.LatLng(-85.1054596961173,0)
];
var EuropeCoords = [
new google.maps.LatLng(29.68224948021748, -23.676965750000022),
new google.maps.LatLng(29.68224948021748, 44.87772174999998),
new google.maps.LatLng(71.82725578445813, 44.87772174999998),
new google.maps.LatLng(71.82725578445813, -23.676965750000022)
];
// Construct the polygon.
poly = new google.maps.Polygon({
paths: [worldCoords,EuropeCoords],
strokeColor: '#000000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#000000',
fillOpacity: 0.35
});
poly.setMap(map);
}
这是小提琴:http://jsfiddle.net/xs9fcdf9/4/>
推荐答案
需要使外多边形的缠绕方向与内多边形的缠绕方向相反并添加一个点:
You need to make the winding direction of the outer polygon opposite that of the inner polygon and add a point:
var worldCoords = [
new google.maps.LatLng(-85.1054596961173, -180),
new google.maps.LatLng(85.1054596961173, -180),
new google.maps.LatLng(85.1054596961173, 180),
new google.maps.LatLng(-85.1054596961173, 180),
new google.maps.LatLng(-85.1054596961173, 0)
];
工作代码片段:
function initialize() {
var mapOptions = {
zoom: 1,
center: new google.maps.LatLng(24.886436490787712, -70.2685546875),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var worldCoords = [
new google.maps.LatLng(-85.1054596961173, -180),
new google.maps.LatLng(85.1054596961173, -180),
new google.maps.LatLng(85.1054596961173, 180),
new google.maps.LatLng(-85.1054596961173, 180),
new google.maps.LatLng(-85.1054596961173, 0)];
var EuropeCoords = [
new google.maps.LatLng(29.68224948021748, -23.676965750000022),
new google.maps.LatLng(29.68224948021748, 44.87772174999998),
new google.maps.LatLng(71.82725578445813, 44.87772174999998),
new google.maps.LatLng(71.82725578445813, -23.676965750000022)];
// Construct the polygon.
poly = new google.maps.Polygon({
paths: [worldCoords, EuropeCoords],
strokeColor: '#000000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#000000',
fillOpacity: 0.35
});
poly.setMap(map);
}
initialize();
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas" style="width:500px;height:500px;"></div>
这篇关于带孔的世界地图多边形(谷歌地图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!