Issue
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 :
- How do i change the background color to the color of my choice?
- How do i Zoom In and Zoom Out programmatically? Say i want to zoom in by 5F as soon as map loads.
Solution
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" GroundOverlay
s.
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" GroundOverlay
s 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();
Answered By - Andrii Omelchenko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.