问题
I am using Google maps (via OpenStreetMap) and need to have world repeated horizontally. I thought its by default, but it is not.
var map;
map = new google.maps.Map(element, {
center : new google.maps.LatLng(mapa_start_X, mapa_start_Y),
zoom : mapa_start_Z,
mapTypeId: "OSM",
mapTypeControl: false,
streetViewControl: true
});
map.mapTypes.set("OSM", new google.maps.ImageMapType({
getTileUrl: function(coord, zoom) {
return "http://tile.openstreetmap.org/" + zoom + "/" + coord.x + "/" + coord.y + ".png";
//return "/tile.php?z=" + zoom + "&x=" + coord.x + "&y=" + coord.y;
},
tileSize: new google.maps.Size(256, 256),
name: "OpenStreetMap",
maxZoom: 18
}));
element
is object in DOM, mapa_start_X
, mapa_start_Y
and mapa_start_Z
are variables defined in other part of code.
What should I add to the constructor of the map?
DEMO
回答1:
You have to change the getTileUrl function to normalize the coordinates in the x direction like the example in the documentation.
map.mapTypes.set("OSM", new google.maps.ImageMapType({
getTileUrl: function (coord, zoom) {
var normalizedCoord = getNormalizedCoord(coord, zoom);
if (!normalizedCoord) {
return null;
}
return "http://tile.openstreetmap.org/" + zoom + "/" + normalizedCoord.x + "/" + normalizedCoord.y + ".png";
},
tileSize: new google.maps.Size(256, 256),
name: "OpenStreetMap",
maxZoom: 18,
minZoom: 1
}));
// 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
};
}
working fiddle
来源:https://stackoverflow.com/questions/28947413/how-to-enable-horizontally-world-repeat-in-google-maps