【问题标题】:How to make initialCameraPosition from my address in flutter google maps?如何从我在颤动的谷歌地图中的地址制作 initialCameraPosition?
【发布时间】:2020-07-28 11:29:46
【问题描述】:

我尝试使用“地理定位器”库确定我的地址的相机位置,但我不知道如何将其设置为谷歌地图的初始相机位置

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

【问题讨论】:

    标签: android flutter location google-maps-flutter


    【解决方案1】:

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

    //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,
          ),
        );
    

    【讨论】:

    • 非常感谢您,但是您可以添加该代码吗?
    • 您还需要哪些代码来从地理定位中获取您的初始位置
    • 这不是回答你的问题吗
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 2021-03-06
      • 2021-01-03
      • 2020-09-27
      • 1970-01-01
      相关资源
      最近更新 更多