How marker of geolocate add to map? - google-maps

I make a screen with a map on which had be the marker with my location and the marker of my choose coordinates on the map. How can i put a marker with my location on the map?
class MapsDemo extends StatefulWidget {
#override
State createState() => MapsDemoState();
}
class MapsDemoState extends State<MapsDemo> {
var currentLocation;
GoogleMapController mapController;
_getLocation() async {
var location = new Location();
try {
currentLocation = await location.getLocation();
print("locationLatitude: ${currentLocation["latitude"]}");
print("locationLongitude: ${currentLocation["longitude"]}");
setState(
() {
mapController.addMarker(
MarkerOptions(
position: LatLng(37.421971, -122.084056),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
),
);
mapController.addMarker(
MarkerOptions(
position: LatLng(currentLocation["latitude"],currentLocation["longitude"]),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
),
);});
} on Exception {
currentLocation = null;
}
}
#override
void initState() {
_getLocation();
super.initState();
}
#override
Widget build(BuildContext context) {
return GoogleMap(
options:GoogleMapOptions(
myLocationEnabled: true,
),
);
}
}
Marker with my selected coordinates appears but marker with my location dosen't.
Any assistance is very much appreciated.

Use location package to detect location which will provide you the lat long coordinates and you can add marker as you have in your code.

Related

How to add markers from the firestore in the google maps?

I have tried to build a code from the internet which should draw two markers in goole maps. The difficulty is that the coordinates come from a cloud firestore. I don't know why, but I can`t see the markers. I did it the same way the people in the tutorial told me.
This is a screenshot of my firestore:
And this is the code I tried:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class maps2 extends StatefulWidget {
#override
_maps2State createState() => _maps2State();
}
class _maps2State extends State<maps2> {
GoogleMapController controller;
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
void initMarker(specify, specifyId) async {
var markerIdVal = specifyId;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position:
LatLng(specify['location'].latitude, specify['location'].longitude),
infoWindow: InfoWindow(title: 'Shop', snippet: specify['name']),
);
setState(() {
markers[markerId] = marker;
});
}
getMarkerData() async {
Firestore.instance.collection('data').getDocuments().then((myMockDoc) {
if (myMockDoc.documents.isNotEmpty) {
for (int i = 0; i < myMockDoc.documents.length; i++) {
initMarker(
myMockDoc.documents[i].data, myMockDoc.documents[i].documentID);
}
}
});
}
#override
void initState() {
getMarkerData();
super.initState();
}
#override
Widget build(BuildContext context) {
Set<Marker> getMarker() {
return <Marker>[
Marker(
markerId: MarkerId('Shop'),
position: LatLng(21.1458, 79.0882),
icon: BitmapDescriptor.defaultMarker,
infoWindow: InfoWindow(title: 'Home'))
].toSet();
}
return Scaffold(
body: GoogleMap(
markers: Set<Marker>.of(markers.values),
mapType: MapType.normal,
initialCameraPosition:
CameraPosition(target: LatLng(21.1458, 79.0882), zoom: 12.0),
onMapCreated: (GoogleMapController controller) {
controller = controller;
},
),
);
}
}
You need to change the code to:
initMarker(
myMockDoc.documents[i].data(), myMockDoc.documents[i].documentID);
}

How can I track user location continuously in Flutter?

I want to track user location. For this reason, I am using location package of flutter. Likewise, I am using Streams to track in real-time. but how can I show user location on the Google Maps in real time. I have some problem about showing real-time location on Google Maps.
** Here is my Location Service class **
class LocationService {
UserLocation _currentLocationofUser;
Location location = Location();
//Sürekli güncelle
StreamController<UserLocation> _locationController =
StreamController<UserLocation>.broadcast();
LocationService() {
location.requestPermission().then((granted) {
if (granted != null) {
location.onLocationChanged.listen((locationData) {
if (locationData != null) {
_locationController.add(UserLocation(
latitude: locationData.latitude,
longitude: locationData.longitude));
}
});
}
});
}
Stream<UserLocation> get locationStream => _locationController.stream;
** Here is my main class to show real-time location of user **
#override
_HomeViewState createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
GoogleMapController _mapController;
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
var userLocation = Provider.of<UserLocation>(context);
return GoogleMap(
mapType: MapType.normal,
initialCameraPosition: CameraPosition(
zoom: 12,
target: LatLng(userLocation.latitude, userLocation.longitude)),
onMapCreated: (GoogleMapController controller) {
_mapController = controller;
},
);
}
}

Google Maps Marker Not Displaying From Firebase

