【问题标题】:Java Android Google maps change zoom controls position and set camera to markerJava Android Google 地图更改缩放控件位置并将相机设置为标记
【发布时间】:2019-10-02 06:57:25
【问题描述】:

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

  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));

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    据我所知,您无法更改地图控件,您可以隐藏它们并制作自己的放大和缩小按钮。

    map.getUiSettings().setZoomControlsEnabled(false);
    

    然后将您的按钮置于相对布局或约束布局中,并根据需要放置它们。

    然后让它们放大和缩小

    (来自 google maps api for Android 的片段) https://developers.google.com/maps/documentation/android-sdk/views

    CameraUpdateFactory.zoomIn() and CameraUpdateFactory.zoomOut()// give you a CameraUpdate that changes the zoom level by 1.0, while keeping all other properties the same.
    

    【讨论】:

      【解决方案2】:
      1. 隐藏谷歌地图的默认缩放按钮

        mMap.uiSettings.isZoomControlsEnabled = false

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

      3. 在 Widget 点击时调用以下方法:

        放大

        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))
      

      【讨论】:

      • 好的,谢谢,我怎样才能使视图居中,使指示的标记位于左下角
      • 编辑了我的答案以包括定位部分。希望对您有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多