下面是我的代码。我已经实现了地址搜索栏,但没有显示。功能中更重要的是,谷歌地图将显示客户提供的地址。 也许我需要一个将地址(街道、nr、邮政编码、城市)转换为 LatLong 的函数。我尝试过使用地理定位器插件,但只找到了反向解决方案 -> LatLong into Address。
所以我的客户提供了他的地址,我需要谷歌地图在地图上显示这个地址。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:material_floating_search_bar/material_floating_search_bar.dart';
class MapsDemo extends StatefulWidget {
MapsDemo() : super();
final String title = "Maps Demo";
@override
MapsDemoState createState() => MapsDemoState();
}
class MapsDemoState extends State<MapsDemo> {
Completer<GoogleMapController> _controller = Completer();
static const LatLng _center = const LatLng(45.521563, -122.677433);
final Set<Marker> _markers = {};
LatLng _lastMapPosition = _center;
MapType _currentMapType = MapType.normal;
static final CameraPosition _position1 = CameraPosition(
bearing: 192.833,
target: LatLng(45.531563, -122.677433),
tilt: 59.440,
zoom: 11.0,
);
Future<void> _goToPosition1() async {
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(_position1));
}
_onMapCreated(GoogleMapController controller) {
_controller.complete(controller);
}
_onCameraMove(CameraPosition position) {
_lastMapPosition = position.target;
}
/*_onMapTypeButtonPressed() {
setState(() {
_currentMapType = _currentMapType == MapType.normal
? MapType.satellite
: MapType.normal;
});
}
_onAddMarkerButtonPressed() {
setState(() {
_markers.add(
Marker(
markerId: MarkerId(_lastMapPosition.toString()),
position: _lastMapPosition,
infoWindow: InfoWindow(
title: 'This is a Title',
snippet: 'This is a snippet',
),
icon: BitmapDescriptor.defaultMarker,
),
);
});
}
*/
Widget buildFloatingSearchBar() {
final isPortrait =
MediaQuery.of(context).orientation == Orientation.portrait;
return FloatingSearchBar(
hint: 'Search...',
scrollPadding: const EdgeInsets.only(top: 16, bottom: 56),
transitionDuration: const Duration(milliseconds: 800),
transitionCurve: Curves.easeInOut,
physics: const BouncingScrollPhysics(),
axisAlignment: isPortrait ? 0.0 : -0.1,
openAxisAlignment: 0.0,
width: isPortrait ? 600 : 500,
debounceDelay: const Duration(milliseconds: 500),
onQueryChanged: (query) {},
// Specify a custom transition to be used for
// animating between opened and closed stated.
transition: CircularFloatingSearchBarTransition(),
actions: [
FloatingSearchBarAction(
showIfOpened: false,
child: CircularButton(
icon: const Icon(Icons.place),
onPressed: () {
_goToPosition1();
},
),
),
FloatingSearchBarAction.searchToClear(
showIfClosed: false,
),
],
builder: (context, transition) {
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Material(
color: Colors.white,
elevation: 4.0,
child: Column(
mainAxisSize: MainAxisSize.min,
children: Colors.accents.map((color) {
return Container(height: 112, color: color);
}).toList(),
),
),
);
},
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text('Kundenlocation'),
centerTitle: true,
backgroundColor: Colors.blue,
),
body: Stack(
children: <Widget>[
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
mapType: _currentMapType,
markers: _markers,
onCameraMove: _onCameraMove,
),
Padding(
padding: EdgeInsets.all(16.0),
child: Align(
alignment: Alignment.topRight,
child: Column(
children: <Widget>[
//button(_onMapTypeButtonPressed, Icons.map),
SizedBox(
height: 16.0,
),
//button(_onAddMarkerButtonPressed, Icons.add_location),
SizedBox(
height: 16.0,
),
//button(_goToPosition1, Icons.location_searching),
],
),
),
),
],
),
),
);
}
}