GoogleMaps markers are not displaying after I query Firebase where location coordinates are stored as geopoints (latitude and longitude) under the collection 'markers.'
I have followed the changelog on the Flutter GoogleMaps plugin where markers where made as their own widget, without any success. I connect to the database using Firebase.instance and query through 'markers.' The for loops attempts to return the relevant information. populateClients and initMarkers are the the methods that are created to add the markers to the map, using the process described in the GoogleMaps recent marker update.
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class FirstScreen extends StatefulWidget {
const FirstScreen({Key key}) : super(key: key);
#override
State<FirstScreen> createState() => _FirstScreen();
}
class _FirstScreen extends State<FirstScreen> {
GoogleMapController mapController;
var currentLocation;
var clients = [];
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
#override
void initState(){
super.initState();
populateClients();
Geolocator().getCurrentPosition().then((currloc){
setState(() {
currentLocation = currloc;
});
});
}
populateClients() {
Firestore.instance.collection('markers').getDocuments().then((docs)
{
if(docs.documents.isNotEmpty) {
for(int i = 0; i < docs.documents.length; i++) {
//clients.add(docs.documents[i].data);
initMarker(docs.documents[i].data,
docs.documents[i].documentID);
}
}
});
}
void initMarker(client, markerRef) {
var markerIDVal = markerRef;
final MarkerId markerId = MarkerId(markerIDVal);
//new marker
final Marker marker = Marker(
position: LatLng(client['location'].latitude,
client['location'].longitude),
markerId: markerId,
);
setState(() {
// adding a new marker to map
markers[markerId] = marker;
});
}
#override
Widget build(BuildContext context) {
return currentLocation == null ? Container(
alignment: Alignment.center,
child: Center(
child: CircularProgressIndicator(),
),
):
Stack(
children: <Widget>[
GoogleMap(
initialCameraPosition:
CameraPosition(target: LatLng(currentLocation.latitude,
currentLocation.longitude), zoom: 17),
onMapCreated: _onMapCreated,
myLocationEnabled: true,
mapType: MapType.normal,
markers: Set<Marker>.of(markers.values),
),
],
);
}
void _onMapCreated(GoogleMapController controller) {
setState(() {
mapController = controller;
});
}
}
The markers would display on the geolocation points that are stored in Firebase.

How to get lat and long coordinates on location from google maps in flutter?

I am using flutter 1.3** version it's not defined markers syntax in GoogleMap()
GoogleMap(
onMapCreated: _onMapCreated,
markers: markers,
)
So i m using below code to try to get lat and long from map
void _onMapCreated(GoogleMapController controller){
this._controller = controller;
controller.onMarkerTapped.add(_onMarkerTapped);
}
void _onMarkerTapped(Marker marker) {
...
}
Widget build(BuildContext context) {
GoogleMap(
onMapCreated: _onMapCreated,
options: GoogleMapOptions(
...
));
}
My question is how to get lat and long using MarkerOptions and above methods into string values?
LocationData currentLocation;
getLocation() async {
var location = new Location();
location.onLocationChanged().listen(( currentLocation) {
print(currentLocation.latitude);
print(currentLocation.longitude);
setState(() {
latLng = LatLng(currentLocation.latitude, currentLocation.longitude);
});
print("getLocation:$latLng");
loading = false;
});
}
From this above code, you simply get a Lattitude and longitude
and use this latLng variable on
initialCameraPosition: CameraPosition(
target: latLng,
zoom: 17.4746,
),
this method in GoogleMap
This Code Maybe Helpful to you.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Google Maps Demo',
home: MapSample(),
);
}
}
class MapSample extends StatefulWidget {
#override
State<MapSample> createState() => MapSampleState();
}
class MapSampleState extends State<MapSample> {
Completer<GoogleMapController> _controller = Completer();
static final CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(37.42796133580664, -122.085749655962),
zoom: 14.4746,
);
static final CameraPosition _kLake = CameraPosition(
bearing: 192.8334901395799,
target: LatLng(37.43296265331129, -122.08832357078792),
tilt: 59.440717697143555,
zoom: 19.151926040649414);
#override
Widget build(BuildContext context) {
return new Scaffold(
body: GoogleMap(
mapType: MapType.hybrid,
initialCameraPosition: _kGooglePlex,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _goToTheLake,
label: Text('To the lake!'),
icon: Icon(Icons.directions_boat),
),
);
}
Future<void> _goToTheLake() async {
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(_kLake));
}
}

How to make marker in flutter google map move with camera and not with map

i watched a video in which the location marker moved with the camera and was not stuck to the map. I am not sure if this is the official google map for flutter, but if it is how was this achieved.
i tried passing the controllers camera position to the marker, but it didnt work, as i am not sure the markers position got updated with the camera movement.
mapController.addMarker(MarkerOptions(
icon: BitmapDescriptor.fromAsset("assets/images/marker.png"),
position: LatLng(mapController.cameraPosition.target.latitude,mapController.cameraPosition.target.longitude),
infoWindowText: InfoWindowText("Pick up location", "Pick up location")
));
You can do that by creating a new marker every time the camera moves but providing the same marker id. It won't, actually, create a new marker but it will start moving the same marker according to the camera position.
Here's how you can do it:
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class YourMapView extends StatefulWidget {
#override
_YourMapViewState createState() => _YourMapViewState();
}
class _YourMapViewState extends State<YourMapView> {
final markers = Set<Marker>();
MarkerId markerId = MarkerId("YOUR-MARKER-ID");
LatLng latLng = LatLng(43.2994, 74.2179);
#override
void initState() {
markers.add(
Marker(
markerId: markerId,
position: latLng,
),
);
super.initState();
}
#override
Widget build(BuildContext context) {
return GoogleMap(
initialCameraPosition: CameraPosition(target: latLng,zoom: 14.34),
markers: markers,
onCameraMove: (position){
setState(() {
markers.add(Marker(markerId: markerId,position: position.target));
});
},
);
}
}
you can do it using these properties.
onCameraMove: ,
onCameraIdle: ,
onCameraMoveStarted:
,
If you want the marker just for visual purposes, you can place a "fake" marker on top of the map that is always centered and get the camera position to get the actual camera coordinates.
the other way is to update the marker onCameraMove, the marker will not perfeclty be at the center everytime and it will jerk.