| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- 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/poi_bean_entity.dart';
- import 'package:bbyyy/my_tools/map_util.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;
- @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) {
- final AMapWidget map = AMapWidget(
- initialCameraPosition: _kInitialPosition,
- onMapCreated: onMapCreated,
- rotateGesturesEnabled: false,
- touchPoiEnabled: true,
- onPoiTouched: _onPoiTouched,
- myLocationStyleOptions: MyLocationStyleOptions(
- true,
- ),
- markers: Set<Marker>.of(_markers.values),
- );
- return Scaffold(
- body: Column(
- children: [
- MyViews().myAppBar('amap', context, []),
- Expanded(
- child: Stack(
- children: [
- Container(
- height: MediaQuery.of(context).size.height,
- width: MediaQuery.of(context).size.width,
- child: map,
- ),
- 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: () {
- inquirePOI();
- },
- child: Container(
- color: Colors.blue,
- 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),
- )
- ],
- ),
- ),
- ],
- ),
- 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() {
- Dio()
- .get(
- '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')
- .then((value) {
- print(
- '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(value);
- PoiBeanEntity entity =
- PoiBeanEntity().fromJson(json.decode(value.toString()));
- 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: BitmapDescriptor.fromIconPath('images/地址.png'),
- infoWindow: InfoWindow(title: element.name),
- // onTap: (markerId) {
- // print(markerId);
- // MapUtil.gotoAMap(double.parse(element.location.split(',')[0]),
- // double.parse(element.location.split(',')[1]));
- // },
- // onDragEnd: (markerId, endPosition) =>
- // _onMarkerDragEnd(markerId, endPosition),
- );
- });
- setState(() {
- if (entity.pois.length != 0) {
- _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('未搜索到你查找的地点');
- }
- });
- });
- }
- }
|