vtmap_gl 4.0.1-dev.3
vtmap_gl: ^4.0.1-dev.3 copied to clipboard
A Flutter plugin for integrating Mapbox Maps inside a Flutter application on Android, iOS and web platfroms.
example/lib/main.dart
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'animate_camera.dart';
import 'full_map.dart';
import 'line.dart';
import 'map_ui.dart';
import 'move_camera.dart';
import 'page.dart';
import 'place_circle.dart';
import 'place_source.dart';
import 'place_symbol.dart';
import 'place_fill.dart';
import 'scrolling_map.dart';
final List<ExamplePage> _allPages = <ExamplePage>[
MapUiPage(),
FullMapPage(),
AnimateCameraPage(),
MoveCameraPage(),
PlaceSymbolPage(),
PlaceSourcePage(),
LinePage(),
PlaceCirclePage(),
PlaceFillPage(),
ScrollingMapPage(),
];
class MapsDemo extends StatelessWidget {
//FIXME: Add your Mapbox access token here
// static const String ACCESS_TOKEN = "pk.eyJ1IjoibGVwcHJvOTAiLCJhIjoiY2s4cG94N2hpMDU4MTNlcGdvY2gyamQwayJ9.LrwJ0gNZ6ncU7yLeaHKxAQ";
static const String ACCESS_TOKEN = "6ht5fdbc-1996-4f54-87gf-5664f304f3d2";
final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance;
bool positionStreamStarted = false;
// void _pushPage(BuildContext context, ExamplePage page) async {
// // if (!kIsWeb) {
// // final location = Location();
// // final hasPermissions = await location.hasPermission();
// // if (hasPermissions != PermissionStatus.granted) {
// // await location.requestPermission();
// // }
// final hasPermission = await _handlePermission();
// if (!hasPermission) {
// return;
// }
// // }
// Navigator.of(context).push(MaterialPageRoute<void>(
// builder: (_) => Scaffold(
// appBar: AppBar(title: Text(page.title)),
// body: page,
// )));
// }
Future<void> _pushPage(BuildContext context, ExamplePage page) async {
final ok = await _ensureLocationReady();
if (!ok) {
// Gợi ý: báo nhẹ cho user
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Cần bật Location và cấp quyền để tiếp tục.')),
);
}
return;
}
if (!context.mounted) return; // tránh gọi Navigator khi widget đã dispose
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => Scaffold(
appBar: AppBar(title: Text(page.title)),
body: page,
),
),
);
}
Future<bool> _ensureLocationReady() async {
final geo = GeolocatorPlatform.instance;
// 1) Dịch vụ vị trí phải bật
var serviceEnabled = await geo.isLocationServiceEnabled();
if (!serviceEnabled) {
// Thử mở phần cài đặt để user bật
await Geolocator.openLocationSettings();
serviceEnabled = await geo.isLocationServiceEnabled();
if (!serviceEnabled) return false;
}
// 2) Quyền truy cập
LocationPermission permission = await geo.checkPermission();
if (permission == LocationPermission.denied) {
permission = await geo.requestPermission();
}
if (permission == LocationPermission.deniedForever) {
// Không thể request thêm → mở App Settings cho user tự bật
await Geolocator.openAppSettings();
return false;
}
if (permission == LocationPermission.denied ||
permission == LocationPermission.unableToDetermine) {
return false;
}
// (Optional) Nếu bạn cần chính xác cao, có thể kiểm tra thêm:
// - iOS có "Precise" vs "Approximate"
// - Android 12+ có approximate; đa phần app vẫn OK
return true;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('MapboxMaps examples')),
body: ListView.builder(
itemCount: _allPages.length,
itemBuilder: (_, int index) => ListTile(
leading: _allPages[index].leading,
title: Text(_allPages[index].title),
onTap: () => _pushPage(context, _allPages[index])),
),
);
}
}
void main() {
runApp(MaterialApp(home: MapsDemo()));
}