首页 > 解决方案 > Java Android Google 地图更改缩放控件位置并将相机设置为标记

问题描述

我希望缩放控件位于中间的最顶部,我试图做这样的事情,但它不起作用:

  SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().
                findFragmentById(R.id.map);
        View mapView = mapFragment.getView();
View zoom_in_button = mapView.findViewWithTag("GoogleMapZoomInButton");


RelativeLayout.LayoutParams location_layout = (RelativeLayout.LayoutParams) zoom_in_button.getLayoutParams();
        location_layout.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);

我还需要得到这个:将视图居中,使标记位于左下角

我这样做了,但它不起作用。

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentPGO.getLatitude(), currentPGO.getLongitude()), 16));

标签: javaandroidgoogle-maps

解决方案


  1. 隐藏谷歌地图的默认缩放按钮

    mMap.uiSettings.isZoomControlsEnabled = false

  2. 为放大和缩小创建 ImageView/Button 小部件(在顶部中心或任何您喜欢的位置对齐,在片段上方)

  3. 在 Widget click 上调用以下方法:

    放大

    mMap.animateCamera(CameraUpdateFactory.zoomIn())

    缩小

    mMap.animateCamera(CameraUpdateFactory.zoomOut())

编辑:如果您希望将标记放置在左下角,则需要计算新的 LatLang 值,该值将略微位于所需位置的右侧和顶部,并相应地移动相机。例如,如果您的缩放级别为 16,则使用如下内容:

var lat = markerLatLng.latitude
var lng = markerLatLng.longitude
mMap.addMarker(MarkerOptions().position(LatLng(lat, lng)))
lat += 0.005
lng += 0.003
moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(lat, lng), 16f))

推荐阅读