Skip to content

Commit

Permalink
Merge branch 'fitbounds_edgeinsets_padding' of ssh://github.com/csjam…
Browse files Browse the repository at this point in the history
…es/flutter_map into csjames-fitbounds_edgeinsets_padding
  • Loading branch information
johnpryan committed Nov 21, 2018
2 parents 000f307 + 7ca6307 commit e032f06
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
57 changes: 32 additions & 25 deletions example/lib/pages/animated_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class AnimatedMapControllerPage extends StatefulWidget {
}
}

class AnimatedMapControllerPageState extends State<AnimatedMapControllerPage> with TickerProviderStateMixin {
class AnimatedMapControllerPageState extends State<AnimatedMapControllerPage>
with TickerProviderStateMixin {
// Note the addition of the TickerProviderStateMixin here. If you are getting an error like
// 'The class 'TickerProviderStateMixin' can't be used as a mixin because it extends a class other than Object.'
// in your IDE, you can probably fix it by adding an analysis_options.yaml file to your project
Expand All @@ -34,23 +35,31 @@ class AnimatedMapControllerPageState extends State<AnimatedMapControllerPage> wi
mapController = new MapController();
}

void _animatedMapMove (LatLng destLocation, double destZoom) {
void _animatedMapMove(LatLng destLocation, double destZoom) {
// Create some tweens. These serve to split up the transition from one location to another.
// In our case, we want to split the transition be<tween> our current map center and the destination.
final _latTween = new Tween<double>(begin: mapController.center.latitude, end: destLocation.latitude);
final _lngTween = new Tween<double>(begin: mapController.center.longitude, end: destLocation.longitude);
final _zoomTween = new Tween<double>(begin: mapController.zoom, end: destZoom);
final _latTween = new Tween<double>(
begin: mapController.center.latitude, end: destLocation.latitude);
final _lngTween = new Tween<double>(
begin: mapController.center.longitude, end: destLocation.longitude);
final _zoomTween =
new Tween<double>(begin: mapController.zoom, end: destZoom);

// Create a new animation controller that has a duration and a TickerProvider.
AnimationController controller = AnimationController(duration: const Duration(milliseconds: 500), vsync: this);
AnimationController controller = AnimationController(
duration: const Duration(milliseconds: 500), vsync: this);
// The animation determines what path the animation will take. You can try different Curves values, although I found
// fastOutSlowIn to be my favorite.
Animation<double> animation = CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn);
Animation<double> animation =
CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn);

controller.addListener(() {
// Note that the mapController.move doesn't seem to like the zoom animation. This may be a bug in flutter_map.
mapController.move(LatLng(_latTween.evaluate(animation), _lngTween.evaluate(animation)), _zoomTween.evaluate(animation));
print("Location (${_latTween.evaluate(animation)} , ${_lngTween.evaluate(animation)}) @ zoom ${_zoomTween.evaluate(animation)}");
mapController.move(
LatLng(_latTween.evaluate(animation), _lngTween.evaluate(animation)),
_zoomTween.evaluate(animation));
print(
"Location (${_latTween.evaluate(animation)} , ${_lngTween.evaluate(animation)}) @ zoom ${_zoomTween.evaluate(animation)}");
});

animation.addStatusListener((status) {
Expand All @@ -63,7 +72,6 @@ class AnimatedMapControllerPageState extends State<AnimatedMapControllerPage> wi
});

controller.forward();

}

