如何在 flutter google 地图中从我的地址创建 initialCameraPosition?

How to make initialCameraPosition from my address in flutter google maps?

我尝试使用 'geolocator' 库来确定我地址的相机位置,但我不知道如何将其设置到 google 地图的初始相机位置

searchAndNavigate() {
      searchAddress = widget.author.address;
      Geolocator().placemarkFromAddress(searchAddress).then((result) {
        mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
            target:
            LatLng(result[0].position.latitude, result[0].position.longitude),
            zoom: 10.0)));
      });
    }

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

    Widget mapSection = Container(
      height: 400,
      width: 400,
      child: Card(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
        child: GoogleMap(
          onMapCreated: onMapCreated,
          initialCameraPosition: CameraPosition(
            target: LatLng(40.7128, -74.0060), zoom: 10.0)),
      ),
    );

    @override
    void initState() {
      super.initState();
      searchAndNavigate();
    }

这是我设置初始相机位置的方式

//initialize _center
Position _center;
final Set<Marker> _markers = {};

 @override
  void initState() {
    super.initState();
//Then define _center in initState 
    _center = Position(locationData.latitude, locationData.longitude);

 _markers.add(
      Marker(
        // This marker id can be anything that uniquely identifies each marker.
        markerId: MarkerId(_center.toString()),
        position: _center,
        infoWindow: InfoWindow(
          title: widget.hearingItem.location.locationName,
          snippet: widget.hearingItem.location.address,
        ),
        icon: BitmapDescriptor.defaultMarker,
      ),
    );
  }


 GoogleMap(
      myLocationEnabled: true,
      onMapCreated: _onMapCreated,
      markers: _markers,
      gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
        Factory<OneSequenceGestureRecognizer>(
          () => EagerGestureRecognizer(),
        ),
      ].toSet(),
//Finally assign _center to target in CameraPosition
      initialCameraPosition: CameraPosition(
        target: _center,
        zoom: 11.0,
      ),
    );

如果你必须实现它,你需要给它一个自定义位置或者在你的应用程序获取你的当前位置时显示一个加载器。在应用程序获取您的当前位置后,您必须调用相机更新以将视图移动到您的当前位置。

You could give this a shot that should work perfectly. Remember you don't have watch position. You can just get position and move the camera to that position after the position has been returned.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';


class Home extends StatefulWidget {
  @override
  State<Home> createState() => HomeState();

}

class HomeState extends State<Home> {
  GoogleMapController mapController;
  Location _location = Location();
  LocationData _locationData;

  watchLocation() async{

    _location.onLocationChanged.listen((LocationData currentLocation) {

      LatLng latLng = LatLng(currentLocation.latitude, currentLocation.longitude);
      CameraUpdate cameraUpdate = CameraUpdate.newLatLngZoom(latLng, 15);
      mapController.animateCamera(cameraUpdate);

      setState(() {
        this._locationData = currentLocation;
      });
    });
  }

  @override
  void initState() {
    super.initState();
    watchLocation();
  }


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: Stack(
        children: <Widget>[
          GoogleMap(
            myLocationEnabled: true,
            mapToolbarEnabled: true,
            zoomControlsEnabled: false,
            myLocationButtonEnabled: false,
            mapType: MapType.normal,
            initialCameraPosition: CameraPosition(
              target: LatLng(this._locationData?.latitude ?? 6.7008168, this._locationData?.longitude ?? -1.6998494),
              zoom: 14.4746,
            ),
            onMapCreated: (GoogleMapController controller) {
              setState(() {
                mapController = controller;
              });
          ),
          )
        ],
      ),
    );
  }
}