简体   繁体   English

Google Maps API v3 ImageMapType

[英]Google Maps API v3 ImageMapType

I'm following the Google Maps API v3 ImageMapType example here: https://developers.google.com/maps/documentation/javascript/examples/maptype-image 我在这里遵循Google Maps API v3 ImageMapType示例: https : //developers.google.com/maps/documentation/javascript/examples/maptype-image

But, I don't have a clear understanding from the documentation how the tiles/zoom work. 但是,我没有从文档中清楚地了解磁贴/缩放的工作方式。 For now, I'm just trying to get it to work with 0 zoom. 现在,我只是试图使其在0缩放下工作。 Once I tackle that, then I can figure out the zoom piece. 一旦解决了这个问题,便可以找出变焦片。

My image is 2000px X 2000px. 我的图片是2000px X 2000px。 I've sliced it up into 8 tiles by 8 tiles at 250px X 250px per tile. 我将其切成8块乘8块,每块像素为250px X 250px。

I am doing console.log on getTileUrl. 我正在getTileUrl上做console.log。 I was expecting to see all 64 of my tiles loaded from 0-0.png to 7-7.png But, I'm seeing 0-0.png attempt to load nine times. 我原本希望看到所有64个图块从0-0.png加载到7-7.png但是,我看到0-0.png尝试加载9次。

I've created this http://jsfiddle.net/2N2sy/1/ (code below) to simulate my code. 我创建了这个http://jsfiddle.net/2N2sy/1/ (下面的代码)来模拟我的代码。

Help unraveling the tiles/zoom would be greatly appreciated! 帮助解开瓷砖/缩放将不胜感激!

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
    };
}

var map;

function initMaps() {

    $.getScript("http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js").done(function (script, textStatus) {

        var customMapTypeOptions = {
            getTileUrl: function (coord, zoom) {
                var normalizedCoord = getNormalizedCoord(coord, zoom);
                if (!normalizedCoord) {
                    return null;
                }
                var bound = Math.pow(2, zoom);
                console.log('http://img.photobucket.com/albums/v229/s15199d/ + normalizedCoord.x + '-' + (bound - normalizedCoord.y - 1) + '.png');
                return 'http://img.photobucket.com/albums/v229/s15199d/ + normalizedCoord.x + '-' + (bound - normalizedCoord.y - 1) + '.png';
            },
            tileSize: new google.maps.Size(250, 250),
            maxZoom: 0,
            minZoom: 0,
            radius: 1738000,
            name: 'custom map'
        };

        var customMapType = new google.maps.ImageMapType(customMapTypeOptions);

        var latlng = new google.maps.LatLng(0, 0), // center point

        mapOptions = {
            zoom: 0,
            center: latlng,
            draggable: true,
            scrollwheel: false,
            mapTypeControl: false,
            panControl: false,
            scaleControl: false,
            zoomControl: true,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.LARGE,
                position: google.maps.ControlPosition.RIGHT_TOP
            },
            streetViewControl: false,
            streetViewControlOptions: {
                position: google.maps.ControlPosition.RIGHT_TOP
            },        
            mapTypeControlOptions: {
                mapTypeIds: ['custom map']
            }
        };

        map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

        map.mapTypes.set('custom map', customMapType);
        map.setMapTypeId('custom map');

    });
}

$(function () {
    if (window.google && google.maps) {
        //alert("Map script is already loaded. Initializing");
        initMaps();
    } else {
        //alert("Lazy loading Google map...");
        lazyLoadGoogleMap();
    }

});

function lazyLoadGoogleMap() {
    $.getScript("http://maps.google.com/maps/api/js?sensor=true&callback=initMaps")
    .done(function (script, textStatus) {
        //alert("Google map script loaded successfully");
    })
    .fail(function (jqxhr, settings, ex) {
        //alert("Could not load Google Map script: " + jqxhr);
    });
}

You're telling it to do so. 您是在告诉它这样做。 If you look at your getNormalizedCoord function, you'll see that you're expecting a tileRange of 1. Then you check the x coordinate against the tileRange . 如果查看getNormalizedCoord函数,则会看到tileRange期望为1。然后根据tileRange检查x坐标。 Since that is 1, all values above or equal to 1 will be normalized down (only one option below 1, and that's zero). 由于该值为1,所有大于或等于1的值都将被归一化(只有一个选项低于1,则为零)。

