Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: standalone classic mapview #181

Merged
merged 18 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ The view can be controlled with the `GoogleNavigationViewController` that is pas
The `GoogleMapsNavigationView` widget should be used within a widget with a bounded size. Using it
in an unbounded widget will cause the application to throw a Flutter exception.

You can also add a bare GoogleMapsMapView that works as a normal map view without navigation functionality.

### Add a navigation view

```dart
Expand Down Expand Up @@ -139,6 +141,29 @@ class _NavigationSampleState extends State<NavigationSample> {
}
```

### Add a map view

```dart
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Google Maps Navigation Sample')),
body: _navigationSessionInitialized
? GoogleMapsMapView(
onViewCreated: _onViewCreated,
initialCameraPosition: CameraPosition(
// Initialize map to user location.
target: _userLocation!,
zoom: 15,
),
// Other view initialization settings
)
: const Center(child: CircularProgressIndicator()),
);
}

```

See the [example](./example) directory for a complete navigation sample app.

### Requesting and handling permissions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2023 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 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,
viewId: Int,
viewEventApi: ViewEventApi,
private val viewRegistry: GoogleMapsViewRegistry,
imageRegistry: ImageRegistry
) : PlatformView, GoogleMapsBaseMapView(viewId, viewEventApi, imageRegistry) {
private val _mapView: MapView = MapView(context, mapOptions)

override fun getView(): View {
return _mapView
}

init {
// Call all of these three lifecycle functions in sequence to fully
// initialize the map view.
_mapView.onCreate(context.applicationInfo.metaData)
_mapView.onStart()
_mapView.onResume()

_mapView.getMapAsync { map ->
setMap(map)
initListeners()
imageRegistry.mapViewInitializationComplete()
mapReady()
invalidateViewAfterMapLoad()
}

viewRegistry.registerMapView(viewId, this)
}

override fun dispose() {
// When view is disposed, all of these lifecycle functions must be
// called to properly dispose navigation view and prevent leaks.
_mapView.onPause()
_mapView.onStop()
_mapView.onDestroy()

viewRegistry.unregisterMapView(viewId)
}

override fun onStart() {
_mapView.onStart()
}

override fun onResume() {
_mapView.onResume()
}

override fun onStop() {
_mapView.onStop()
}

override fun onPause() {
_mapView.onPause()
}
}
Loading
Loading