Google Map地图类型GoogleMap.MAP_TYPE_NONE的背景色 [英] Google map Background color for Map type GoogleMap.MAP_TYPE_NONE

查看:160
本文介绍了Google Map地图类型GoogleMap.MAP_TYPE_NONE的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用添加到Google Map的平面图的地面叠加层.它完美加载.代码如下所示.

I have used Ground overlay of my floorplan added to Google Map. It loads up perfectly. Code is as shown below.

override fun onMapReady(map: GoogleMap) {
    mMap = map;

    mMap.setOnGroundOverlayClickListener(this);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(DEFAULT_LAT_LNG, 1F));
    mMap.setMapType(GoogleMap.MAP_TYPE_NONE);


    mGroundOverlay = mMap.addGroundOverlay(GroundOverlayOptions()
            .image(BitmapDescriptorFactory.fromBitmap(bitmap)).anchor(0F, 1F)
            .position(DEFAULT_LAT_LNG, floorPlanWidth.toFloat(), 
    floorPlanHeight.toFloat()));

    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mGroundOverlay.getBounds(), 0))
}

这将产生如下所示的UI.

This shall produce the UI as attached below.

我在这里有2个问题:

  1. 如何将背景颜色更改为我选择的颜色?
  2. 我如何以编程方式放大和缩小?假设我想在地图加载后立即放大5F.

推荐答案

对于p.1,最简单的方法是使用其他GroundOverlay,它是由地面叠加层的背景色填充的纯色位图.正是您的示例图片可以是这样的内容(overlay_background.png):

For p.1 easiest way is to use additional GroundOverlay which is solid color bitmap filled by background color of your ground overlay. Exactly for your example picture can be something like that (overlay_background.png):

该图片应用作背景" GroundOverlay下的背景" GroundOverlay.

That picture you should use as "background" GroundOverlay placed under your "floors" GroundOverlays.

为确保背景" GroundOverlay覆盖整个可见区域,应在地板"覆盖层上稍微缩放该图像.您可以通过radius,可以使用如下方法:

To ensure that the "background" GroundOverlay covers the entire visible area you should scale that image slightly over the "floors" overlay. You can do it via .positionFromBounds() method of GroundOverlayOptions. For get "background" overlay bounds radius distant from the center you can use method like that:

public LatLngBounds createBounds(LatLng center, float radius) {
    LatLngBounds bounds = null;
    if (center != null) {
        bounds = new LatLngBounds.Builder()
                .include(SphericalUtil.computeOffset(center, radius * Math.sqrt(2), 45))
                .include(SphericalUtil.computeOffset(center, radius * Math.sqrt(2), 225))
                .build();
    }
    return bounds;
}

其中SphericalUtil适用于Android实用工具库的Maps SDK的一部分. 此处.

还可以通过 setLatLngBoundsForCameraTarget() 方法进行设置相机视图的边界要比背景"覆盖区域小一些,以避免当用户将地图滚动到背景"覆盖范围之外时的情况.您需要设置最小和最大缩放级别(通过

Also via setLatLngBoundsForCameraTarget() method you can set camera view bounds slightly less then "background" overlay area to avoid situation when user scrolls map out of "background" overlay bounds. You need to set minimal and maximal zoom level (via setMinZoomPreference() and setMaxZoomPreference()) also.

因此,下图描述了总体思路:

So general idea is described on figure below:

要在楼层" GroundOverlay下放置背景" GroundOverlay,可以使用.setZIndex()方法/android/gms/maps/model/GroundOverlayOptions#getZIndex()"rel =" nofollow noreferrer> GroundOverlayOptions GroundOverlay设置为小于Z-Index 地板" GroundOverlay例如Z-Index = 1表示背景",Z-Index = 2表示地面覆盖(Z索引的默认值为0).

For placing "background" GroundOverlay under your "floors" GroundOverlays you can use .setZIndex() method of GroundOverlayOptions or GroundOverlay and set Z-Index for "background" GroundOverlay less than Z-Index of "floors" GroundOverlay e.g. Z-Index = 1 for "background" and Z-Index = 2 for floor overlay (default value of Z-Index is 0).

因此使用这样的源代码:

So with source code like this:

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;

    // calculate borders for "background" overlay
    LatLngBounds borders = createBounds(OVERLAY_CENTER, 1000);

    // constrain the camera target to slightly less bounds.
    LatLngBounds cameraBorders = createBounds(OVERLAY_CENTER, 500);
    mGoogleMap.setLatLngBoundsForCameraTarget(borders);
    mGoogleMap.setMinZoomPreference(18.0f);
    mGoogleMap.setMaxZoomPreference(21.0f);

    GroundOverlayOptions overlayBackground = new GroundOverlayOptions()
            .image(BitmapDescriptorFactory.fromResource(R.drawable.overlay_background))
            .transparency(0.0f)
            .bearing(0)
            .zIndex(1) // NB! set Z-Index for "background" overlay
            .positionFromBounds(borders);

    mGoogleMap.addGroundOverlay(overlayBackground);

    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NONE);

    GroundOverlayOptions overlayOptions = new GroundOverlayOptions()
            .image(BitmapDescriptorFactory.fromResource(R.drawable.overlay))
            .transparency(0.0f)
            .bearing(0)
            .zIndex(2)  // NB! set Z-Index for "floor" overlay
            .position(OVERLAY_CENTER, 200f);

    GroundOverlay overlay = mGoogleMap.addGroundOverlay(overlayOptions);

    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(OVERLAY_CENTER, 18.0f));
}

您可以得到如下结果:

对于p.2问题,恕我直言,最简单的方法是通过使用一组楼层叠加来模拟"室内地图.在这种情况下,您可以通过向地图添加相应的楼层叠加层来显示必要的楼层,并使用

For p.2 question IMHO easiest way is to "emulate" indoor maps by using set of floors overlay. In that case you can show necessary floor by adding corresponding floor overlay to map and show/hide it by using setTransparency() method: use use setTransparency(1) for all floors that you want to hide, and setTransparency(0) for one floor, for which needs to be shown. For example, for display an overlay for floor #3:

GroundOverlay floor1Overlay = mGoogleMap.addGroundOverlay(floor1OverlayOptions);
GroundOverlay floor2Overlay = mGoogleMap.addGroundOverlay(floor2OverlayOptions);
GroundOverlay floor3Overlay = mGoogleMap.addGroundOverlay(floor2OverlayOptions);
...
floor1Overlay.setTransparency(1);
floor2Overlay.setTransparency(1);
floor3Overlay.setTransparency(0);

或者您可以使用 remove() 方法,并在需要时重新创建它.为此,您需要在地板叠加层对象添加到地图上时对其进行存储:

Or you can remove not necessary floors overlay from map with remove() method on GroundOverlay (not on GroundOverlayOptions!) object and recreate it when needed again. For this you need to store floor overlay object when it was added on map:

GroundOverlay floor1Overlay = mGoogleMap.addGroundOverlay(floor1OverlayOptions);
...
floor1Overlay.remove();

这篇关于Google Map地图类型GoogleMap.MAP_TYPE_NONE的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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