好的,默认情况下,在标记点击中,标记位于地图的中心。问题是,我有高的infoView,从布局膨胀,它显示出地图范围。我该怎么办,将地平线中心固定,并将其放置在地图中心下方,以使infoWindow可见。
答案 0 :(得分:18)
所以我最终得到了这个解决方案,它就像一个魅力。我在动画后打开infoWindow。
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);
答案 1 :(得分:9)
GoogleMaps使用Web Mercator极大地扭曲了高纬度地区的数据,因为它实际上将我们的星球映射到了一个正方形。多亏了这一点,你越来越远离赤道,你引入一些简化计算的更大错误。
我们会让Android为我们处理这些内容;)
以下文字mGoogleMap
是您的GoogleMap
。我们将设置一个
onMarkerClickListener
我们在其中打开InfoWindow
(如在onMarkerClickListener的默认实现中),而不是
以标记本身为中心,我们根据 GoogleMap投影重新计算相机中心位置。
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_dx
和map_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>
我们已经完成了,这种方法(与其他方法不同)在整个地图上运行良好,甚至接近地球的极点。
答案 2 :(得分:1)
在infoWindow的适配器中试试这个:
@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>
嗯,这对我有用,随意重写它
答案 3 :(得分:1)
我尝试了以下完美的解决方案
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;
}
});