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

Adding ios changes for0ground overlays #2

Open
wants to merge 11 commits into
base: adding-google-maps-ground-overlay
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<!-- Update this value to your google maps api key. -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${mapsApiKey}" />
android:value="AIzaSyCSxThdnVBGIcSYKKpgKZr0MX18rnQuInI" />
<activity android:name="io.flutter.embedding.android.FlutterActivity"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
Expand All @@ -20,7 +20,7 @@
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="androi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d.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'move_camera.dart';
import 'padding.dart';
import 'page.dart';
import 'place_circle.dart';
import 'place_ground_overlay.dart';
import 'place_marker.dart';
import 'place_polygon.dart';
import 'place_polyline.dart';
Expand All @@ -43,6 +44,7 @@ final List<GoogleMapExampleAppPage> _allPages = <GoogleMapExampleAppPage>[
const LiteModePage(),
const TileOverlayPage(),
const MapIdPage(),
PlaceGroundOverlayPage(),
];

/// MapsDemo is the Main Application.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';

import 'page.dart';

class PlaceGroundOverlayPage extends GoogleMapExampleAppPage {
PlaceGroundOverlayPage() : super(const Icon(Icons.image), 'Place image');

@override
Widget build(BuildContext context) {
return const PlaceGroundOverlayBody();
}
}

class PlaceGroundOverlayBody extends StatefulWidget {
const PlaceGroundOverlayBody();

@override
State<StatefulWidget> createState() => PlaceGroundOverlayBodyState();
}

class PlaceGroundOverlayBodyState extends State<PlaceGroundOverlayBody> {
PlaceGroundOverlayBodyState();

BitmapDescriptor? _bitMapDesc;
GoogleMapController? controller;
Map<GroundOverlayId, GroundOverlay> groundOverlays =
<GroundOverlayId, GroundOverlay>{};
int _groundOverlayIdCounter = 0;
GroundOverlayId? selectedGroundOverlay;

void _onMapCreated(GoogleMapController controller) {
this.controller = controller;
}

@override
void dispose() {
super.dispose();
}

void _onGroundOverlayTapped(GroundOverlayId groundOverlayId) {
setState(() {
selectedGroundOverlay = groundOverlayId;
});
}

Future<void> _createGroundOverlayImageFromAsset(BuildContext context) async {
if (_bitMapDesc == null) {
final ImageConfiguration imageConfiguration =
createLocalImageConfiguration(context, size: Size.square(48));
await BitmapDescriptor.fromAssetImage(
imageConfiguration,
'assets/red_square.png',
).then(_updateBitmap);
}
}

void _updateBitmap(BitmapDescriptor bitmap) {
setState(() {
_bitMapDesc = bitmap;
});
}

void _remove(GroundOverlayId groundOverlayId) {
setState(() {
if (groundOverlays.containsKey(groundOverlayId)) {
groundOverlays.remove(groundOverlayId);
}
if (groundOverlayId == selectedGroundOverlay) {
selectedGroundOverlay = null;
}
});
}

void _add() {
final double offset = _groundOverlayIdCounter.ceilToDouble() / 1000;
LatLngBounds bounds = LatLngBounds(
southwest: LatLng(-33.853432 + offset, 151.211807),
northeast: LatLng(-33.851327 + offset, 151.213880),
);
final int groundOverlayCount = groundOverlays.length;

if (groundOverlayCount == 12) {
return;
}

final String groundOverlayIdVal =
'ground_overlay_id_$_groundOverlayIdCounter';
_groundOverlayIdCounter++;
final GroundOverlayId groundOverlayId = GroundOverlayId(groundOverlayIdVal);

final GroundOverlay groundOverlay = GroundOverlay.fromBounds(
bounds,
groundOverlayId: groundOverlayId,
bitmap: _bitMapDesc,
consumeTapEvents: true,
onTap: () {
_onGroundOverlayTapped(groundOverlayId);
},
);

setState(() {
groundOverlays[groundOverlayId] = groundOverlay;
});
}

void _changeTransparency(GroundOverlayId groundOverlayId) {
final GroundOverlay groundOverlay = groundOverlays[groundOverlayId]!;
final double current = groundOverlay.opacity;
setState(() {
groundOverlays[groundOverlayId] = groundOverlay.copyWith(
opacityParam: current < 0.1 ? 1.0 : current * 0.75,
);
});
}

void _changeBearing(GroundOverlayId groundOverlayId) {
final GroundOverlay groundOverlay = groundOverlays[groundOverlayId]!;
final double current = groundOverlay.bearing;
setState(() {
groundOverlays[groundOverlayId] = groundOverlay.copyWith(
bearingParam: current == 330.0 ? 0.0 : current + 30.0,
);
});
}

void _toggleVisible(GroundOverlayId groundOverlayId) {
final GroundOverlay groundOverlay = groundOverlays[groundOverlayId]!;
setState(() {
groundOverlays[groundOverlayId] = groundOverlay.copyWith(
visibleParam: !groundOverlay.visible,
);
});
}

@override
Widget build(BuildContext context) {
_createGroundOverlayImageFromAsset(context);
final GroundOverlayId? selectedId = selectedGroundOverlay;
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Center(
child: SizedBox(
width: 350.0,
height: 300.0,
child: GoogleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(-33.852, 151.211),
zoom: 15.0,
),
groundOverlays: groundOverlays.values.toSet(),
onMapCreated: _onMapCreated,
),
),
),
Expanded(
child: SingleChildScrollView(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
children: <Widget>[
Column(
children: <Widget>[
TextButton(
child: const Text('add'),
onPressed: _add,
),
TextButton(
child: const Text('remove'),
onPressed: (selectedId == null)
? null
: () => _remove(selectedId),
),
TextButton(
child: const Text('change transparency'),
onPressed: (selectedId == null)
? null
: () => _changeTransparency(selectedId),
),
TextButton(
child: const Text('change bearing'),
onPressed: (selectedId == null)
? null
: () => _changeBearing(selectedId),
),
TextButton(
child: const Text('toggle visible'),
onPressed: (selectedId == null)
? null
: () => _toggleVisible(selectedId),
),
],
),
],
)
],
),
),
),
],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export 'package:google_maps_flutter_platform_interface/google_maps_flutter_platf
TileOverlayId,
TileOverlay,
TileProvider,
WebGestureHandling;
WebGestureHandling,
GroundOverlay,
GroundOverlayUpdates,
GroundOverlayId;

