谷歌地图自定义标记视网膜分辨率

发布于 2025-01-03 13:34:32 字数 1071 浏览 1 评论 0原文

我正在 html5 中开发 iPhone 应用程序并使用 Phonegap 进行构建。 在应用程序中有一个带有自定义标记的Google地图,标记图标的创建方式如下:

var image = new google.maps.MarkerImage("hat.png", null, null, null, new google.maps.Size(20,30));
var shadow = new google.maps.MarkerImage("shadow.png", null, null, null, new google.maps.Size(20,30));

var marker = new google.maps.Marker({
                 map: map,
                 position: latlng,
                 index: markers.length,
                 icon: image,                     
                 shadow: shadow,
                 animation: google.maps.Animation.DROP,
                 html: htmlContent                                                
             });

与代码中定义的尺寸相比,图标的实际尺寸是两倍。这样做是为了确保图标在视网膜显示屏上以高分辨率显示。 上面的代码直到今天都运行良好,但现在发生的情况如下。

当图标下拉时,使用 google.maps.Animation.DROP,图标在下降过程中以高分辨率显示,但是当图标“降落”在地图上时,图标会切换到低分辨率版本。

有人有过同样的经历吗?

谢谢...

更新 发现如果我指定 Google 地图版本,例如:

http://maps.googleapis.com/maps/api/js?v=3.0

所以我猜这是最新的 Google 地图 API 中的一个错误。

I'm developing an iPhone app in html5 and making the build with Phonegap.
In the app there's a Google map with custom markers, the way the marker icons are created is as follows:

var image = new google.maps.MarkerImage("hat.png", null, null, null, new google.maps.Size(20,30));
var shadow = new google.maps.MarkerImage("shadow.png", null, null, null, new google.maps.Size(20,30));

var marker = new google.maps.Marker({
                 map: map,
                 position: latlng,
                 index: markers.length,
                 icon: image,                     
                 shadow: shadow,
                 animation: google.maps.Animation.DROP,
                 html: htmlContent                                                
             });

The actual size of the icon's are double size compared to the sizes defined in the code. This is done to make sure the icons are shown in high res on the Retina display.
The code above worked fine until today, but what happens now is the following.

When the icons Drop down, using the google.maps.Animation.DROP, the icon is shown in high res on the way down, but when the icon "lands" on the map, the icon switches to a low res resolution version.

Has anyone ever experienced the same?

Thank you...

UPDATE
Found out that if I specify the Google map version like:

http://maps.googleapis.com/maps/api/js?v=3.0

So I guess it's a bug in the newest Goolge map API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别挽留 2025-01-10 13:34:32

我通过使用 scaledSize 而不是 size 来定义图像的宽度和高度找到了解决方案。

I found my solution to this by using scaledSize instead of size to define the width and height of the image.

﹂绝世的画 2025-01-10 13:34:32

只需使用具有 urlsizescaledSize 属性的对象即可:

var icon = {
    url: 'path/img/[email protected]',
    size: new google.maps.Size(30, 40),
    scaledSize: new google.maps.Size(30, 40)
};

其中 path/img/[电子邮件受保护]60px x 80px PNG。

Simply use an object with url, size, and scaledSize attributes:

var icon = {
    url: 'path/img/[email protected]',
    size: new google.maps.Size(30, 40),
    scaledSize: new google.maps.Size(30, 40)
};

Where path/img/[email protected] is a 60px x 80px PNG.

海风掠过北极光 2025-01-10 13:34:32

我也刚刚发现这个问题。该错误似乎出现在其 API 的最新版本 (v3.7) 中,因此如果您通过 URL 参数 ?v=3.6 指定 v3.6,它将正常工作。

更新: 在 3.8+ 版本中(我认为),如果您正在使用的话,您可以使用 optimization: false 来强制显示视网膜图像。这是因为 optimized: true 会获取所有精灵并将它们编织到一个主精灵中。这意味着只有在标记很少的情况下才应该这样做。另外,与 Clusterer 配合得不太好。

默认值现已优化:true,这将首先确定设备是否可以处理渲染如此多的高分辨率图标,然后再以更高分辨率创建主精灵。例如,在视网膜 Macbook Pro 上加载带有大量标记的地图,它们会显示为高分辨率,但尝试 iPhone 4 时却不会。如果你强迫iPhone 4使用optimized: false,并且有很多标记,它可能会很卡顿。不确定 4S,但我认为它可能会使用更高分辨率的版本,因为它具有更好的处理能力。

I just found this problem too. The bug appears to be in latest (v3.7) of their API, so if you specify v3.6 via the URL parameter ?v=3.6 it'll work fine.

Update: In version 3.8+ (I think) you can use optimized: false to force a retina image, if you are using one. This is because optimized: true takes all your sprites and weaves them together into a master sprite. This means you should only do this if you have very few markers. Also, doesn't work too well with Clusterer.

The default is now optimized: true, which will determine first if the device can even handle rendering so many high-res icons before creating a master sprite at a higher resolution. For example, load a map with a lot of markers on a retina Macbook Pro, and they'll appear high-res, but try an iPhone 4 and it will not. If you force the iPhone 4 using optimized: false, and have a lot of markers, it might stutter a lot. Not sure about the 4S but I assume it'll probably use the higher res version as it has a lot better processing capability.

仄言 2025-01-10 13:34:32

如果您遇到任何将图标切换到较低分辨率的情况,那是因为地图 api 使用画布来渲染图标(对它们进行分组等等?),显然是为了更快的用户体验。因此,从技术上讲,这不是一个错误,但它确实会导致某些浏览器(例如较旧的 ie)中发生错误,

但是您可以使用一个设置来使用 optimized 在 MarkerOptions 中关闭该设置:假

var marker = new google.maps.Marker({
             map: map,
             position: latlng,
             icon: image,                     
             shadow: shadow,
             optimized: false                                               
         });

If you encounter any switching of icons to lower resolution, it's because the maps api uses canvas to render the icons (grouping them and whatnot?) for a faster user experience apparently. So it isn't technically a bug, but it does cause buggy things to happen in certain browsers (like older ie)

But there is a setting you can use to turn off in MarkerOptions using optimized: false

var marker = new google.maps.Marker({
             map: map,
             position: latlng,
             icon: image,                     
             shadow: shadow,
             optimized: false                                               
         });
满地尘埃落定 2025-01-10 13:34:32

缩放尺寸属性是 Google 此处引用的正确使用属性: https://developers.google.com/maps/documentation/javascript/3.exp/reference#Icon

Scaled Size property is the correct one to use as referenced by Google here: https://developers.google.com/maps/documentation/javascript/3.exp/reference#Icon

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文