gpt4 book ai didi

google-maps - flutter 贴图,setState 不更新标记和多边形

转载 作者:行者123 更新时间:2023-12-03 04:59:08 28 4
gpt4 key购买 nike

在初始化 map 时直接给出标记和多边形似乎可以工作,但是在调用 setState 方法时不会添加多边形和标记。

我不知道我似乎错过了什么,

以下是我迄今为止尝试过的。

有人可以帮助解决我所缺少的 map 未更新的问题。

  import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';

class HomeWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MyHome();
}

class _MyHome extends State<HomeWidget> {
Completer<GoogleMapController> _controller = Completer();

double _latitude;
double _longitude;

CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(13.092191, 80.246994), zoom: 19.151926040649414);

@override
void initState() {
this._getLocation().then((value) {
this._latitude = value.latitude;
this._longitude = value.longitude;
_kGooglePlex = CameraPosition(
target: LatLng(_latitude, _longitude),
zoom: 14.4746,
);
});
super.initState();
}

@override
void dispose() {
super.dispose();
}

Map<String, double> userLocation;
LocationData currentLocation;

var location = new Location();
Set<Marker> _markers = Set();
Set<Polygon> _polygons = Set();
GoogleMap map;

CameraPosition _kLake =
CameraPosition(target: LatLng(13.092191, 80.246994), tilt: 0, zoom: 14);
@override
Widget build(BuildContext context) {
map = GoogleMap(
mapType: MapType.hybrid,
polygons: _polygons,
markers: _markers,
initialCameraPosition: _kGooglePlex,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
addMarker(LatLng(_latitude, _longitude));
},
onTap: _addPolygon,
);
return new Scaffold(
body: map,
);
}

List<LatLng> polypoints = List();
_addPolygon(LatLng points) {
assert(points != null);
polypoints.add(points);
if (polypoints.length > 4) {
print("called polygon");
print(polypoints);
setState(() {
_polygons.add(Polygon(
polygonId: PolygonId('test'),
points: polypoints,
strokeColor: Colors.blue));
});
polypoints.clear();
}
}

void addMarker(LatLng mLatLng) {
setState(() {
_markers.add(Marker(
// This marker id can be anything that uniquely identifies each marker.
markerId: MarkerId('title'),
position: mLatLng,
infoWindow: InfoWindow(
title: 'someTile',
snippet: 'some desc',
),
icon: BitmapDescriptor.defaultMarker));
});
}


Future<LocationData> _getLocation() async {
try {
currentLocation = await location.getLocation();
} catch (e) {
if (e.code == 'PERMISSION_DENIED') {
print('Permission denied');
}
}
return currentLocation;
}
}

最佳答案

经过这么多时间,我似乎找到了自己问题的解决方案

问题似乎出在 polypoints.clear();

它在 UI 中呈现多边形之前清除了多边形,因此抛出了一个我没有注意到的异常。

关于google-maps - flutter 贴图,setState 不更新标记和多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59945362/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com