我正在尝试从 aa json 数据中获取标记的纬度和经度,并将这些标记放入给定的字段中。我可以在地图上看到动态标记,但看不到从 json 获取的动态标记。我正在等待一些方法,但仍然无法获得任何 json 数据。这是我的代码:
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
CameraPosition _initialLocation = CameraPosition(target: LatLng(0.0, 0.0));
GoogleMapController mapController;
Position _currentPosition;
// Method for retrieving the current location
_getCurrentLocation() async {
await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
.then((Position position) async {
setState(() {
_currentPosition = position;
print('CURRENT POS: $_currentPosition');
mapController.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(position.latitude, position.longitude),
zoom: 18.0,
),
),
);
});
}).catchError((e) {
print(e);
});
print(_currentPosition.latitude);
}
// Dynamic List
List<Location> locations = [
Location(LatLng(38.728496, 35.511437)),
Location(LatLng(38.7287631, 35.5127753)),
Location(LatLng(38.731411, 35.529966)),
Location(LatLng(38.72741, 35.52776))
];
// Two Lists, one for lat one for lon
List<double> a = [];// lat
List<double> b = [];// lon
getLatLon() async{
await getStops();// json data
for(int i = 0; i < api.length; i++){
a.add(api[i]["latitude"]);
b.add(api[i]["longitude"]);
print(a[i]);
}
}
getLocations() {
getLatLon();
for (int i = 0; i < a.length; i++) {
locations.add(Location(LatLng(a[i], b[i])));// fill locations list
}
}
// get json data
List api;
Future<String> getStops() async {
var response = await http.get(
Uri.parse(
"http://kaktusmobile.kayseriulasim.com.tr/api/rest/busstops/borders=38.732972,35.508576,38.735353,35.526429,38.722087,35.510464,38.726996,35.530301"),
headers: {"Accept": "application/json"});
this.api = jsonDecode(response.body);
return "success";
}
// create markers
List<MapMarker> mapMarkers = [];
List<Marker> customMarkers = [];
List<Marker> mapBitmapsToMarkers(List<Uint8List> bitmaps) {
getLocations();
bitmaps.asMap().forEach((i, bmp) {
customMarkers.add(Marker(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomePage()));
},
markerId: MarkerId("$i"),
position: locations[i].coordinates,
icon: BitmapDescriptor.fromBytes(bmp),
));
});
}
@override
void initState() {
super.initState();
_getCurrentLocation();
getLocations();
MarkerGenerator(markerWidgets(), (bitmaps) {
setState(() {
mapBitmapsToMarkers(bitmaps);
});
}).generate(context);
}
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
// create mapps[![enter image description here][1]][1]
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
GoogleMap(
onMapCreated: _onMapCreated,
markers: customMarkers.toSet(),
initialCameraPosition: _initialLocation,
),
],
),
);
}