How to get bounds of visible map with Flutter Google Map plugin

In this Google Flutter code snippet example, we are going to learn How to get bounds of visible map with flutter google maps plugin.

You can copy and adopt this source code example to your flutter project easily without issues.

Add this code to your pubspec.yaml file

permission_handler: ^3.0.1
google_maps_flutter: ^0.5.19

Create a get_visible_bounds.dart file (use any name of your choice) and add this code:

import 'dart:async';

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

class GetVisibleBounds extends StatefulWidget {
  @override
  _GetVisibleBoundsState createState() => _GetVisibleBoundsState();
}

class _GetVisibleBoundsState extends State<GetVisibleBounds> {
  Completer<GoogleMapController> _controller = Completer();
  GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  static const LatLng _center = const LatLng(45.521563, -122.677433);

  LatLng _lastMapPosition = _center;


  void _onCameraMove(CameraPosition position) {
    _lastMapPosition = position.target;
  }

  void _onMapCreated(GoogleMapController controller) async  {
    _controller.complete(controller);
    LatLngBounds latLngBounds = await controller.getVisibleRegion();
    _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("NorthEast: ${latLngBounds.northeast.latitude}, ${latLngBounds.northeast.longitude}. SouthWest: ${latLngBounds.southwest.latitude}, ${latLngBounds.southwest.longitude}")));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
          title: Text('Visible Bounds of Map'),
          backgroundColor: Colors.red,
        ),
        body: Stack(
          children: <Widget>[
            GoogleMap(
              onMapCreated: _onMapCreated,
              myLocationEnabled: true,
              initialCameraPosition: CameraPosition(
                target: _center,
                zoom: 11.0,
              ),
              onCameraMove: _onCameraMove,
            ),
            /* Padding(
              padding: const EdgeInsets.all(16.0),
              child: Align(
                alignment: Alignment.topRight,
                child: Column(
                  children: <Widget> [
                    FloatingActionButton(
                      onPressed: _onGetBoundsButtonPressed,
                      materialTapTargetSize: MaterialTapTargetSize.padded,
                      backgroundColor: Colors.red,
                      child: const Icon(Icons.map, size: 36.0),
                    ),
                    
                  ],
                ),
              ),
            ), */
          ],
        ),
      ),
    );
  }
}

In your main.dart file (main entry point of the app), add this:

import 'package:inducesmile/get_visible_bounds.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Code Snippets',
      theme: new ThemeData(primarySwatch: Colors.red),
      home: new GetVisibleBounds(),
    );
  }
}

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment