0

我试图通过使用'geolocator'库来确定我的地址的相机位置,但我不知道如何将其设置为谷歌地图的初始相机位置

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();
    }
4

2 回答 2

0

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

您可以试一试,它应该可以完美运行。记住你没有手表位置。您可以在返回位置后获取位置并将相机移动到该位置。

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;
              });
          ),
          )
        ],
      ),
    );
  }
}

于 2020-05-30T03:02:46.443 回答
0

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

//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,
      ),
    );
于 2020-04-15T13:47:43.110 回答