О пакете Flutter GeoLocator - PullRequest
       21

О пакете Flutter GeoLocator

0 голосов
/ 29 апреля 2020

надеюсь, у вас все хорошо. Я столкнулся с проблемой, касающейся карт Google во флаттере. Я использую Flutter Google Maps. И GeoLocator Package of Flutter. Проблема в том, что пакет geolocator не выполняет поиск во многих местах на картах Google, а скорее выдает ошибку Ошибка при поиске местоположений

Мой код указан ниже,

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:geocoder/geocoder.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: LocationScreen(),
    );
  }
}

class LocationScreen extends StatefulWidget {
  final Key _mapKey = UniqueKey();
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<LocationScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Map not crashing demo')),
        body: TheMap(key: widget._mapKey));
  }
}

class TheMap extends StatefulWidget {
  TheMap({@required Key key}) : super(key: key);
  @override
  _TheMapState createState() => _TheMapState();
}

class _TheMapState extends State<TheMap> {
  GoogleMapController mapController;
  final Set<Marker> _markers = {};
  String searchAddr;
 // CLASS MEMBER, MAP OF MARKS
Future<List<Placemark>> currentLocation;
  double lati = 40.7128;
  double longi = -74.0060;
  String newLocation;
  var first;
  String updatedLocation;
  List<Marker> allMarkers = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    currentLocation =
        Geolocator().placemarkFromCoordinates(40.7128, -74.0060);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: <Widget>[
        GoogleMap(
          markers: _markers,
          onMapCreated: onMapCreated,
          initialCameraPosition:
              CameraPosition(target: LatLng(40.7128, -74.0060), zoom: 10.0),
        ),
        Positioned(
          bottom: 30.0,
          right: 15.0,
          left: 15.0,
          child: RaisedButton(
            child: Text('Update your Home Location'),
            onPressed: () {
              showModalBottomSheet(
                  context: context,
                  builder: (context) {
                    return Column(
                      children: <Widget>[
                        TextFormField(

                          initialValue: updatedLocation,
                          decoration:
                              InputDecoration(hintText: 'Current location'),
                        ),
                        TextField(
                          onChanged: (val) {
                            updatedLocation =val;
                            searchAddr = val;
                          },
                          decoration:
                              InputDecoration(hintText: 'Update your Location'),
                        ),
                        RaisedButton(
                            onPressed: () {
                              if(searchAddr!=null){


                              Navigator.pop(context);
                              searchandNavigate();
                              onAddMarkerButtonPressed();}
                            },
                            child: Text('Update'))
                      ],
                    );
                  });
            },
          ),
        )
      ],
    ));
  }

  void onButtonPressed() {}
  searchandNavigate() {
    Geolocator().placemarkFromAddress(searchAddr).then((result) {
      mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
          target:
              LatLng(result[0].position.latitude, result[0].position.longitude),
          zoom: 10.0)));
      setState(() {
        lati = result[0].position.latitude;
        longi = result[0].position.longitude;
      });
      onAddMarkerButtonPressed();
      print(currentLocation);
    });
  }

  onAddMarkerButtonPressed() {
    setState(() {
      _markers.add(
        Marker(
          markerId: MarkerId('MyMarker'),
          position: LatLng(lati, longi),
          infoWindow: InfoWindow(
            title: 'This is a Title',
            snippet: 'This is a snippet',
          ),
          icon: BitmapDescriptor.defaultMarker,
        ),
      );
    });
  }

  void onMapCreated(controller) {
    setState(() {
      mapController = controller;
    });
  }
}
...