part 'src/controller.dart';

part 'src/google_map.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class GoogleMapController {
.listen((MapTapEvent e) => _googleMapState.onTap(e.position));
GoogleMapsFlutterPlatform.instance.onLongPress(mapId: mapId).listen(
(MapLongPressEvent e) => _googleMapState.onLongPress(e.position));
GoogleMapsFlutterPlatform.instance
.onGroundOverlayTap(mapId: mapId)
.listen((GroundOverlayTapEvent e) =>
_googleMapState.onGroundOverlayTap(e.value));
}

/// Updates configuration options of the map user interface.
Expand Down Expand Up @@ -159,6 +163,19 @@ class GoogleMapController {
.clearTileCache(tileOverlayId, mapId: mapId);
}

/// Updates ground overlay configuration.
///
/// Change listeners are notified once the update has been made on the
/// platform side.
///
/// The returned [Future] completes after listeners have been notified.
Future<void> _updateGroundOverlays(
GroundOverlayUpdates groundOverlayUpdates) {
assert(groundOverlayUpdates != null);
return GoogleMapsFlutterPlatform.instance
.updateGroundOverlays(groundOverlayUpdates, mapId: mapId);
}

/// Starts an animated change of the map camera position.
///
/// The returned [Future] completes after the change has been started on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class GoogleMap extends StatefulWidget {
this.onTap,
this.onLongPress,
this.cloudMapId,
this.groundOverlays = const <GroundOverlay>{},
});

/// Callback method for when the map is ready to be used.
Expand Down Expand Up @@ -203,6 +204,9 @@ class GoogleMap extends StatefulWidget {
/// Tile overlays to be placed on the map.
final Set<TileOverlay> tileOverlays;

/// Ground overlays to be placed on the map.
final Set<GroundOverlay> groundOverlays;

/// Called when the camera starts moving.
///
/// This can be initiated by the following:
Expand Down Expand Up @@ -314,6 +318,8 @@ class _GoogleMapState extends State<GoogleMap> {
Map<PolygonId, Polygon> _polygons = <PolygonId, Polygon>{};
Map<PolylineId, Polyline> _polylines = <PolylineId, Polyline>{};
Map<CircleId, Circle> _circles = <CircleId, Circle>{};
Map<GroundOverlayId, GroundOverlay> _groundOverlays =
<GroundOverlayId, GroundOverlay>{};
late MapConfiguration _mapConfiguration;

@override
Expand All @@ -332,6 +338,7 @@ class _GoogleMapState extends State<GoogleMap> {
markers: widget.markers,
polygons: widget.polygons,
polylines: widget.polylines,
groundOverlays: widget.groundOverlays,
circles: widget.circles,
),
mapConfiguration: _mapConfiguration,
Expand All @@ -346,6 +353,7 @@ class _GoogleMapState extends State<GoogleMap> {
_polygons = keyByPolygonId(widget.polygons);
_polylines = keyByPolylineId(widget.polylines);
_circles = keyByCircleId(widget.circles);
_groundOverlays = keyByGroundOverlayId(widget.groundOverlays);
}

@override
Expand All @@ -368,6 +376,7 @@ class _GoogleMapState extends State<GoogleMap> {
_updatePolylines();
_updateCircles();
_updateTileOverlays();
_updateGroundOverlays();
}

Future<void> _updateOptions() async {
Expand Down Expand Up @@ -414,6 +423,14 @@ class _GoogleMapState extends State<GoogleMap> {
unawaited(controller._updateTileOverlays(widget.tileOverlays));
}

Future<void> _updateGroundOverlays() async {
final GoogleMapController controller = await _controller.future;
// ignore: unawaited_futures
controller._updateGroundOverlays(GroundOverlayUpdates.from(
_groundOverlays.values.toSet(), widget.groundOverlays));
_groundOverlays = keyByGroundOverlayId(widget.groundOverlays);
}

Future<void> onPlatformViewCreated(int id) async {
final GoogleMapController controller = await GoogleMapController.init(
id,
Expand Down Expand Up @@ -505,6 +522,18 @@ class _GoogleMapState extends State<GoogleMap> {
}
}

void onGroundOverlayTap(GroundOverlayId groundOverlayId) {
assert(groundOverlayId != null);
final GroundOverlay? groundOverlay = _groundOverlays[groundOverlayId];
if (groundOverlay == null) {
throw UnknownMapObjectIdError('groundOverlay', groundOverlayId, 'onTap');
}
final VoidCallback? onTap = groundOverlay.onTap;
if (onTap != null) {
onTap();
}
}

void onInfoWindowTap(MarkerId markerId) {
final Marker? marker = _markers[markerId];
if (marker == null) {
Expand Down
Loading