| | |
| | | import 'dart:convert'; |
| | | import 'dart:io'; |
| | | import 'dart:typed_data'; |
| | | import 'dart:ui'; |
| | | |
| | | import 'package:flutter/cupertino.dart'; |
| | | import 'package:flutter/material.dart'; |
| | | import 'package:flutter/rendering.dart'; |
| | | import 'package:flutter/services.dart'; |
| | | import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'; |
| | | import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart'; |
| | | import 'package:flutter_baidu_mapapi_search/flutter_baidu_mapapi_search.dart'; |
| | | import 'package:locations/api/http.dart'; |
| | | import 'package:locations/model/map/location_model.dart'; |
| | | import 'package:locations/model/map/map_model.dart'; |
| | | import 'package:locations/model/map/poi_model.dart'; |
| | | import 'package:locations/utils/ad_util.dart'; |
| | | import 'package:locations/utils/global.dart'; |
| | | import 'package:locations/utils/location_util.dart'; |
| | | import 'package:locations/utils/map_util.dart'; |
| | | import 'package:locations/utils/map_js_util.dart'; |
| | | import 'package:locations/utils/search_util.dart'; |
| | | import 'package:locations/utils/string_util.dart'; |
| | | import 'package:locations/utils/ui_constant.dart'; |
| | | import 'package:url_launcher/url_launcher.dart'; |
| | | import 'package:webview_flutter/webview_flutter.dart'; |
| | | |
| | | //控件阴影 |
| | | List<BoxShadow> getViewShadow() { |
| | |
| | | |
| | | class _LocationSearchPageState extends State<LocationSearchPage> |
| | | with SingleTickerProviderStateMixin { |
| | | BMFMapController? _mapController; |
| | | BMFMapOptions mapOptions = BMFMapOptions( |
| | | showMapScaleBar: false, |
| | | mapType: BMFMapType.Standard, |
| | | center: Global.currentPosition != null |
| | | ? Global.currentPosition |
| | | : BMFCoordinate(39.917215, 116.380341), |
| | | mapScaleBarPosition: BMFPoint(0, 200), |
| | | zoomLevel: 12, |
| | | mapPadding: BMFEdgeInsets(left: 30, top: 0, right: 30, bottom: 200)); |
| | | WebViewController? _webViewController; |
| | | |
| | | // BMFMapOptions mapOptions = BMFMapOptions( |
| | | // showMapScaleBar: false, |
| | | // mapType: BMFMapType.Standard, |
| | | // center: Global.currentPosition != null |
| | | // ? Global.currentPosition |
| | | // : BMFCoordinate(39.917215, 116.380341), |
| | | // mapScaleBarPosition: BMFPoint(0, 200), |
| | | // zoomLevel: 14, |
| | | // rotateEnabled: false, |
| | | // compassPosition: BMFPoint(20, 20), |
| | | // mapPadding: BMFEdgeInsets(left: 40, top: 0, right: 40, bottom: 0)); |
| | | |
| | | //1-搜索页面 2-建议搜索页面 3-结果展示页面 |
| | | int state = 1; |
| | |
| | | Widget? expressAd; |
| | | |
| | | TextEditingController? editingController; |
| | | List<BMFPoiInfo>? suggestList; |
| | | List<BMFPoiInfo>? recordList; |
| | | BMFCoordinate? currentPosition; |
| | | List<PoiModel>? suggestList; |
| | | List<PoiModel>? recordList; |
| | | |
| | | //选中位置信息 |
| | | BMFPoiInfo? selectedPoiInfo; |
| | | PoiModel? selectedPoiInfo; |
| | | |
| | | BMFMarker? selectedPoiInfoMarker; |
| | | Marker? selectedPoiInfoMarker; |
| | | |
| | | bool deleteAd = false; |
| | | |
| | | @override |
| | | void initState() { |
| | | super.initState(); |
| | | if (Platform.isAndroid) { |
| | | WebView.platform = SurfaceAndroidWebView(); |
| | | } |
| | | editingController = TextEditingController(); |
| | | loadRecord(); |
| | | } |
| | |
| | | }); |
| | | } |
| | | |
| | | WebView? _webview; |
| | | |
| | | //获取地图视图 |
| | | Widget getMapView() { |
| | | return Container( |
| | | child: BMFMapWidget( |
| | | onBMFMapCreated: (controller) { |
| | | _mapController = controller; |
| | | LocationUtil.startLocation(0, (state, map) { |
| | | if (state == LocationState.success) { |
| | | currentPosition = BMFCoordinate.fromMap(map!); |
| | | _mapController!.setCenterCoordinate(currentPosition!, true); |
| | | } |
| | | }); |
| | | }, |
| | | mapOptions: mapOptions, |
| | | ), |
| | | ); |
| | | _webview ??= getMapWebView(context, (controller) { |
| | | _webViewController = controller; |
| | | }); |
| | | return _webview!; |
| | | } |
| | | |
| | | @override |
| | |
| | | if (state == 3) { |
| | | if (selectedPoiInfoMarker != null) { |
| | | MapUtil.removeMarker( |
| | | _mapController, selectedPoiInfoMarker!); |
| | | _webViewController, selectedPoiInfoMarker!); |
| | | selectedPoiInfoMarker = null; |
| | | } |
| | | setState(() { |
| | |
| | | // } |
| | | |
| | | startPOISearch(String key) async { |
| | | if (Global.currentPosition == null) { |
| | | return; |
| | | } |
| | | setState(() { |
| | | suggestList = null; |
| | | if (key.isNotEmpty) |
| | |
| | | state = 1; |
| | | }); |
| | | |
| | | BMFPoiNearbySearchOption poiNearbySearchOption = BMFPoiNearbySearchOption( |
| | | keywords: <String>[key], |
| | | location: currentPosition != null |
| | | ? currentPosition |
| | | : BMFCoordinate(29.674509, 106.517571), |
| | | radius: 10000, |
| | | isRadiusLimit: true); |
| | | |
| | | BMFPoiNearbySearch nearbySearch = BMFPoiNearbySearch(); |
| | | nearbySearch.onGetPoiNearbySearchResult( |
| | | callback: (BMFPoiSearchResult result, BMFSearchErrorCode errorCode) { |
| | | print("搜索结果回调:$result"); |
| | | setState(() { |
| | | suggestList = result.poiInfoList; |
| | | Map<String, dynamic>? result = await LocationApiUtil.searchNearBy(key, |
| | | Global.currentPosition!.latitude!, Global.currentPosition!.longitude!); |
| | | if (result != null) { |
| | | List<dynamic> list = result["data"]["list"]; |
| | | List<PoiModel> clist = []; |
| | | list.forEach((element) { |
| | | clist.add(PoiModel.fromJson(element)); |
| | | }); |
| | | }); |
| | | bool flag = await nearbySearch.poiNearbySearch(poiNearbySearchOption); |
| | | |
| | | // BMFSuggestionSearchOption suggestionSearchOption = |
| | | // BMFSuggestionSearchOption(keyword: key,cityLimit: false); |
| | | // BMFSuggestionSearch suggestionSearch = BMFSuggestionSearch(); |
| | | // suggestionSearch.onGetSuggestSearchResult(callback: |
| | | // (BMFSuggestionSearchResult result, BMFSearchErrorCode errorCode) { |
| | | // print("result:$result errorCode:$errorCode"); |
| | | // setState(() { |
| | | // suggestList = result.suggestionList; |
| | | // }); |
| | | // }); |
| | | // bool flag = await suggestionSearch.suggestionSearch(suggestionSearchOption); |
| | | |
| | | print("结束$flag"); |
| | | setState(() { |
| | | suggestList = clist; |
| | | }); |
| | | } |
| | | } |
| | | |
| | | //建议搜索视图 |
| | |
| | | )); |
| | | } |
| | | |
| | | void _showPOIDetail(BMFPoiInfo info) async { |
| | | double? distance; |
| | | if (Global.currentPosition != null) { |
| | | distance = await MapUtil.getDistance( |
| | | Global.currentPosition!, BMFCoordinate.fromMap(info.pt!.toMap())); |
| | | } |
| | | distance ??= 0; |
| | | info.distance = distance.toInt(); |
| | | void _showPOIDetail(PoiModel info) async { |
| | | //显示地址详情 |
| | | SearchUtil.addSearchRecord(info); |
| | | setState(() { |
| | |
| | | |
| | | //添加marker |
| | | if (selectedPoiInfoMarker == null) { |
| | | MapUtil.addMarker(_mapController, BMFCoordinate.fromMap(info.pt!.toMap()), |
| | | "assets/images/common/icon_location_marker.png") |
| | | ByteData byteData = await rootBundle |
| | | .load("assets/images/common/icon_location_marker.png"); |
| | | Uint8List pngBytes = byteData.buffer.asUint8List(); |
| | | MapUtil.addMarker( |
| | | _webViewController, info.position!, base64.encode(pngBytes), |
| | | width: 43, height: 60) |
| | | .then((value) { |
| | | selectedPoiInfoMarker = value; |
| | | }); |
| | | } else { |
| | | selectedPoiInfoMarker! |
| | | .updatePosition(BMFCoordinate.fromMap(info.pt!.toMap())); |
| | | MapMarkerUtil.update(_webViewController, selectedPoiInfoMarker!.Id, |
| | | position: info.position); |
| | | } |
| | | _mapController! |
| | | .setCenterCoordinate(BMFCoordinate.fromMap(info.pt!.toMap()), true); |
| | | MapUtil.setCenter(_webViewController, info.position!); |
| | | } |
| | | |
| | | //历史记录项目 |
| | | Widget getHistoryItem(String title, String content, BMFPoiInfo info, |
| | | Widget getHistoryItem(String title, String content, PoiModel info, |
| | | {bool showDistance = false}) { |
| | | return InkWell( |
| | | onTap: () { |