0

I tried to determine my address's camera position by using 'geolocator' library but I don't know how to set it into the google map's initial camera position

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

2 Answers2

0

This is how I set initial camera position

//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,
      ),
    );
wcyankees424
  • 2,554
  • 2
  • 12
  • 24
0

If you have to implement that, you need to give it a custom location or display a loader while your app gets your current location. After the app gets your current location you have to call a camera update to move the view to your current location.

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

Limitless Claver
  • 479
  • 5
  • 17