为同时移动目标纬度/经度和缩放级别的动画偏移 MapFragment 的中心 [英] Offseting the center of the MapFragment for an animation moving both the target lat/lng and the zoom level

查看:31
本文介绍了为同时移动目标纬度/经度和缩放级别的动画偏移 MapFragment 的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UI,它有一个 MapFragment,上面覆盖着一个透明的 View.地图占据了整个屏幕,而视图只是屏幕的左三分之一.因此,地图的默认中心"是关闭的.当有人点击某个标记时,我想将该标记置于 MapFragment 的完全可见区域的中心(而不是 MapFragment 本身的中心).

I've got a UI that has a MapFragment with a transparent View overlaid on top of it. The map takes up the entire screen, whereas the View is just the left third of the screen. As a result, the default "center" of the map is off. When someone clicks on a marker, I want to center that marker in the wholly visible area of the MapFragment (not the center of the MapFragment itself).

这很难用语言来描述,让我用几张图来描述.假设我的 UI 是这样的:

Since this is hard to describe with words, let me use a few pictures. Suppose this is how my UI looks:

当用户点击一个标记时,我想将它居中放大以更近地看到它.无需任何调整,这就是您将得到的:

When the user clicks a marker, I want to both center it and zoom in to see it closer. Without any adjustments, this is what you'll get:

我想要的是标记居中,但在右侧的空间中,如下所示:

What I want is for the marker to be centered, but in the space to the right, like this:

如果您不使用地图的投影更改缩放级别,则很容易实现这一壮举:

It's very easy to achieve this feat if you're not changing the zoom level using the map's projection:

// Offset the target latitude/longitude by preset x/y ints
LatLng target = new LatLng(latitude, longitude);
Projection projection = getMap().getProjection();
Point screenLocation = projection.toScreenLocation(target);
screenLocation.x += offsetX;
screenLocation.y += offsetY;
LatLng offsetTarget = projection.fromScreenLocation(screenLocation);

// Animate to the calculated lat/lng
getMap().animateCamera(CameraUpdateFactory.newLatLng(offsetTarget));

但是,如果您同时更改缩放级别,则上述计算不起作用(因为 lat/lng 偏移量在不同缩放级别会发生变化).

However, if you're changing the zoom level at the same time, the above calculations don't work (since the lat/lng offsets change at different zoom levels).

让我列出已尝试修复的列表:

Let me run down a list of attempted fixes:

  • 快速更改缩放级别,进行计算,缩放回原始相机位置,然后制作动画.不幸的是,突然的相机变化(即使只是一瞬间)非常明显,我想避免闪烁.

  • Changing the zoom level quickly, doing the calculations, zooming back to the original camera position, then animating. Unfortunately the sudden camera change (even if it's only for a split second) is unfortunately very obvious and I'd like to avoid the flicker.

将两个 MapFragment 相互叠加,其中一个进行计算,另一个显示.我发现 MapFragments 并没有真正构建为相互叠加(这条路线有不可避免的错误).

Overlaying two MapFragments on top of each other, having one do the calculations while the other displays. I've found that MapFragments are not really built to be layered on top of each other (there are unavoidable bugs down this route).

通过缩放级别的平方差修改屏幕位置的 x/y.理论上这应该可行,但它总是偏离很多(~.1 纬度/经度,这足以偏离).

Modifying the screen location's x/y by the difference in zoom level squared. Theoretically this should work but it's always off by quite a bit (~.1 latitude/longitude, which is enough to be way off).

有没有办法在缩放级别发生变化的情况下计算 offsetTarget?

Is there a way to calculate the offsetTarget even with the zoom level changing?

推荐答案

最新版本的播放服务库增加了一个 setPadding() 函数到 GoogleMap.此填充会将所有相机移动投影到偏移地图中心.本文档进一步解释了地图填充的行为.

The most recent version of the play services library adds a setPadding() function to GoogleMap. This padding will project all camera movements to the offset map center. This doc further explains how map padding behaves.

最初在这里提出的问题现在可以通过添加与左侧布局宽度相等的左填充来解决.

The question originally asked here can now simply be solved by adding left padding equivalent to the width of the left-side layout.

mMap.setPadding(leftPx, topPx, rightPx, bottomPx);

这篇关于为同时移动目标纬度/经度和缩放级别的动画偏移 MapFragment 的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