我想用谷歌地图服务构建一个应用程序,我找到了很多关于这方面的教程,但最适合我的需要这个教程我正在尝试构建这个应用程序,这个应用程序有两个文件,一个是main.dart,另一个是googlemap.dart,主要的dart代码是:
import 'package:flutter/material.dart';
import 'package:location/location.dart';
import 'package:maps/pages/goomap.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Location location = new Location();
bool _serviceEnabled;
PermissionStatus _permissionGranted;
LocationData _locationData;
@override
void initState() {
super.initState();
_checkLocationPermission();
}
// Check Location Permissions, and get my location
void _checkLocationPermission() async {
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return;
}
}
_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}
_locationData = await location.getLocation();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[800],
appBar: AppBar(
title: Text('Studyng Maps - Zeh'),
centerTitle: true,
backgroundColor: Colors.grey[900],
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () => _locationData != null ? Navigator.push(
context, MaterialPageRoute(builder: (context) => GooMap(location: _locationData,))) : null,
backgroundColor: Colors.orange,
label: Row(
children: <Widget>[
Text(
'Open Maps',
style: TextStyle(color: Colors.black87),
),
Icon(
Icons.map,
color: Colors.black87,
),
],
)),
body: Container(
padding: EdgeInsets.all(16),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Study of google maps - Zeh',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.white),
),
SizedBox(height: 20),
Text(
'App to study some features of google maps: Testing markers, polygons, polylines and circles',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
)),
),
);
}
}`
另一个文件(谷歌地图(代码是:
import 'dart:collection';
import 'package:flutter/cupertino.dart';
import 'package:location/location.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class GooMap extends StatefulWidget {
//GooMap({Key key}) : super(key: key);
final LocationData location;
GooMap({this.location});
@override
_GooMapState createState() => _GooMapState();
}
class _GooMapState extends State<GooMap> {
// Location
LocationData _locationData;
// Maps
Set<Marker> _markers = HashSet<Marker>();
Set<Polygon> _polygons = HashSet<Polygon>();
Set<Circle> _circles = HashSet<Circle>();
GoogleMapController _googleMapController;
BitmapDescriptor _markerIcon;
List<LatLng> polygonLatLngs = List<LatLng>();
double radius;
//ids
int _polygonIdCounter = 1;
int _circleIdCounter = 1;
int _markerIdCounter = 1;
// Type controllers
bool _isPolygon = true; //Default
bool _isMarker = false;
bool _isCircle = false;
@override
void initState() {
super.initState();
// If I want to change the marker icon:
// _setMarkerIcon();
_locationData = widget.location;
}
// This function is to change the marker icon
void _setMarkerIcon() async {
_markerIcon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(), 'assets/farm.png');
}
// Draw Polygon to the map
void _setPolygon() {
final String polygonIdVal = 'polygon_id_$_polygonIdCounter';
_polygons.add(Polygon(
polygonId: PolygonId(polygonIdVal),
points: polygonLatLngs,
strokeWidth: 2,
strokeColor: Colors.yellow,
fillColor: Colors.yellow.withOpacity(0.15),
));
}
// Set circles as points to the map
void _setCircles(LatLng point) {
final String circleIdVal = 'circle_id_$_circleIdCounter';
_circleIdCounter++;
print(
'Circle | Latitude: ${point.latitude} Longitude: ${point.longitude} Radius: $radius');
_circles.add(Circle(
circleId: CircleId(circleIdVal),
center: point,
radius: radius,
fillColor: Colors.redAccent.withOpacity(0.5),
strokeWidth: 3,
strokeColor: Colors.redAccent));
}
// Set Markers to the map
void _setMarkers(LatLng point) {
final String markerIdVal = 'marker_id_$_markerIdCounter';
_markerIdCounter++;
setState(() {
print(
'Marker | Latitude: ${point.latitude} Longitude: ${point.longitude}');
_markers.add(
Marker(
markerId: MarkerId(markerIdVal),
position: point,
),
);
});
}
// Start the map with this marker setted up
void _onMapCreated(GoogleMapController controller) {
_googleMapController = controller;
setState(() {
_markers.add(
Marker(
markerId: MarkerId('0'),
position: LatLng(-20.131886, -47.484488),
infoWindow:
InfoWindow(title: 'Roça', snippet: 'Um bom lugar para estar'),
//icon: _markerIcon,
),
);
});
}
Widget _fabPolygon() {
return FloatingActionButton.extended(
onPressed: () {
//Remove the last point setted at the polygon
setState(() {
polygonLatLngs.removeLast();
});
},
icon: Icon(Icons.undo),
label: Text('Undo point'),
backgroundColor: Colors.orange,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Studying Maps - Zeh'),
centerTitle: true,
backgroundColor: Colors.grey[900],
),
floatingActionButton:
polygonLatLngs.length > 0 && _isPolygon ? _fabPolygon() : null,
body: Stack(
children: <Widget>[
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(_locationData.latitude, _locationData.longitude),
zoom: 16,
),
mapType: MapType.hybrid,
markers: _markers,
circles: _circles,
polygons: _polygons,
myLocationEnabled: true,
onTap: (point) {
if (_isPolygon) {
setState(() {
polygonLatLngs.add(point);
_setPolygon();
});
} else if (_isMarker) {
setState(() {
_markers.clear();
_setMarkers(point);
});
} else if (_isCircle) {
setState(() {
_circles.clear();
_setCircles(point);
});
}
},
),
Align(
alignment: Alignment.bottomCenter,
child: Row(
children: <Widget>[
RaisedButton(
color: Colors.black54,
onPressed: () {
_isPolygon = true;
_isMarker = false;
_isCircle = false;
},
child: Text(
'Polygon',
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
)),
RaisedButton(
color: Colors.black54,
onPressed: () {
_isPolygon = false;
_isMarker = true;
_isCircle = false;
},
child: Text('Marker',
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white))),
RaisedButton(
color: Colors.black54,
onPressed: () {
_isPolygon = false;
_isMarker = false;
_isCircle = true;
radius = 50;
return showDialog(
context: context,
child: AlertDialog(
backgroundColor: Colors.grey[900],
title: Text(
'Choose the radius (m)',
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
),
content: Padding(
padding: EdgeInsets.all(8),
child: Material(
color: Colors.black,
child: TextField(
style: TextStyle(fontSize: 16, color: Colors.white),
decoration: InputDecoration(
icon: Icon(Icons.zoom_out_map),
hintText: 'Ex: 100',
suffixText: 'meters',
),
keyboardType:
TextInputType.numberWithOptions(),
onChanged: (input) {
setState(() {
radius = double.parse(input);
});
},
),
)),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text(
'Ok',
style: TextStyle(
fontWeight: FontWeight.bold,),
)),
],
));
},
child: Text('Circle',
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white)))
],
),
)
],
));
}
}
我有这些错误:
error: The named parameter 'child' isn't defined. (undefined_named_parameter at [mappolygon] goomap.dart:202)
error: The method 'GooMap' isn't defined for the type '_HomePageState'. (undefined_method at [mappolygon] lib/main.dart:52)
error: The function 'run' isn't defined. (undefined_function at [mappolygon] integration_test/app_test.dart:14)
error: The function 'main' isn't defined. (undefined_function at [mappolygon] integration_test/app_test.dart:19)
error: Target of URI doesn't exist: 'package:maps/pages/goomap.dart'. (uri_does_not_exist at [mappolygon] lib/main.dart:3)
warning: The parameter 'builder' is required. (missing_required_param at [mappolygon] goomap.dart:200)
info: Unused import: 'package:integration_test/integration_test.dart'. (unused_import at [mappolygon] integration_test/app_test.dart:10)
info: The value of the field '_markerIcon' isn't used. (unused_field at [mappolygon] goomap.dart:26)
info: The value of the field '_googleMapController' isn't used. (unused_field at [mappolygon] goomap.dart:25)
info: The import of 'package:flutter/cupertino.dart' is unnecessary because all of the used elements are also provided by the import of 'package:flutter/material.dart'. (unnecessary_import at [mappolygon] goomap.dart:2)
info: The declaration '_setMarkerIcon' isn't referenced. (unused_element at [mappolygon] goomap.dart:49)
info: The declaration '_onMapCreated' isn't referenced. (unused_element at [mappolygon] goomap.dart:98)
info: 'RaisedButton.RaisedButton' is deprecated and shouldn't be used. Use ElevatedButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). This feature was deprecated after v1.26.0-18.0.pre.. (deprecated_member_use at [mappolygon] goomap.dart:173)
info: 'RaisedButton.RaisedButton' is deprecated and shouldn't be used. Use ElevatedButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). This feature was deprecated after v1.26.0-18.0.pre.. (deprecated_member_use at [mappolygon] goomap.dart:193)
info: 'RaisedButton.RaisedButton' is deprecated and shouldn't be used. Use ElevatedButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). This feature was deprecated after v1.26.0-18.0.pre.. (deprecated_member_use at [mappolygon] goomap.dart:184)
info: 'RaisedButton' is deprecated and shouldn't be used. Use ElevatedButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). This feature was deprecated after v1.26.0-18.0.pre.. (deprecated_member_use at [mappolygon] goomap.dart:173)
info: 'RaisedButton' is deprecated and shouldn't be used. Use ElevatedButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). This feature was deprecated after v1.26.0-18.0.pre.. (deprecated_member_use at [mappolygon] goomap.dart:193)
info: 'RaisedButton' is deprecated and shouldn't be used. Use ElevatedButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). This feature was deprecated after v1.26.0-18.0.pre.. (deprecated_member_use at [mappolygon] goomap.dart:184)
info: 'List.List' is deprecated and shouldn't be used. Use a list literal, [], or the List.filled constructor instead. (deprecated_member_use at [mappolygon] goomap.dart:27)
info: 'FlatButton.FlatButton' is deprecated and shouldn't be used. Use TextButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). This feature was deprecated after v1.26.0-18.0.pre.. (deprecated_member_use at [mappolygon] goomap.dart:229)
info: 'FlatButton' is deprecated and shouldn't be used. Use TextButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). This feature was deprecated after v1.26.0-18.0.pre.. (deprecated_member_use at [mappolygon] goomap.dart:229)
我不知道如何解决这些问题,你能帮忙吗?
请参阅以下代码
class MapDemo extends StatelessWidget {
const MapDemo({Key key}) : super(key: key);
/*Controller*/
GoogleMapController mapController;
/*Variable declaration*/
bool customFlag = false;
BitmapDescriptor customIcon1;
/*Custom Marker for Maps*/
createMarker(context) {
if (customIcon1 == null) {
ImageConfiguration configuration = createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(configuration, ImagePaths.dcGlobe) /* ImagePaths.dcGlobe i san asset image */
.then((icon) {
customIcon1 = icon;
});
}
}
@override
Widget build(BuildContext context) {
createMarker(context);
return Scaffold(
body: Column(
children: [
LocationMaps(
mapController: mapController,
customFlag: customFlag,
customIcon1: customIcon1,
longitudeValues: 85,
latitudeValues: 12.0,
),
],
),
);
}
}
class LocationMaps extends StatelessWidget {
GoogleMapController mapController;
bool customFlag;
final BitmapDescriptor customIcon1;
final longitudeValues;
final latitudeValues;
LocationMaps({
Key key,
this.mapController,
this.customFlag,
this.customIcon1,
this.longitudeValues,
this.latitudeValues,
}) : super(key: key);
double locationLatitude = 0.0;
double locationLongitude = 0.0;
LatLng _center;
List<Marker> _markers = <Marker>[];
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
Widget build(BuildContext context) {
locationLatitude = double.parse(latitudeValues);
locationLongitude = double.parse(longitudeValues);
_center = LatLng(locationLatitude, locationLongitude);
_markers.add(Marker(
icon: customFlag == true ? customIcon1 : null,
markerId: MarkerId(''),
position: LatLng(locationLatitude, locationLongitude),
infoWindow: InfoWindow(title: '')));
return ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Container(
color: AppColors.creamColor,
width: ScreenUtil().screenWidth,
child: Column(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Container(
width: ScreenUtil().screenWidth,
height: ScreenUtil().setHeight(120.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
),
child: GoogleMap(
myLocationButtonEnabled: (Platform.isAndroid) ? true : false,
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 13.0,
),
mapType: MapType.normal,
markers: Set<Marker>.of(_markers),
),
),
),
],
),
),
);
}
}
``