If you follow the logic it does this: 如果遵循逻辑,它将执行以下操作:

  1. Set x to 1 % 1 (that's 0) x设置为1 % 1 (即0)
  2. Set x to 0 + 1 (that's 1) x设置为0 + 1 (即1)
  3. Set x to 1 % 1 (that's 0 again) x设置为1 % 1 (再次为0)

So at the zoom level you chose, the function is rightfully always returning 0. If you try a bigger zoom level, you will notice it does load your other tiles. 因此,在您选择的缩放级别上,该函数正确地始终返回0。如果尝试更大的缩放级别,您会注意到它确实会加载其他图块。

One of the mistakes you're making (I think), is that you got the zoom levels backwards: 0 is fully zoomed out and 9 is fully zoomed in. The example you used is based around a single overview image for zoom level 0 and smaller chunks on every step further zoomed in. Therefor you should be expecting one tile (0, 0) being loaded at zoom level 0. To load all of the 64 tiles, you will need a higher zoom level. 您犯的一个错误(我认为)是,您将缩放级别向后移动:0完全缩小,9完全放大。您使用的示例基于缩放级别为0的单个概图和每个步骤上的较小块都会进一步放大。因此,您应该期望以0级缩放级别加载一个图块(0,0)。要加载所有64个图块,则需要更高的缩放级别。 The multiple calls to the same url is simply the API using its cache to fill in the tiles. 对同一URL的多次调用只是使用其缓存填充图块的API。 If you look at the example, it is just repeating the same image along the X axis there as well. 如果您查看该示例,它也只是沿X轴重复相同的图像。


To recap what is discussed in the comments, the zoom level at which the normalization function used will load all tiles is 3. Right now, the tiles are being loaded with the y axis flipped. 回顾一下评论中讨论的内容,使用归一化功能加载所有图块的缩放级别为3。现在,正在加载y轴翻转的图块。 To fix this, you will have to change the following line in getTileUrl from (bound - normalizedCoord.y - 1) to (normalizedCoord.y - 1) . 要解决此问题,您必须将getTileUrl的以下行从(bound - normalizedCoord.y - 1)更改为(normalizedCoord.y - 1)

The whole idea of zooming in Google Maps is based on loading images with a higher level of detail for the same tile grid, giving the idea of being zoomed in further. 放大Google Maps的整个想法是基于为同一图块网格加载具有更高细节水平的图像,从而提供了进一步放大的想法。 To achieve this, you will therefor need to produce extra images. 为此,您将需要产生额外的图像。

If you don't have / don't need images with extra detail, you can either opt to fix the zoom level at 3 (nobody will ever notice anyway) and deploy like that or clean up (perhaps even remove) the normalization code to your needs. 如果您没有/不需要更多细节的图像,则可以选择将缩放级别固定为3(无论如何也不会有人注意到)并像这样进行部署,或者清理(也许甚至删除)规范化代码以您的需求。 Currently the function is in charge of repeating the same images across the X axis, while not repeating at all on the Y axis. 当前,该功能负责在X轴上重复相同的图像,而在Y轴上完全不重复。 For the repetition on the X axis it looks at the zoom level to determine how many tiles should be loaded before wrapping around to 0 again. 对于X轴上的重复,它将查看缩放级别,以确定在再次环绕为0之前应加载多少个图块。

From the question, I can't deduce if you need the wrapping or just picked that up from the example. 从这个问题上,我无法推断出您是否需要包装纸或者只是从示例中挑选包装纸。 Nor can I tell if you have more images or not. 我也无法告诉您是否有更多图像。 Therefor I can't hand you any ready-to-use code, because I don't have any specifications to work with. 因此,我无法将任何现成的代码交给您,因为我没有任何可使用的规范。


If you don't need the wrapping, make it handle the X coordinates like it does the Y coordinates: 如果不需要包装,则使其像处理Y坐标一样处理X坐标:

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;
    }

    // don't repeat across x-axis (horizontally)
    if (x < 0 || x >= tileRange) {
        return null;
    }

    return {
        x: x,
        y: y
    };
}

(Note that those two if statements can be collapsed into one, I just wanted to keep it easy to notice what changes influence this behavior) (请注意,这两个if语句可以折叠为一个,我只是想让它很容易注意到哪些更改会影响此行为)

You seem to be having a zoom level issue. 您似乎遇到了缩放级别问题。 By changing the minZoom and maxZoom in the customMapTypeOptions object to '0'and '2' respectively and setting the starting zoom to '1' in the mapOptions object, the application called images '0-1', '0-0','1-1', and '1-0' on load. 通过将customMapTypeOptions对象中的minZoom和maxZoom分别更改为“ 0”和“ 2”,并在mapOptions对象中将开始缩放比例设置为“ 1”,应用程序称为图像“ 0-1”,“ 0-0”,“ 1-1”和“ 1-0”加载。

After I investigated the Google example you listed, I noticed that when you url for their map tile include the zoom level in the url. 在调查了您列出的Google示例之后,我注意到当您为他们的地图图块添加网址时,网址中会包含缩放级别。 Here is an example of the one tile called at a zoom level of 0: 这是缩放级别为0的一个图块的示例:

http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/0/0/0.jpg http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/0/0/0.jpg

Note the last three numbers, ".../0/0/0.jpg". 注意最后三个数字“ ... / 0/0 / 0.jpg”。 After I zoomed in to zoom level 1, the first two map tiles called was have the urls: 在放大到1级缩放后,前两个地图图块具有网址:

http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/1/0/1.jpg http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/1/0/1.jpg
http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/1/0/0.jpg http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/1/0/0.jpg

Notice how the last three number have changed. 注意最后三个数字是如何变化的。 I believe the first number is the zoom level the image should be displayed at followed by the x & y of the images position in the map tile grid. 我相信第一个数字是图像应显示的缩放级别,然后是地图图块网格中图像位置的x和y。

According to a comment in the example code: 根据示例代码中的注释:

// 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;

you will use a single image file for zoom level 0 (which is why 0-0 is your only image loaded), 2 images for zoom level 1, 4 images for zoom level 2, and so on. 您将使用一个图像文件进行缩放级别0(这就是为什么0-0是唯一加载的图像),2个图像进行缩放级别1、4个图像进行缩放级别2,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM