親愛的所有人,請幫助我以下。我在下面找到了一段代碼,併爲我自己的使用添加了一點。現在我輸入以下問題。正如你所看到的,我使用尺寸爲100 x 100 px而不是普通的256 x 256 px瓷磚的瓷磚。Maps API更改瓷磚尺寸導致錯誤座標
地圖本身看起來不錯,但座標有點搞砸了。例如,您可以看到我必須將地圖居中放置lat:73以獲得中心的「赤道」。你也可以看到,如果在y方向上滾動,我構造的三角形不會在地圖上的相同位置重複。你必須在世界各地滾動2.56次才能找到下一個三角形,而不是在精確的1次之後找到它。
現在的問題是,我可以以某種方式修復座標設置嗎?或者我可以以某種方式使用標準化的x和y座標放置我的三角形?
的地圖可以在magtest.r4u.nl
(我知道需要清理一些。註釋的代碼,如「明月」都是原代碼直接拷貝,因爲我發現它在網絡上)
查看function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 73, lng: 0},
zoom: 3,
streetViewControl: false,
mapTypeControlOptions: {
mapTypeIds: ['moon']
}
});
// Define the LatLng coordinates for the polygon's path.
var triangleCoords = [
{lat: 85, lng: 0},
{lat: 85, lng: 30},
{lat: 37, lng: 10}
];
var triangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
triangle.setMap(map);
var moonMapType = new google.maps.ImageMapType({
getTileUrl: function(coord, zoom) {
var normalizedCoord = getNormalizedCoord(coord, zoom);
if (!normalizedCoord) {
return null;
}
var bound = Math.pow(2, zoom);
return '//magtest.r4u.nl/map/' + zoom + '/' + normalizedCoord.x + '/' + (bound - normalizedCoord.y - 1) + '.png';
},
tileSize: new google.maps.Size(100, 100),
maxZoom: 14,
minZoom: 3,
radius: 10000,
name: 'Moon'
});
map.mapTypes.set('moon', moonMapType);
map.setMapTypeId('moon');
}
// Normalizes the coords that tiles repeat across the x axis (horizontally)
// like the standard Google map tiles.
function getNormalizedCoord(coord, zoom) {
var y = coord.y;
var x = coord.x;
// tile range in one direction range is dependent on zoom level
// 0 = 1 tile, 1 = 2 tiles, 2 = 4 tiles, 3 = 8 tiles, etc
var tileRange = 1 << zoom;
// don't repeat across y-axis (vertically)
if (y < 0 || y >= tileRange) {
return null;
}
// repeat across x-axis
if (x < 0 || x >= tileRange) {
x = (x % tileRange + tileRange) % tileRange;
}
return {x: x, y: y};
}
我試圖在代碼中的其他地方放置tilesize語句,但沒有奏效。還有其他建議嗎? –