Skip to content

Commit

Permalink
feat: add map padding (#232)
Browse files Browse the repository at this point in the history
* feat: add map padding support

* feat: add padding to map init params

* refactor: general pr improvements

* chore: format

* chore: fix padding value presentation on example app

* feat: add get padding

* test: padding integration test

---------

Co-authored-by: Joonas Kerttula <[email protected]>
  • Loading branch information
illuminati1911 and jokerttu authored Dec 17, 2024
1 parent d1c0cfb commit bf991ee
Show file tree
Hide file tree
Showing 31 changed files with 1,366 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ open class AndroidAutoBaseScreen(carContext: CarContext) :
mViewRegistry = viewRegistry
mAutoMapView =
GoogleMapsAutoMapView(
GoogleMapOptions(),
MapOptions(GoogleMapOptions(), null),
viewRegistry,
imageRegistry,
navigationView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ object Convert {
* @param options pigeon message [NavigationViewCreationOptionsDto].
* @return Google Map Options [GoogleMapOptions].
*/
fun convertMapOptionsFromDto(options: MapOptionsDto): GoogleMapOptions {
val mapOptions = GoogleMapOptions()
fun convertMapOptionsFromDto(options: MapOptionsDto): MapOptions {
val googleMapOptions = GoogleMapOptions()

mapOptions.camera(convertCameraPositionFromDto(options.cameraPosition))
mapOptions.mapType(convertMapTypeFromDto(options.mapType))
mapOptions.compassEnabled(options.compassEnabled)
mapOptions.rotateGesturesEnabled(options.rotateGesturesEnabled)
mapOptions.scrollGesturesEnabled(options.scrollGesturesEnabled)
mapOptions.tiltGesturesEnabled(options.tiltGesturesEnabled)
mapOptions.zoomGesturesEnabled(options.zoomGesturesEnabled)
mapOptions.scrollGesturesEnabledDuringRotateOrZoom(
googleMapOptions.camera(convertCameraPositionFromDto(options.cameraPosition))
googleMapOptions.mapType(convertMapTypeFromDto(options.mapType))
googleMapOptions.compassEnabled(options.compassEnabled)
googleMapOptions.rotateGesturesEnabled(options.rotateGesturesEnabled)
googleMapOptions.scrollGesturesEnabled(options.scrollGesturesEnabled)
googleMapOptions.tiltGesturesEnabled(options.tiltGesturesEnabled)
googleMapOptions.zoomGesturesEnabled(options.zoomGesturesEnabled)
googleMapOptions.scrollGesturesEnabledDuringRotateOrZoom(
options.scrollGesturesEnabledDuringRotateOrZoom
)
mapOptions.mapToolbarEnabled(options.mapToolbarEnabled)
googleMapOptions.mapToolbarEnabled(options.mapToolbarEnabled)
options.cameraTargetBounds?.let {
mapOptions.latLngBoundsForCameraTarget(convertLatLngBoundsFromDto(it))
googleMapOptions.latLngBoundsForCameraTarget(convertLatLngBoundsFromDto(it))
}
options.minZoomPreference?.let { mapOptions.minZoomPreference(it.toFloat()) }
options.maxZoomPreference?.let { mapOptions.maxZoomPreference(it.toFloat()) }
mapOptions.zoomControlsEnabled(options.zoomControlsEnabled)
options.minZoomPreference?.let { googleMapOptions.minZoomPreference(it.toFloat()) }
options.maxZoomPreference?.let { googleMapOptions.maxZoomPreference(it.toFloat()) }
googleMapOptions.zoomControlsEnabled(options.zoomControlsEnabled)

return mapOptions
return MapOptions(googleMapOptions, options.padding)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ package com.google.maps.flutter.navigation

import android.content.Context
import android.view.View
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.MapView
import io.flutter.plugin.platform.PlatformView

class GoogleMapView
internal constructor(
context: Context,
mapOptions: GoogleMapOptions,
mapOptions: MapOptions,
viewId: Int,
viewEventApi: ViewEventApi,
private val viewRegistry: GoogleMapsViewRegistry,
imageRegistry: ImageRegistry,
) : PlatformView, GoogleMapsBaseMapView(viewId, mapOptions, viewEventApi, imageRegistry) {
private val _mapView: MapView = MapView(context, mapOptions)
private val _mapView: MapView = MapView(context, mapOptions.googleMapOptions)

override fun getView(): View {
return _mapView
Expand All @@ -51,6 +50,7 @@ internal constructor(
initListeners()
imageRegistry.mapViewInitializationComplete()
mapReady()
mapOptions.padding?.let { setPadding(it) }
invalidateViewAfterMapLoad()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ package com.google.maps.flutter.navigation

import android.view.View
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.libraries.navigation.NavigationViewForAuto

class GoogleMapsAutoMapView
internal constructor(
mapOptions: GoogleMapOptions,
mapOptions: MapOptions,
private val viewRegistry: GoogleMapsViewRegistry,
imageRegistry: ImageRegistry,
private val mapView: NavigationViewForAuto,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,12 @@ class GoogleMapsAutoViewMessageHandler(private val viewRegistry: GoogleMapsViewR
override fun isAutoScreenAvailable(): Boolean {
return viewRegistry.getAndroidAutoView() != null
}

override fun setPadding(padding: MapPaddingDto) {
getView().setPadding(padding)
}

override fun getPadding(): MapPaddingDto {
return getView().getPadding()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.google.android.gms.maps.CameraUpdate
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.Circle
import com.google.android.gms.maps.model.FollowMyLocationOptions
Expand All @@ -39,7 +38,7 @@ import com.google.android.libraries.navigation.NavigationView

abstract class GoogleMapsBaseMapView(
protected val viewId: Int?,
mapOptions: GoogleMapOptions,
mapOptions: MapOptions,
protected val viewEventApi: ViewEventApi?,
private val imageRegistry: ImageRegistry,
) {
Expand All @@ -59,6 +58,8 @@ abstract class GoogleMapsBaseMapView(
private var _minZoomLevelPreference: Float? = null
private var _maxZoomLevelPreference: Float? = null

private var _mapOptions: MapOptions? = null

// Nullable variable to hold the callback function
private var _mapReadyCallback: ((Result<Unit>) -> Unit)? = null
private var _loadedCallbackPending = false
Expand Down Expand Up @@ -100,8 +101,9 @@ abstract class GoogleMapsBaseMapView(
}

init {
_minZoomLevelPreference = mapOptions.minZoomPreference
_maxZoomLevelPreference = mapOptions.maxZoomPreference
_minZoomLevelPreference = mapOptions.googleMapOptions.minZoomPreference
_maxZoomLevelPreference = mapOptions.googleMapOptions.maxZoomPreference
_mapOptions = mapOptions
}

protected fun mapReady() {
Expand Down Expand Up @@ -942,4 +944,19 @@ abstract class GoogleMapsBaseMapView(
) {}
}
}

fun setPadding(padding: MapPaddingDto) {
_mapOptions?.padding = padding
getMap()
.setPadding(
padding.left.toInt(),
padding.top.toInt(),
padding.right.toInt(),
padding.bottom.toInt(),
)
}

fun getPadding(): MapPaddingDto {
return _mapOptions?.padding ?: MapPaddingDto(0, 0, 0, 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ import android.content.Context
import android.content.res.Configuration
import android.view.View
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.libraries.navigation.NavigationView
import io.flutter.plugin.platform.PlatformView

class GoogleMapsNavigationView
internal constructor(
context: Context,
mapOptions: GoogleMapOptions,
mapOptions: MapOptions,
navigationOptions: NavigationViewOptions?,
viewId: Int,
private val viewRegistry: GoogleMapsViewRegistry,
viewEventApi: ViewEventApi,
private val imageRegistry: ImageRegistry,
) : PlatformView, GoogleMapsBaseMapView(viewId, mapOptions, viewEventApi, imageRegistry) {
private val _navigationView: NavigationView = NavigationView(context, mapOptions)
private val _navigationView: NavigationView = NavigationView(context, mapOptions.googleMapOptions)

/// Default values for UI features.
private var _isNavigationTripProgressBarEnabled: Boolean = false
Expand Down Expand Up @@ -79,11 +78,12 @@ internal constructor(
// respected.
_navigationView.isNavigationUiEnabled = navigationViewEnabled
if (!navigationViewEnabled) {
map.moveCamera(CameraUpdateFactory.newCameraPosition(mapOptions.camera))
map.moveCamera(CameraUpdateFactory.newCameraPosition(mapOptions.googleMapOptions.camera))
}

// Call and clear view ready callback if available.
mapReady()
mapOptions.padding?.let { setPadding(it) }
invalidateViewAfterMapLoad()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,12 @@ class GoogleMapsViewMessageHandler(private val viewRegistry: GoogleMapsViewRegis
override fun registerOnCameraChangedListener(viewId: Long) {
getView(viewId.toInt()).registerOnCameraChangedListener()
}

override fun setPadding(viewId: Long, padding: MapPaddingDto) {
getView(viewId.toInt()).setPadding(padding)
}

override fun getPadding(viewId: Long): MapPaddingDto {
return getView(viewId.toInt()).getPadding()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.maps.flutter.navigation

import com.google.android.gms.maps.GoogleMapOptions

class MapOptions(val googleMapOptions: GoogleMapOptions, var padding: MapPaddingDto?) {}
Loading

0 comments on commit bf991ee

Please sign in to comment.