| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- import 'dart:convert';
- import 'package:amap_flutter_base/amap_flutter_base.dart';
- import 'package:amap_flutter_map/amap_flutter_map.dart';
- import 'package:bbyyy/beans/address_bean_entity.dart';
- import 'package:bbyyy/beans/poi_bean_entity.dart';
- import 'package:bbyyy/my_tools/my_colors.dart';
- import 'package:bbyyy/my_tools/my_cookie.dart';
- import 'package:bbyyy/my_tools/my_tools.dart';
- import 'package:bbyyy/my_tools/my_views.dart';
- import 'package:dio/dio.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- class MapDemoPage extends StatefulWidget {
- @override
- _MapDemoPageState createState() => _MapDemoPageState();
- }
- class _MapDemoPageState extends State<MapDemoPage> {
- CameraPosition _kInitialPosition;
- List<Widget> _approvalNumberWidget = [];
- TextEditingController _controller = TextEditingController();
- Map<String, Marker> _markers = <String, Marker>{};
- Widget _poiInfo;
- List<PoiBeanPois> pois;
- BitmapDescriptor _markerIcon;
- PoiBeanPois poi;
- bool searchMode = true;
- @override
- void initState() {
- // TODO: implement initState
- super.initState();
- _kInitialPosition = CameraPosition(
- target:
- LatLng(MyCookie().location.latitude, MyCookie().location.longitude),
- zoom: 15.0,
- );
- }
- @override
- Widget build(BuildContext context) {
- if (null == _markerIcon) {
- _markerIcon = BitmapDescriptor.fromIconPath('assets/地址.png');
- }
- final AMapWidget map = AMapWidget(
- initialCameraPosition: _kInitialPosition,
- onMapCreated: onMapCreated,
- rotateGesturesEnabled: false,
- touchPoiEnabled: true,
- tiltGesturesEnabled: false,
- onPoiTouched: _onPoiTouched,
- myLocationStyleOptions: MyLocationStyleOptions(
- true,
- ),
- markers: Set<Marker>.of(_markers.values),
- onTap: (LatLng latLng){
- print('================|${latLng.latitude},${latLng.longitude}|===================');
- markTheClickLocation(latLng);
- },
- );
- return Scaffold(
- body: Column(
- children: [
- MyViews().myAppBar('选择地址', context, [
- GestureDetector(
- onTap: (){
- setState(() {
- _markers.clear();
- pois?.clear();
- poi = null;
- searchMode = !searchMode;
- _mapController.moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
- target: LatLng(MyCookie().location.latitude, MyCookie().location.longitude),
- zoom: 15,
- )));
- });
- },
- behavior: HitTestBehavior.translucent,
- child: Container(
- margin: EdgeInsets.symmetric(vertical: 5,horizontal: 10),
- child: Text(
- searchMode?'手动选择':'搜索选择',
- style: TextStyle(color: MyColors.c333333, fontSize: 14),
- ),
- ),
- )
- ]),
- Expanded(
- child: Stack(
- children: [
- Container(
- height: MediaQuery.of(context).size.height,
- width: MediaQuery.of(context).size.width,
- child: map,
- ),
- if(searchMode)
- Container(
- child: Row(
- children: [
- Expanded(
- child: Padding(
- padding: EdgeInsets.symmetric(horizontal: 10),
- child: TextField(
- controller: _controller,
- cursorColor: MyColors.cFF4233,
- cursorWidth: 1.0,
- decoration: InputDecoration(
- border: InputBorder.none,
- disabledBorder: InputBorder.none,
- enabledBorder: InputBorder.none,
- focusedBorder: InputBorder.none,
- hintText: '请输入地址',
- hintStyle: TextStyle(
- color: MyColors.c999999, fontSize: 16),
- isDense: true,
- contentPadding:
- const EdgeInsets.fromLTRB(14, 4.5, 8, 4.5)),
- maxLines: 1, style: TextStyle(
- color: MyColors.c333333,
- fontSize: 16,
- height: 1.3,
- letterSpacing: 0.2),
- onChanged: (t) {},
- ),
- ),
- ),
- GestureDetector(
- onTap: () {
- MyTools().hideKeyboard(context);
- inquirePOI();
- },
- child: Container(
- color: MyColors.cFF4233,
- child: Text(
- '搜索',
- style: TextStyle(color: Colors.white, fontSize: 18),
- ),
- width: 100,
- height: 48,
- alignment: Alignment.center,
- ),
- ),
- ],
- ),
- color: Colors.white,
- margin: EdgeInsets.symmetric(horizontal: 15, vertical: 30),
- ),
- if (poi != null)
- Container(
- margin: EdgeInsets.fromLTRB(
- 12,
- MediaQuery.of(context).size.height - 200 - 12 - 45,
- 12,
- 12),
- width: double.infinity,
- height: 150,
- decoration: BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.circular(12),
- boxShadow: [
- BoxShadow(
- color: MyColors.c7FE1E1E1,
- blurRadius: 5.0,
- ),
- ]),
- child: Column(
- children: [
- Text(poi == null ? '' : poi.name),
- Text(poi == null ? '' : poi.address),
- GestureDetector(
- onTap: () {
- Navigator.pop(context, poi);
- },
- behavior: HitTestBehavior.translucent,
- child: Container(
- decoration: BoxDecoration(
- color: MyColors.cFF4233,
- borderRadius: BorderRadius.circular(20)),
- height: 40,
- width: 120,
- child: Text(
- '就是这里',
- style:
- TextStyle(color: Colors.white, fontSize: 16),
- ),
- alignment: Alignment.center,
- ),
- )
- ],
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- ),
- )
- ],
- ),
- ),
- ],
- ),
- resizeToAvoidBottomInset: false,
- );
- }
- AMapController _mapController;
- void onMapCreated(AMapController controller) {
- setState(() {
- _mapController = controller;
- getApprovalNumber();
- });
- }
- /// 获取审图号
- void getApprovalNumber() async {
- //普通地图审图号
- String mapContentApprovalNumber =
- await _mapController?.getMapContentApprovalNumber();
- //卫星地图审图号
- String satelliteImageApprovalNumber =
- await _mapController?.getSatelliteImageApprovalNumber();
- setState(() {
- if (null != mapContentApprovalNumber) {
- _approvalNumberWidget.add(Text(mapContentApprovalNumber));
- }
- if (null != satelliteImageApprovalNumber) {
- _approvalNumberWidget.add(Text(satelliteImageApprovalNumber));
- }
- });
- print('地图审图号(普通地图): $mapContentApprovalNumber');
- print('地图审图号(卫星地图): $satelliteImageApprovalNumber');
- }
- Widget showPoiInfo(AMapPoi poi) {
- return Container(
- alignment: Alignment.center,
- color: Color(0x8200CCFF),
- child: Text(
- '您点击了 ${poi.name}',
- style: TextStyle(fontWeight: FontWeight.w600),
- ),
- );
- }
- void _onPoiTouched(AMapPoi poi) {
- setState(() {
- _poiInfo = showPoiInfo(poi);
- });
- }
- void inquirePOI() {
- String v1 =
- 'https://restapi.amap.com/v3/place/text?keywords=${_controller.text.toString()}&city=${MyCookie().location.city}&output=json&offset=25&page=1&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&extensions=all';
- print(v1);
- Dio().get(v1).then((value) {
- print(value);
- PoiBeanEntity entity =
- PoiBeanEntity().fromJson(json.decode(value.toString()));
- pois = entity.pois;
- entity.pois.forEach((element) {
- print(element.name);
- _markers[element.id] = Marker(
- position: LatLng(double.parse(element.location.split(',')[1]),
- double.parse(element.location.split(',')[0])),
- icon: _markerIcon,
- infoWindow: InfoWindow(title: element.name),
- onTap: (markerId) {
- print(markerId);
- // MapUtil.gotoAMap(double.parse(element.location.split(',')[0]),
- // double.parse(element.location.split(',')[1]));
- setState(() {
- poi = element;
- });
- },
- );
- });
- setState(() {
- if (entity.pois.length != 0) {
- showBottomSheet();
- _mapController
- .moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
- target: LatLng(double.parse(entity.pois[0].location.split(',')[1]),
- double.parse(entity.pois[0].location.split(',')[0])),
- zoom: 18,
- )));
- } else {
- showToast('未搜索到你查找的地点');
- }
- });
- });
- }
- //显示底部弹框的功能
- void showBottomSheet() {
- //用于在底部打开弹框的效果
- showModalBottomSheet(
- builder: (BuildContext context) {
- //构建弹框中的内容
- return Container(
- child: ListView.builder(
- padding: EdgeInsets.all(8),
- itemBuilder: (BuildContext context, int index) {
- return GestureDetector(
- onTap: () {
- _mapController.moveCamera(
- CameraUpdate.newCameraPosition(CameraPosition(
- target: LatLng(
- double.parse(pois[index].location.split(',')[1]),
- double.parse(pois[index].location.split(',')[0])),
- zoom: 15,
- )));
- Navigator.pop(context);
- setState(() {
- poi = pois[index];
- });
- },
- behavior: HitTestBehavior.translucent,
- child: Column(
- children: [
- Text(
- pois[index].name,
- style: TextStyle(fontSize: 16),
- ),
- Text(
- pois[index].address,
- style: TextStyle(fontSize: 12),
- ),
- Container(
- height: .5,
- color: Colors.grey[200],
- margin: EdgeInsets.symmetric(vertical: 10),
- )
- ],
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- crossAxisAlignment: CrossAxisAlignment.start,
- ),
- );
- },
- itemCount: pois.length,
- ),
- );
- },
- context: context);
- }
- void markTheClickLocation(LatLng latLng) {
- print('https://restapi.amap.com/v3/geocode/regeo?output=json&location=${latLng.longitude},${latLng.latitude}&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&radius=10&extensions=all');
- Dio()
- .get(
- 'https://restapi.amap.com/v3/geocode/regeo?output=json&location=${latLng.longitude},${latLng.latitude}&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&radius=10&extensions=all')
- .then((value) {
- print('value.toString()------------${value.toString()}');
- AddressBeanEntity addressBeanEntity =
- AddressBeanEntity().fromJson(json.decode(value.toString()));
- poi = PoiBeanPois();
- poi.location = '${latLng.longitude},${latLng.latitude}';
- poi.pname = addressBeanEntity.regeocode.addressComponent.province;
- poi.cityname = addressBeanEntity.regeocode.addressComponent.city;
- poi.adname = addressBeanEntity.regeocode.addressComponent.district;
- poi.address = addressBeanEntity.regeocode.pois[0].address;
- poi.name = addressBeanEntity.regeocode.pois[0].name;
- _markers['手动选择'] = Marker(
- position: latLng,
- icon: _markerIcon,
- infoWindow: InfoWindow(title: poi.name),
- );
- _mapController.moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
- target: latLng,
- zoom: 20,
- )));
- setState(() {
- });
- });
- }
- }
|