Android 如何将地图居中放置在标记上单击以显示整个信息窗口
确定,默认情况下,单击标记时,标记在地图上居中。问题是,我有很高的信息视图,从布局中膨胀,它显示超出地图边界。我可以做什么,水平居中定位销,并将其放置在地图中心下方,以使信息窗口可见。在信息窗口的适配器中尝试以下操作:Android 如何将地图居中放置在标记上单击以显示整个信息窗口,android,maps,center,marker,Android,Maps,Center,Marker,确定,默认情况下,单击标记时,标记在地图上居中。问题是,我有很高的信息视图,从布局中膨胀,它显示超出地图边界。我可以做什么,水平居中定位销,并将其放置在地图中心下方,以使信息窗口可见。在信息窗口的适配器中尝试以下操作: @Override public View getInfoWindow(Marker marker) { View infoWindowLayout = (View) inflater.inflate(R.layout.my_info_window_layout, null);
@Override
public View getInfoWindow(Marker marker) {
View infoWindowLayout = (View) inflater.inflate(R.layout.my_info_window_layout, null);
int width = display.getWidth() (I put here a "*2/3" too to not fill the whole screen, but this is your choice);
LinearLayout infoWindowSubLayout = (LinearLayout) infoWindowLayout.findViewById(R.id.myInfoWindowSubLayout);
android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(width, LayoutParams.MATCH_PARENT);
infoWindowSubLayout.setLayoutParams(lp);
return infoWindowLayout;
}
在我的例子中,这个布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myInfoWindowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/..."
android:orientation="horizontal"
... >
<LinearLayout
android:id="@+id/myInfoWindowSubLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
...>
<LinearLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical" >
CONTENT HERE
</LinearLayout>
CONTENT HERE TOO
</LinearLayout>
</LinearLayout>
满足于此
这里也有内容
嗯,这对我来说很有效,请随意重写它所以我最终得到了这个解决方案,它就像一个符咒。我在动画后打开信息窗口
int zoom = (int)map.getCameraPosition().zoom
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new
LatLng(arg0.getPosition().latitude + (double)90/Math.pow(2, zoom),
arg0.getPosition().longitude), zoom);
map.animateCamera(cu,500,null);
我尝试了以下完美的解决方案
mMap.setOnMarkerClickListener(new OnMarkerClickListener()
{
@Override
public boolean onMarkerClick(Marker marker)
{
int yMatrix = 200, xMatrix =40;
DisplayMetrics metrics1 = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics1);
switch(metrics1.densityDpi)
{
case DisplayMetrics.DENSITY_LOW:
yMatrix = 80;
xMatrix = 20;
break;
case DisplayMetrics.DENSITY_MEDIUM:
yMatrix = 100;
xMatrix = 25;
break;
case DisplayMetrics.DENSITY_HIGH:
yMatrix = 150;
xMatrix = 30;
break;
case DisplayMetrics.DENSITY_XHIGH:
yMatrix = 200;
xMatrix = 40;
break;
case DisplayMetrics.DENSITY_XXHIGH:
yMatrix = 200;
xMatrix = 50;
break;
}
Projection projection = mMap.getProjection();
LatLng latLng = marker.getPosition();
Point point = projection.toScreenLocation(latLng);
Point point2 = new Point(point.x+xMatrix,point.y-yMatrix);
LatLng point3 = projection.fromScreenLocation(point2);
CameraUpdate zoom1 = CameraUpdateFactory.newLatLng(point3);
mMap.animateCamera(zoom1);
marker.showInfoWindow();
return true;
}
});
谷歌地图在高纬度地区使用了一种非常扭曲的数据,因为它基本上把我们的星球映射成了一个正方形。由于这一点,离赤道越远,简化计算的误差就越大
我们将让Android代我们处理这些问题;)强>
在下面的文本中,mGoogleMap
是您的GoogleMap
。我们将设置一个
onMarkerClickListener
打开InfoWindow
(与onMarkerClickListener的默认实现相同),然后
以标记本身为中心,我们根据谷歌地图投影重新计算相机中心位置
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// Calculate required horizontal shift for current screen density
final int dX = getResources().getDimensionPixelSize(R.dimen.map_dx);
// Calculate required vertical shift for current screen density
final int dY = getResources().getDimensionPixelSize(R.dimen.map_dy);
final Projection projection = mGoogleMap.getProjection();
final Point markerPoint = projection.toScreenLocation(
marker.getPosition()
);
// Shift the point we will use to center the map
markerPoint.offset(dX, dY);
final LatLng newLatLng = projection.fromScreenLocation(markerPoint);
// Buttery smooth camera swoop :)
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(newLatLng));
// Show the info window (as the overloaded method would)
marker.showInfoWindow();
return true; // Consume the event since it was dealt with
}
});
animateCamera()
将相机平滑地移动到计算位置,您可以使用moveCamera()
跳到新位置
我们还需要向资源中添加两个维度,map\u dx
和map\u dy
:
<resources>
<!-- Where to shift the map center when user taps on marker -->
<dimen name="map_dx">10dp</dimen>
<dimen name="map_dy">-100dp</dimen>
</resources>
10dp
-100dp
我们已经完成了,这种方法(与其他方法不同)在整个地图上都能很好地工作,甚至在接近地球极点的地方。好吧,我的观点有点复杂,不能用这种方式缩小它,但是如果有人使用比例感知布局,这将是一个很好的答案。这很好。我很好奇数学是从哪里来的。这些是你提出的包含缩放级别的合适值,还是这些数字都是特定的理论?我知道世界宽度是256dp*2^zoomLevel。arg0.getPosition()。纬度是我的pin纬度。90只是我需要的一个因素,我通过计算距离得到它,我想在特定的缩放级别移动相机,比如说在zoom 14时,我需要将相机移动约0006度以使infoView可见。基于这个,我计算了因子90,这是有效的:)如果管脚居中,我的信息视图太大,无法放入地图,所以我想设置缩放动画,使其指向管脚上方的点。有一件事。。。它适用于整数缩放级别,所以我将其转换为整数。这是一个绝妙的技巧。不要问我为什么,但如果我用150而不是90,对我来说效果会更好。mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(新Latlings(selectedMarker.getPosition().latitude+(double)150/Math.pow(2,zoom),selectedMarker.getPosition().longitude),zoom));什么动画之后?@PabloCegarra我指的是地图中心动画。