Widget build(BuildContext context) {
Expand All @@ -73,29 +81,29 @@ class AnimatedMapControllerPageState extends State<AnimatedMapControllerPage> wi
height: 80.0,
point: london,
builder: (ctx) => new Container(
key: new Key("blue"),
child: new FlutterLogo(),
),
key: new Key("blue"),
child: new FlutterLogo(),
),
),
new Marker(
width: 80.0,
height: 80.0,
point: dublin,
builder: (ctx) => new Container(
child: new FlutterLogo(
key: new Key("green"),
colors: Colors.green,
),
),
child: new FlutterLogo(
key: new Key("green"),
colors: Colors.green,
),
),
),
new Marker(
width: 80.0,
height: 80.0,
point: paris,
builder: (ctx) => new Container(
key: new Key("purple"),
child: new FlutterLogo(colors: Colors.purple),
),
key: new Key("purple"),
child: new FlutterLogo(colors: Colors.purple),
),
),
];

Expand Down Expand Up @@ -145,7 +153,7 @@ class AnimatedMapControllerPageState extends State<AnimatedMapControllerPage> wi
mapController.fitBounds(
bounds,
options: new FitBoundsOptions(
padding: new Point<double>(30.0, 0.0),
padding: new EdgeInsets.only(left: 15.0, right: 15.0),
),
);
},
Expand All @@ -160,12 +168,11 @@ class AnimatedMapControllerPageState extends State<AnimatedMapControllerPage> wi
center: new LatLng(51.5, -0.09),
zoom: 5.0,
maxZoom: 5.0,
minZoom: 3.0
),
minZoom: 3.0),
layers: [
new TileLayerOptions(
urlTemplate:
"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
subdomains: ['a', 'b', 'c']),
new MarkerLayerOptions(markers: markers)
],
Expand Down
2 changes: 1 addition & 1 deletion example/lib/pages/map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class MapControllerPageState extends State<MapControllerPage> {
mapController.fitBounds(
bounds,
options: new FitBoundsOptions(
padding: new Point<double>(30.0, 0.0),
padding: new EdgeInsets.only(left: 15.0, right: 15.0),
),
);
},
Expand Down
6 changes: 3 additions & 3 deletions lib/flutter_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ class MapOptions {
}

class FitBoundsOptions {
final Point<double> padding;
final EdgeInsets padding;
final double maxZoom;
final double zoom;

const FitBoundsOptions({
this.padding = const Point<double>(0.0, 0.0),
this.maxZoom = 17.0,
this.padding = const EdgeInsets.all(0.0),
this.maxZoom = 17.0, // TODO why is this?
this.zoom,
});
}
Expand Down
20 changes: 16 additions & 4 deletions lib/src/map/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:flutter_map/src/core/center_zoom.dart';
import 'package:flutter_map/src/core/point.dart';
import 'package:latlong/latlong.dart';

import 'package:flutter/material.dart';

class MapControllerImpl implements MapController {
Completer<Null> _readyCompleter = new Completer<Null>();
MapState _state;
Expand All @@ -27,7 +29,7 @@ class MapControllerImpl implements MapController {
void fitBounds(
LatLngBounds bounds, {
FitBoundsOptions options =
const FitBoundsOptions(padding: const Point(24.0, 24.0)),
const FitBoundsOptions(padding: EdgeInsets.all(12.0)),
}) {
_state.fitBounds(bounds, options);
}
Expand Down Expand Up @@ -154,10 +156,20 @@ class MapState {

CenterZoom _getBoundsCenterZoom(
LatLngBounds bounds, FitBoundsOptions options) {
var paddingTL = options.padding;
var paddingBR = options.padding;
var paddingTL = Point<double>(options.padding.left, options.padding.top);
var paddingBR =
Point<double>(options.padding.right, options.padding.bottom);

print(options.padding.left);
print(options.padding.right);
print(options.padding.top);
print(options.padding.bottom);

var paddingTotalXY = paddingTL + paddingBR;

print(paddingTotalXY);

var zoom = getBoundsZoom(bounds, paddingTL + paddingBR, inside: false);
var zoom = getBoundsZoom(bounds, paddingTotalXY, inside: false);
zoom = math.min(options.maxZoom, zoom);

var paddingOffset = (paddingBR - paddingTL) / 2;
Expand Down

0 comments on commit e032f06

Please sign in to comment.