| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- 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/my_cookie.dart';
- import 'package:dio/dio.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,
- touchPoiEnabled: true,
- onPoiTouched: _onPoiTouched,
- myLocationStyleOptions: MyLocationStyleOptions(
- true,
- circleFillColor: Colors.lightBlue,
- circleStrokeColor: Colors.blue,
- circleStrokeWidth: 1,
- ),
- );
- return Scaffold(
- body: Stack(
- children: [
- Container(
- height: MediaQuery.of(context).size.height,
- width: MediaQuery.of(context).size.width,
- child: map,
- ),
- Container(
- child: Row(
- children: [
- Expanded(
- child: TextField(
- controller: _controller,
- )),
- 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.only(top: 50),
- )
- ],
- ),
- );
- }
- 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=50&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=50&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.name] = Marker(
- position: LatLng(double.parse(element.location.split(',')[0]),double.parse(element.location.split(',')[1])),
- icon: BitmapDescriptor.fromIconPath('images/地址.png'),
- infoWindow: InfoWindow(title: element.name),
- // onTap: (markerId) => _onMarkerTapped(markerId),
- // onDragEnd: (markerId, endPosition) =>
- // _onMarkerDragEnd(markerId, endPosition),
- );
- });
- });
- }
- }
|