Skip to content

Added map move event, added zoom & bounds attributes #49

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion lib/src/core/google_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart';

import 'package:google_directions_api/google_directions_api.dart'
show GeoCoord, DirectionsService;
import 'package:google_maps_flutter/google_maps_flutter.dart' as gmap;

import 'map_items.dart';
import 'map_operations.dart';
Expand All @@ -27,6 +28,7 @@ class GoogleMap extends StatefulWidget {
this.markers,
this.onTap,
this.onLongPress,
this.onMapMove,
this.interactive = true,
this.initialZoom = _zoom,
this.mapType = MapType.roadmap,
Expand Down Expand Up @@ -85,6 +87,8 @@ class GoogleMap extends StatefulWidget {
/// For `web` this will be called when `right mouse clicked`.
final ValueChanged<GeoCoord> onLongPress;

final ValueChanged<gmap.CameraPosition> onMapMove;

/// Set of mobile map preferences.
final MobileMapPreferences mobilePreferences;

Expand Down Expand Up @@ -117,4 +121,4 @@ abstract class GoogleMapStateBase extends State<GoogleMap>
icon.endsWith('/marker_a.png') || icon.endsWith('/marker_b.png')
? 'packages/flutter_google_maps/'
: '';
}
}
5 changes: 5 additions & 0 deletions lib/src/core/google_map.state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'map_items.dart';
import 'google_map.dart';

class GoogleMapState extends GoogleMapStateBase {

@override
void moveCameraBounds(
GeoCoordBounds newBounds, {
Expand Down Expand Up @@ -42,6 +43,10 @@ class GoogleMapState extends GoogleMapStateBase {
@override
FutureOr<GeoCoord> get center => throw UnimplementedError();

FutureOr<double> get zoom => throw UnimplementedError();

FutureOr<GeoCoordBounds> get bounds => throw UnimplementedError();

@override
void changeMapStyle(
String mapStyle, {
Expand Down
6 changes: 6 additions & 0 deletions lib/src/core/map_operations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ abstract class MapOperations implements MapMarkers, MapDirections, MapPolygons {

/// Gets center coordinates of the map.
FutureOr<GeoCoord> get center;

/// Gets zoom coordinates of the map.
FutureOr<double> get zoom;

/// Gets zoom coordinates of the map.
FutureOr<GeoCoordBounds> get bounds;

/// Sets the styling of the base map.
///
Expand Down
7 changes: 7 additions & 0 deletions lib/src/mobile/google_map.state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ class GoogleMapState extends gmap.GoogleMapStateBase {
FutureOr<GeoCoord> get center async =>
(await _controller?.getVisibleRegion())?.toGeoCoordBounds()?.center;

FutureOr<double> get zoom async =>
(await _controller?.getZoomLevel());

FutureOr<GeoCoordBounds> get bounds async => (await _controller?.getVisibleRegion()).toGeoCoordBounds();

@override
void changeMapStyle(
String mapStyle, {
Expand Down Expand Up @@ -504,6 +509,8 @@ class GoogleMapState extends gmap.GoogleMapStateBase {
onTap: (coords) => widget.onTap?.call(coords?.toGeoCoord()),
onLongPress: (coords) =>
widget.onLongPress?.call(coords?.toGeoCoord()),
onCameraMove: (position) =>
widget.onMapMove?.call(position),
onMapCreated: (GoogleMapController controller) {
_controller = controller;
_controller.setMapStyle(widget.mapStyle);
Expand Down
18 changes: 16 additions & 2 deletions lib/src/web/google_map.state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:ui' as ui;

import 'package:flutter/widgets.dart';
import 'package:flutter/scheduler.dart' show SchedulerBinding;
import 'package:google_maps_flutter/google_maps_flutter.dart' as gmaps;

import 'package:uuid/uuid.dart';
import 'package:flinq/flinq.dart';
Expand Down Expand Up @@ -62,13 +63,13 @@ class GoogleMapState extends GoogleMapStateBase {

_map.center = newBounds.center.toLatLng();

final zoom = _map.zoom;
// final zoom = _map.zoom;
if (animated == true) {
_map.panToBounds(newBounds.toLatLngBounds());
} else {
_map.fitBounds(newBounds.toLatLngBounds());
}
_map.zoom = zoom;
// _map.zoom = zoom;
}

@override
Expand Down Expand Up @@ -115,6 +116,10 @@ class GoogleMapState extends GoogleMapStateBase {
@override
FutureOr<GeoCoord> get center => _map.center?.toGeoCoord();

FutureOr<double> get zoom => _map.zoom.toDouble();

FutureOr<GeoCoordBounds> get bounds => _map.bounds.toGeoCoordBounds();

@override
void changeMapStyle(
String mapStyle, {
Expand Down Expand Up @@ -553,6 +558,15 @@ class GoogleMapState extends GoogleMapStateBase {

_map = GMap(elem, _mapOptions);

_subscriptions.add(_map.onCenterChanged.listen(
(event) {
gmaps.CameraPosition pos = gmaps.CameraPosition(
target: gmaps.LatLng( _map.center.lat, _map.center.lng),
zoom: _map.zoom.toDouble(),
tilt: _map.tilt,
);
widget.onMapMove?.call(pos);
}));
_subscriptions.add(_map.onClick.listen(
(event) => widget.onTap?.call(event?.latLng?.toGeoCoord())));
_subscriptions.add(_map.onRightclick.listen(
Expand Down