Skip to content

Instantly share code, notes, and snippets.

@yasudacloud
Last active March 19, 2022 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasudacloud/1577a52a20acee956eb441ed1954c29e to your computer and use it in GitHub Desktop.
Save yasudacloud/1577a52a20acee956eb441ed1954c29e to your computer and use it in GitHub Desktop.
Flutter Leaflet sample
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final baseX = 2;
final baseY = 2;
Widget _buildTile(BuildContext context, Widget tileWidget, Tile tile) {
if (tile.coords.z == 2.0) {
if (tile.coords.x == baseX && tile.coords.y == baseY) {
return _buildTileAsset(asset: "assets/zoom2.png", coords: tile.coords);
}
} else if (tile.coords.z == 3.0) {
if (tile.coords.x == baseX * 2 && tile.coords.y == baseY * 2) {
return _buildTileAsset(asset: "assets/zoom3.png", coords: tile.coords);
}
} else {
if (tile.coords.x == baseX * 4 && tile.coords.y == baseY * 4) {
return _buildTileAsset(asset: "assets/zoom4_topleft.png", coords: tile.coords);
}
if (tile.coords.x == (baseX * 4) + 1 && tile.coords.y == baseY * 4) {
return _buildTileAsset(asset: "assets/zoom4_topright.png", coords: tile.coords);
}
if (tile.coords.x == baseX * 4 && tile.coords.y == (baseY * 4) + 1) {
return _buildTileAsset(asset: "assets/zoom4_bottomleft.png", coords: tile.coords);
}
if (tile.coords.x == (baseX * 4) + 1 && tile.coords.y == (baseY * 4) + 1) {
return _buildTileAsset(asset: "assets/zoom4_bottomright.png", coords: tile.coords);
}
}
return _buildTileAsset(asset: "", coords: tile.coords);
}
Widget _buildTileAsset({required String asset, required Coords<double> coords}) {
return InkWell(
child: asset.isEmpty ? Container() : Image.asset(asset, fit: BoxFit.cover),
onTap: () {
final x = coords.x;
final y = coords.y;
final z = coords.z;
print("x: $x, y: $y, z: $z");
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Mapのテスト"),
),
body: FlutterMap(
options: MapOptions(
center: LatLng(0.0, 0.0),
zoom: 3,
minZoom: 2,
maxZoom: 4,
interactiveFlags: InteractiveFlag.pinchZoom | InteractiveFlag.drag | InteractiveFlag.flingAnimation,
),
layers: [
TileLayerOptions(
backgroundColor: Colors.white,
tileFadeInDuration: 0,
evictErrorTileStrategy: EvictErrorTileStrategy.none,
urlTemplate: "assets/zoom3.png",
tileProvider: const AssetTileProvider(),
tileBuilder: _buildTile),
],
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment