Google map Background color for Map type GoogleMap.MAP_TYPE_NONE

╄→尐↘猪︶ㄣ 提交于 2020-08-09 09:11:29

问题


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

This shall produce the UI as attached below.

I have 2 questions here :

  1. How do i change the background color to the color of my choice?
  2. How do i Zoom In and Zoom Out programmatically? Say i want to zoom in by 5F as soon as map loads.

回答1:


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

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

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

where SphericalUtil is part of Maps SDK for Android Utility Library. How to add it for project described here.

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:

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

you can get result like that:

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

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


来源:https://stackoverflow.com/questions/62055885/google-map-background-color-for-map-type-googlemap-map-type-none

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!