// Copyright 2013 The Flutter 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 'dart:typed_data';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:http/http.dart' as http;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple)),
home: const GroundOverlayBody(),
);
}
}
class GroundOverlayBody extends StatefulWidget {
const GroundOverlayBody({super.key});
@override
State<StatefulWidget> createState() => GroundOverlayBodyState();
}
class GroundOverlayBodyState extends State<GroundOverlayBody> {
GroundOverlayBodyState();
GoogleMapController? controller;
GroundOverlay? _groundOverlay;
void _onMapCreated(GoogleMapController controller) {
this.controller = controller;
}
void _removeGroundOverlay() {
setState(() {
_groundOverlay = null;
});
}
Future<void> _addGroundOverlay() async {
final imageUrl = 'https://picsum.photos/200/300';
final assetMapBitmap = await _getNetworkBitmap(imageUrl);
try {
final GroundOverlay groundOverlay = GroundOverlay.fromBounds(
groundOverlayId: GroundOverlayId('ground_overlay'),
image: assetMapBitmap,
bounds: LatLngBounds(southwest: const LatLng(37.41, -122.09), northeast: const LatLng(37.423, -122.07)),
onTap: () {},
);
setState(() {
_groundOverlay = groundOverlay;
});
} catch (e) {
print(e);
}
}
Future<BytesMapBitmap> _getNetworkBitmap(String url) async {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final Uint8List bytes = response.bodyBytes;
BytesMapBitmap bytesMapBitmap = BytesMapBitmap(bytes, bitmapScaling: MapBitmapScaling.none);
return bytesMapBitmap;
} else {
throw Exception('Failed to load image from network');
}
}
@override
Widget build(BuildContext context) {
final Set<GroundOverlay> overlays = <GroundOverlay>{if (_groundOverlay != null) _groundOverlay!};
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(37.422026, -122.085329), zoom: 14.0),
groundOverlays: overlays,
onMapCreated: _onMapCreated,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
TextButton(onPressed: _groundOverlay == null ? _addGroundOverlay : null, child: const Text('Add')),
TextButton(onPressed: _groundOverlay != null ? _removeGroundOverlay : null, child: const Text('Remove')),
],
),
],
);
}
}
Steps to reproduce
Expected results
Display the ground overlay on the map
Actual results
No exception or error but doesn't display the ground overlay
Code sample
Code sample
Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
[Paste your logs here]Flutter Doctor output
Doctor output