gpt4 book ai didi

flutter - 用户如何在Google Maps Flutter上添加多个标记

转载 作者:行者123 更新时间:2023-12-03 04:15:39 25 4
gpt4 key购买 nike

当用户长按 map 时,用户如何添加多个标记。在这段代码中,我完成了单张 map 。当用户长按 map 时,它将自动添加一个标记,但是我不知道如何在 map 上添加多个标记。我尝试了很多代码,但没有得到正确的结果。希望你理解这个问题。您的小小的帮助可以使我过得愉快。提前致谢。

这是我尝试过的代码:)

import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'dart:async';

import 'package:google_maps_flutter/google_maps_flutter.dart';

void main() => runApp(MyApp());

const kGoogleApiKey = "API_KEY";

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title: "MAP", home: BspAddressmapscreen());
}
}

class BspAddressmapscreen extends StatefulWidget {
BspAddressmapscreen({Key key}) : super(key: key);

@override
_BspAddressmapscreenState createState() => _BspAddressmapscreenState();
}

class _BspAddressmapscreenState extends State<BspAddressmapscreen> {
final homeScaffoldKey = GlobalKey<ScaffoldState>();
Completer<GoogleMapController> _controller = Completer();

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

double zoomVal = 5.0;

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
/*NavigationHelper.navigatetoBack(context);*/
}),
centerTitle: true,
title: Text("Business Address Detail"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {},
),
],
),
bottomNavigationBar: Container(
color: Colors.transparent,
height: 56,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new FlatButton.icon(
icon: Icon(Icons.arrow_back_ios),
label: Text('Show Address'),
textColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7),
),
onPressed: () {
getUserLocation();
},
),
],
),
),
body: Container(
height: double.infinity,
width: double.infinity,
child: Stack(
children: <Widget>[
_searchbar(),
_buildGoogleMap(context),
_zoomminusfunction(),
_zoomplusfunction(),
],
),
),
);
}

getUserLocation() async {
markers.values.forEach((value) async {
print(value.position);
// From coordinates
final coordinates =
new Coordinates(value.position.latitude, value.position.longitude);
var addresses = await Geocoder.google(kGoogleApiKey)
.findAddressesFromCoordinates(coordinates);

print("Address: ${addresses.first.featureName}");
print("Address: ${addresses.first.adminArea}");
});
}

Future<void> _minus(double zoomVal) async {
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(40.712776, -74.005974), zoom: zoomVal)));
}

Future<void> _plus(double zoomVal) async {
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(40.712776, -74.005974), zoom: zoomVal)));
}

Map<MarkerId, Marker> markers = <MarkerId, Marker>{};

Widget _buildGoogleMap(BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: GoogleMap(
mapType: MapType.normal,
initialCameraPosition:
CameraPosition(target: LatLng(40.712776, -74.005974), zoom: 12),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
markers: Set<Marker>.of(markers.values),
onLongPress: (LatLng latLng) {
// creating a new MARKER

final MarkerId markerId = MarkerId('4544');
final Marker marker = Marker(
markerId: markerId,
position: latLng,
);

setState(() {
markers.clear();
// adding a new marker to map
markers[markerId] = marker;
});
},
),
);
}

Widget _searchbar() {
return Positioned(
top: 50.0,
right: 15.0,
left: 15.0,
child: Container(
height: 50.0,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0), color: Colors.white),
child: TextField(
decoration: InputDecoration(
hintText: 'Enter Address',
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 15.0, top: 15.0),
suffixIcon: IconButton(
icon: Icon(Icons.search),
//onPressed: searchandNavigate,
onPressed: () {},
iconSize: 30.0,
),
),
onChanged: (val) {
setState(() {
// searchAddr = val;
});
},
),
),
);
}
}

最佳答案

让我们从创建一个帮助程序功能开始,该功能创建一个新的标记并将其添加到标记图。

 onLongPress: (LatLng latLng) {
// creating a new MARKER

var markerIdVal = markers.length + 1;
String mar = markerIdVal.toString();
final MarkerId markerId = MarkerId(mar);
final Marker marker = Marker(markerId: markerId, position: latLng);

setState(() {
markers[markerId] = marker;
});
},

关于flutter - 用户如何在Google Maps Flutter上添加多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58927260/

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