ต่อจากเนื้อหาตอนที่แล้ว เกี่ยวกับการแสดงแผนที่ Google Map
ในแอป Flutter เราได้เตรียมความพร้อมในส่วนของการตั้งค่าต่างๆ
เรียบร้อยแล้ว เนื้อหาในตอนต่อไปนี้ เราจะมาต่อกันในเรื่องของการแสดง
แผนที่อย่างง่ายกัน
ทบทวนตอนที่แล้วได้ที่บทความ
เตรียมพร้อมสำหรับการแสดงแผนที่ด้วย Google Map Flutter ตอนที่ 1 http://niik.in/1127
แสดงแผนที่ Google Map ใน Flutter
ต่อไปนี้จะเป็นตัวอย่างโค้ดการแสดงแผนที่ใน flutter ด้วย google_map_flutter เราจะแสดง
แผนที่ที่ดึงพิกัดปัจจุบัน โดยใช้ geolocator มาใช้ในการขอตำแหน่ง แล้วแสดงในแผนที่ ในโค้ดตัวอย่าง
เราจะประยุกต์การสร้าง layer ต่างๆ แทนปุ่มควบคุม เพื่อให้สามารถนำไปประยุกต์ต่อได้ เช่น เราสร้าง
ปุ่ม zoom in และ zoom out รวมถึงปุ่ม ระบุพิกัดปัจจุบันหรือ myLocation ซึ่งจริงๆ แล้วปุ่มเหล่านี้
มีมาพร้อมกับตัวแผนที่อยู่แล้ว แต่เราจะปรับอะไรไม่ได้ เช่น ปรับตำแหน่ง หรือการทำงาน จึงไม่ยืดหยุ่น
ในการปรับใช้งานมากนัก ดังนั้น การสร้างปุ่มขึ้นมาควบคุมเอง ก็จะทำให้เราจัดการสิ่งต่างๆ ได้มากขึ้น
คำอธิบายแสดงในโค้ด
ไฟล์ map.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | import 'dart:async' ; import 'package:flutter/material.dart' ; import 'package:google_maps_flutter/google_maps_flutter.dart' ; import 'package:permission_handler/permission_handler.dart' ; import 'package:geolocator/geolocator.dart' ; class Maps extends StatefulWidget { static const routeName = '/map' ; const Maps({Key? key}) : super (key: key); @override State<StatefulWidget> createState() { return _MapsState(); } } class _MapsState extends State<Maps> { // กำนหนดตัวควบคุมให้กับแผนที่ สังเกตว่า มีการใช้แบบ Completer ซึงในภาษา Dart // เป็นคลาสที่ใช้ในการควบคุมการทำงานของ Future ดังนั้น ตัว controller จะถูกสร้างก็เมื่อตัวแผนที่ // โหลดเรียบร้อยแล้วเท่านั้น เพื่อให้พร้อมใช้งาน final Completer<GoogleMapController> _controller = Completer<GoogleMapController>(); // การกำหนดตำแหน่งต่างๆ จะใช้เป็นข้อมูล CameraPosition // ในตัวอย่างเรากำหนดตัวแปรไว้ 2 ตัว CameraPosition? _initialPosition; // ตัวแปรสำหรับตำแหน่งเริ่มต้น ยังไม่กำหนดค่าใดๆ // ตัวอย่งการกำหนดตำแหน่งแบบตายตัว สมมติเป็นห้าง มาบุญครอง static const CameraPosition _place_mbk = CameraPosition( bearing:0.0, // องศาทิศทางของกล้องตามเข็มนาฬิกาจากทิศเหนือ (หมุนแกนนอน) target: LatLng(13.745324385325134, 100.52999353076734), // พิกัด Lat Long tilt: 0.0, // องศามุมที่กล้องหันตรงไปที่พื้นโลก (หมุนแกนตั้ง) zoom: 14.0 // ระดะับการขยาย ); @override void initState() { super .initState(); // ขอสิทธิการเข้าถึงพิกัดตำแหน่ง requestPermissions(); _getCurrentLocation(); } // ขอ permission สำหรับจัดการตำแหน่ง void requestPermissions() async { await Permission.location.request(); } // ฟังก์ชันเพื่อดึงตำแหน่งปัจจุบันของผู้ใช้ Future<void> _getCurrentLocation() async { bool serviceEnabled; LocationPermission permission; // ตรวจสอบว่าเปิด Location Services ไว้หรือไม่ serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { // หาก Location Services ปิด แสดงข้อความหรือจัดการตามต้องการ return ; } // ตรวจสอบ permission สำหรับตำแหน่งที่ตั้ง permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { // Permission ถูกปฏิเสธ return ; } } if (permission == LocationPermission.deniedForever) { // Permission ถูกปฏิเสธอย่างถาวร return ; } // หาก permission ได้รับการอนุญาต ดึงตำแหน่งที่ตั้งปัจจุบัน Position position = await Geolocator.getCurrentPosition(); // กำหนดตำแหน่งเริ่มต้นเป็นตำแหน่งของผู้ใช้ setState(() { // กำหนดค่าให้กับตำแหน่งเริ่มต้น แล้วสร้างรูปแบบพิกัดไว้ใช้งาน // ค่า bearing และ tilt หากไม่กำหนด ก็จะใช้เป็น 0 ตามค่าเริ่มต้น ในที่นี้เราใช้แค่ // ตำแหน่ง กับ การ zoom _initialPosition = CameraPosition( target: LatLng(position.latitude, position.longitude), zoom: 14.0, ); }); } // ฟังก์ชั่นเฉพาะ กรณีเราต้องการให้ไปยังตำแหน่ง ที่เจาะจง ในที่นี้คือไปตำแหน่ง ห้าง มาบุญครอง Future<void> _goToMBK() async { final GoogleMapController controller = await _controller.future; await controller.animateCamera(CameraUpdate.newCameraPosition(_place_mbk)); } // ฟังก์ชั่นสำหรับ กรณีไปยังตำแหน่งใดๆ ที่ต้องการ ตามค่าที่ส่งเข้าไป Future<void> _goToPosition(CameraPosition postion) async { final GoogleMapController controller = await _controller.future; await controller.animateCamera(CameraUpdate.newCameraPosition(postion)); } @override void dispose() { super .dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text( 'แผนที่' ), ), body: _initialPosition == null // กรณียังไม่ได้ตำแหน่ง แสดงตัว Loading ก่อน ? const Center(child: CircularProgressIndicator()) // Loading หรือจะใช้รูปแบบอื่นๆ แทนได้ถ้าต้องการ : Stack( // ใช้ stack ในการวางซ้อนแผนที่ รองรับการเพิ่มเมนูไอคอนต่างๆ children: [ GoogleMap( mapType: MapType.normal, // normal | terrain | satellite | hybrid initialCameraPosition: _initialPosition!, // ใช้ค่าเริ่มต้นจากตำแหน่งของเรา onMapCreated: (GoogleMapController controller) { // ทำให้ Future สำเร็จเมื่อแผนที่ถูกสร้างเสร็จแล้ว // ตัว controller พร้อมใช้งาน _controller.complete(controller); }, // ในที่นี้เราจtซ่อนตัวปุ่ม zoom และกำหนดเอง จากปุ่มที่เราสร้าง zoomControlsEnabled: false , // ซ่อนค่าเริ่มต้นปุ่ม zoom // ในที่นี้เราจะซ่อนตัวปุม ตำแหน่งปัจจุบัน และกำหนดเองจากปุ่มที่เราสร้าง myLocationButtonEnabled: false , // ซ่อนปุ่มตำแหน่งปัจจุบัน myLocationEnabled: true , // แสดงรูป จุด ของตำแหน่งปัจจุบัน ), // ส่วนควบคุมการ Zoom ที่เราสร้างเอง Positioned( top: 16, right: 16, child: Column( children: [ FloatingActionButton( mini: true , onPressed: () async { // ควบคุมการ Zoom In final controller = await _controller.future; controller.animateCamera(CameraUpdate.zoomIn()); }, heroTag: 'zoomin' , // Set a unique tag ต้องกำหนด child: const Icon(Icons.add), ), const SizedBox(height: 8), FloatingActionButton( mini: true , onPressed: () async { // ควบคุมการ Zoom Out final controller = await _controller.future; controller.animateCamera(CameraUpdate.zoomOut()); }, heroTag: 'zoomout' , // Set a unique tag ต้องกำหนด child: const Icon(Icons.remove), ), ], ), ), // ส่วนควบคุมตำแหน่งปัจจุบัน ที่เราสร้างเอง Positioned( bottom: 16, right: 16, child: FloatingActionButton( onPressed: () { // ไปยังตำแหน่ง จากพิกัด _goToPosition(_initialPosition!); }, heroTag: 'mylocation' , // Set a unique tag ต้องกำหนด child: const Icon(Icons.my_location), ), ), ], ), ); } } |
ผลลัพธ์ที่ได้

ตัวอย่างด้านซ้าย เป็นกรณีเราใช้ค่าเริ่มต้นของแผนที่ ส่วนด้านขวาเป็นการใช้ตามโค้ดด้านบน ตัวอย่างนี้
เราสามารถประยุกต์ เช่น มีปุ่มแชร์บนขวา แล้วสามารถแชร์พิกัดของเราไปใช้งานต่อได้ ประยุกต์กับ
บทความด้านล่างนี้ได้
การแชร์ไฟล์ Sharing files ด้วย Share Plus ใน Flutter http://niik.in/1119
การแสดงแผนที่พร้อมตัว Marker และ infoWindow
ต่อไปเราจะประยุกต์เพิ่มเติม โดยการแสดงตัว marker ใช้รูปไอคอนใน asset โฟลเดอร์ เราสามารถ
กดค้างที่ตัว marker เพื่อลากไปยังตำแหน่งที่ต้องการได้ รวมทั้งแสดง infowindow ข้อมูลพิกัด ณ ที่
ตำแหน่งๆ นั้นด้วย ตัวอย่างอธิบายในโค้ด
ไฟล์ map.dart (เพิ่ม marker และ infowindow)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | import 'dart:async' ; import 'package:flutter/material.dart' ; import 'package:flutter/services.dart' ; // สำหรับใช้ rootBundle import 'package:google_maps_flutter/google_maps_flutter.dart' ; import 'package:permission_handler/permission_handler.dart' ; import 'package:geolocator/geolocator.dart' ; class Maps extends StatefulWidget { static const routeName = '/map' ; const Maps({Key? key}) : super (key: key); @override State<StatefulWidget> createState() { return _MapsState(); } } class _MapsState extends State<Maps> { // กำนหนดตัวควบคุมให้กับแผนที่ สังเกตว่า มีการใช้แบบ Completer ซึงในภาษา Dart // เป็นคลาสที่ใช้ในการควบคุมการทำงานของ Future ดังนั้น ตัว controller จะถูกสร้างก็เมื่อตัวแผนที่ // โหลดเรียบร้อยแล้วเท่านั้น เพื่อให้พร้อมใช้งาน final Completer<GoogleMapController> _controller = Completer<GoogleMapController>(); // การกำหนดตำแหน่งต่างๆ จะใช้เป็นข้อมูล CameraPosition // ในตัวอย่างเรากำหนดตัวแปรไว้ 2 ตัว CameraPosition? _initialPosition; // ตัวแปรสำหรับตำแหน่งเริ่มต้น ยังไม่กำหนดค่าใดๆ // ตัวอย่งการกำหนดตำแหน่งแบบตายตัว สมมติเป็นห้าง มาบุญครอง static const CameraPosition _place_mbk = CameraPosition( bearing:0.0, // องศาทิศทางของกล้องตามเข็มนาฬิกาจากทิศเหนือ (หมุนแกนนอน) target: LatLng(13.745324385325134, 100.52999353076734), // พิกัด Lat Long tilt: 0.0, // องศามุมที่กล้องหันตรงไปที่พื้นโลก (หมุนแกนตั้ง) zoom: 14.0 // ระดะับการขยาย ); // สร้าง Set ของ Marker final Set<Marker> _markers = {}; BitmapDescriptor? _markerIcon; // กำหนดไอคอนจากรูป final MarkerId _markerId = const MarkerId( 'current_location' ); LatLng? _activeMarkerPosition; // ตัวแปรสำหรับเก็บตำแหน่งของ Marker ที่ถูกลาก @override void initState() { super .initState(); // ขอสิทธิการเข้าถึงพิกัดตำแหน่ง requestPermissions(); _getCurrentLocation(); // เตรียมไอคอน prepareIcons(); } // ขอ permission สำหรับจัดการตำแหน่ง void requestPermissions() async { await Permission.location.request(); } // สร้างไอคอนจากรูปใน assets void prepareIcons() async { _markerIcon = await BitmapDescriptor.asset( const ImageConfiguration(size: Size(41.5, 48)), // ขนาดของไอคอนที่ต้องการแสดง // const ImageConfiguration(), // ขนาดของไอคอนที่ต้องการแสดงใช้ตามขนาดรูป 'assets/icon/marker.png' , // พาธของ asset ขนาดรูปตัวอย่างที่ทดสอบคือ 32x37 ); } // ฟังก์ชันเพื่อดึงตำแหน่งปัจจุบันของผู้ใช้ Future<void> _getCurrentLocation() async { bool serviceEnabled; LocationPermission permission; // ตรวจสอบว่าเปิด Location Services ไว้หรือไม่ serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { // หาก Location Services ปิด แสดงข้อความหรือจัดการตามต้องการ return ; } // ตรวจสอบ permission สำหรับตำแหน่งที่ตั้ง permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { // Permission ถูกปฏิเสธ return ; } } if (permission == LocationPermission.deniedForever) { // Permission ถูกปฏิเสธอย่างถาวร return ; } // หาก permission ได้รับการอนุญาต ดึงตำแหน่งที่ตั้งปัจจุบัน Position position = await Geolocator.getCurrentPosition(); // กำหนดตำแหน่งเริ่มต้นเป็นตำแหน่งของผู้ใช้ setState(() { // กำหนดค่าให้กับตำแหน่งเริ่มต้น แล้วสร้างรูปแบบพิกัดไว้ใช้งาน // ค่า bearing และ tilt หากไม่กำหนด ก็จะใช้เป็น 0 ตามค่าเริ่มต้น ในที่นี้เราใช้แค่ // ตำแหน่ง กับ การ zoom _initialPosition = CameraPosition( target: LatLng(position.latitude, position.longitude), zoom: 14.0, ); // อัปเดตตำแหน่งของ active marker _activeMarkerPosition = LatLng(position.latitude, position.longitude); // เพิ่ม Marker สำหรับตำแหน่งปัจจุบัน _markers.add( Marker( markerId: _markerId, position: _activeMarkerPosition!, icon: _markerIcon!, // กำหนดไอคอนจาก asset draggable: true , // เปิดใช้งานการลาก marker infoWindow: InfoWindow( title: 'ตำแหน่งของคุณ' , // ข้อความเพิ่มเติมที่จะแสดงใน InfoWindow snippet: '${_activeMarkerPosition!.latitude}, ${_activeMarkerPosition!.longitude}' , onTap: () async { print( "hide infowindow" ); final GoogleMapController controller = await _controller.future; controller.hideMarkerInfoWindow(_markerId); } ), onTap: () async { }, onDragEnd: (LatLng newPosition) { print( "Marker dropped at: ${newPosition.latitude}, ${newPosition.longitude}" ); // อัปเดตตำแหน่งของ active marker _activeMarkerPosition = LatLng(newPosition.latitude, newPosition.longitude); // สามารถจัดการตำแหน่งใหม่ได้ที่นี่ เช่น อัปเดตตำแหน่งในฐานข้อมูลหรือแสดงข้อมูลใหม่ CameraPosition _newPosition = CameraPosition( target: LatLng(newPosition.latitude, newPosition.longitude), zoom: 14.0, ); // เรียกใช้ ฟังก์ชั่นสำหรับ กรณีไปยังตำแหน่งใดๆ ที่ต้องการ ตามค่าที่ส่งเข้าไป _goToPosition(_newPosition); // เรียกใช้งานฟังก์ชัน อัปเดทตำแหน่งตัว markder และ ข้อความภายในที่แสดง _updateMarkerSnippet(_markerId,_activeMarkerPosition!); }, ), ); }); // เรียกใช้ GoogleMapController เพื่อแสดง InfoWindow อัตโนมัติ final GoogleMapController controller = await _controller.future; controller.showMarkerInfoWindow(_markerId); } // ฟังก์ชั่น อัปเดทตำแหน่งตัว markder และ ข้อความภายในที่แสดง void _updateMarkerSnippet(MarkerId markerId, LatLng newPosition) { // ตรวจสอบหาตัว markder เดิม ที่ไอดีตรงกับที่ส่งมา Marker? oldMarker = _markers.firstWhere( (marker) => marker.markerId == markerId, orElse: () => null as Marker, // ใช้ cast เป็น markder เพื่อเลี่ยง null safely ); if (oldMarker != null ) { // ถ้าไม่ใช่ null // ลบ markder ตัวเดิม _markers.remove(oldMarker); // กำหนดข้อมูลที่แสดงใน infowindow ใหม่ เป็นค่าพิกัดใหม่ String newSnippet = '${newPosition.latitude}, ${newPosition.longitude}' ; // เพิ่มตัว markder ตัวใหม่เข้าไป _markers.add( Marker( markerId: markerId, // ใช้ไอดีเดิม position: newPosition, // ตำแหน่งใหม่ icon: _markerIcon!, // กำหนดไอคอนจาก asset draggable: true , // เปิดใช้งานการลาก marker infoWindow: InfoWindow( title: oldMarker.infoWindow.title, // ใช้จากค่าเดิม snippet: newSnippet, // อัปเดทค่าใหม่ onTap: oldMarker.infoWindow.onTap, // ใช้จากค่าเดิม ), onTap: oldMarker.onTap, // ใช้จากค่าเดิม onDragEnd: oldMarker.onDragEnd, // ใช้จากค่าเดิม ), ); // rebuild เพื่ออัปเดทแผนที่ setState(() {}); } } // ฟังก์ชั่นเฉพาะ กรณีเราต้องการให้ไปยังตำแหน่ง ที่เจาะจง ในที่นี้คือไปตำแหน่ง ห้าง มาบุญครอง Future<void> _goToMBK() async { final GoogleMapController controller = await _controller.future; await controller.animateCamera(CameraUpdate.newCameraPosition(_place_mbk)); } // ฟังก์ชั่นสำหรับ กรณีไปยังตำแหน่งใดๆ ที่ต้องการ ตามค่าที่ส่งเข้าไป Future<void> _goToPosition(CameraPosition postion) async { final GoogleMapController controller = await _controller.future; await controller.animateCamera(CameraUpdate.newCameraPosition(postion)); // อัปเดทตำแหน่งตัว markder และ ข้อความภายในที่แสดง _updateMarkerSnippet(_markerId,postion.target!); // เรียกใช้ GoogleMapController เพื่อแสดง InfoWindow อัตโนมัติ controller.showMarkerInfoWindow(_markerId); } // ฟังก์ชั่นการกดที่แผนที่ แล้วไปยังตำแหน่งที่ต้องการ void _onMapTapped(LatLng position) { // อัปเดตตำแหน่งของ active marker _activeMarkerPosition = LatLng(position.latitude, position.longitude); // สามารถจัดการตำแหน่งใหม่ได้ที่นี่ เช่น อัปเดตตำแหน่งในฐานข้อมูลหรือแสดงข้อมูลใหม่ CameraPosition _newPosition = CameraPosition( target: LatLng(position.latitude, position.longitude), zoom: 14.0, ); // เรียกใช้ ฟังก์ชั่นสำหรับ กรณีไปยังตำแหน่งใดๆ ที่ต้องการ ตามค่าที่ส่งเข้าไป _goToPosition(_newPosition); } @override void dispose() { super .dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text( 'แผนที่' ), ), body: _initialPosition == null // กรณียังไม่ได้ตำแหน่ง แสดงตัว Loading ก่อน ? const Center(child: CircularProgressIndicator()) // Loading หรือจะใช้รูปแบบอื่นๆ แทนได้ถ้าต้องการ : Stack( // ใช้ stack ในการวางซ้อนแผนที่ รองรับการเพิ่มเมนูไอคอนต่างๆ children: [ GoogleMap( mapType: MapType.normal, // normal | terrain | satellite | hybrid initialCameraPosition: _initialPosition!, // ใช้ค่าเริ่มต้นจากตำแหน่งของเรา onMapCreated: (GoogleMapController controller) { // ทำให้ Future สำเร็จเมื่อแผนที่ถูกสร้างเสร็จแล้ว // ตัว controller พร้อมใช้งาน _controller.complete(controller); }, // ในที่นี้เราจtซ่อนตัวปุ่ม zoom และกำหนดเอง จากปุ่มที่เราสร้าง zoomControlsEnabled: false , // ซ่อนค่าเริ่มต้นปุ่ม zoom // ในที่นี้เราจะซ่อนตัวปุม ตำแหน่งปัจจุบัน และกำหนดเองจากปุ่มที่เราสร้าง myLocationButtonEnabled: false , // ซ่อนปุ่มตำแหน่งปัจจุบัน myLocationEnabled: false , // แสดงรูป จุด ของตำแหน่งปัจจุบัน markers: _markers, // เพิ่ม markers ใน GoogleMap onTap: _onMapTapped, // เมื่อกดที่แผนที่ให้ทำคำสั่ง ), // ส่วนควบคุมการ Zoom ที่เราสร้างเอง Positioned( top: 16, right: 16, child: Column( children: [ FloatingActionButton( mini: true , onPressed: () async { // ควบคุมการ Zoom In final controller = await _controller.future; controller.animateCamera(CameraUpdate.zoomIn()); }, heroTag: 'zoomin' , // Set a unique tag ต้องกำหนด child: const Icon(Icons.add), ), const SizedBox(height: 8), FloatingActionButton( mini: true , onPressed: () async { // ควบคุมการ Zoom Out final controller = await _controller.future; controller.animateCamera(CameraUpdate.zoomOut()); }, heroTag: 'zoomout' , // Set a unique tag ต้องกำหนด child: const Icon(Icons.remove), ), ], ), ), // ส่วนควบคุมตำแหน่งปัจจุบัน ที่เราสร้างเอง Positioned( bottom: 16, right: 16, child: FloatingActionButton( onPressed: () { // ไปยังตำแหน่ง จากพิกัด _goToPosition(_initialPosition!); }, heroTag: 'mylocation' , // Set a unique tag ต้องกำหนด child: const Icon(Icons.my_location), ), ), ], ), ); } } |
ผลลัพธ์ที่ได้

เราสามารถกดค้างที่ตัว marker เพื่อย้ายตำแหน่งในแผนที่ ไปยังจุดที่ต้องการได้ เมื่อวางตัว marker
ลงก็จะทำการอัปเดทตำแหน่งทั้งตัว marker และ ค่า พิกัดของ marker ใน infowindow ด้วย
ซึ่งในรูปแบบการทำงาน จะเป็นการลบตัว marker เดิมแล้วสร้างใหม่ เพราะตัว marker เองจะไม่
สามารถแก้ไขค่าโดยตรงได้ การปรับค่าต่างๆ จะทำได้ยากกว่าการใช้งานแผนที่ในเว็บ ดังนั้นวิธีที่ใช้
จะเป็นการลบตัวเดิม แล้วใส่ตัวใหม่เข้าไป โดยยังให้คงบางค่าที่ต้องการจาก marker ตัวเดิมได้
ในตัวอย่างเราเพิ่มตัว marker แค่ตัวเดียว ซึ่งจะจัดการง่ายและไม่ซับซ้อนมากนัก เมื่อกดค้าง แล้ว
ลากไปยังจุดที่ต้องการ ก็จะย้ายตำแหน่งพร้อมแสดง infowindow ระบุตำแหน่งปัจจุบัน และถ้ากดที่ปุ่ม
mylocation ก็จะกลับมาตำแหน่งพิกัดปัจจุบันของเรานั่นเอง นอกจากนั้น เพื่อไม่ให้ต้องลากตัว
marker ไกลๆ ในบางกรณี เราเพิ่มส่วนของคำส่ั่งเมื่อ กดที่ตำแหน่งใดๆ ในแผนที่ เราก็จะให้ย้ายตัว
marker ไปยังตำแหน่งนั้นๆ ได้อีกด้วย สะดวกในการย้ายตัว marker ให้เข้าไปใกล้บริเวณที่ต้องการ
ก่อนกดลากอีกที สามารถนำไปปรับประยุกต์เพิ่มเติมได้
เนื้อหาเกี่ยวกับการใช้งานแผนที่ด้วย google_map_flutter ยังมีเพิ่มรอติดตามตอนหน้าจะเป็นอะไร
หวังว่าจะเป็นประโยชน์ในการนำไปปรับประยุกต์ใช้งานต่อไปไม่มากก็น้อย