How to activate location when clicking on button using google maps flutter?Flutter: How to add marker to Google Maps with new Marker API?How to save an Android Activity state using save instance state?How do I pass data between Activities in Android application?How do I create a transparent Activity on Android?How to disable mouse scroll wheel scaling with Google Maps APIHow to prevent a dialog from closing when a button is clickedGoogle Maps JS API v3 - Simple Multiple Marker ExampleHow to start new activity on button clickDilemma: when to use Fragments vs Activities:Google Maps & JavaFX: Display marker on the map after clicking JavaFX buttonHow to offset a scaffold widget in Flutter?

If I have an accident, should I file a claim with my car insurance company?

SQL Always On COPY ONLY backups - what's the point if I cant restore the AG from these backups?

Why did Tony's Arc Reactor do this?

Temporarily simulate being offline programmatically

Looking for the comic book where Spider-Man was [mistakenly] addressed as Super-Man

What quests do you need to stop at before you make an enemy of a faction for each faction?

Round away from zero

Can my imp familiar still talk when shapshifted (to a raven, if that matters)?

Why did Boris Johnson call for new elections?

Why does the seven segment display have decimal point at the right?

How do I delete cookies from a specific site?

Pronounceable encrypted text

In-universe, why does Doc Brown program the time machine to go to 1955?

How to calculate the power level of a Commander deck?

What are the solutions of this Diophantine equation?

What makes an ending "happy"?

What do English-speaking kids call ice-cream on a stick?

Supervisor wants me to support a diploma-thesis SW tool after I graduated

How to interpret or parse this confusing 'NOT' and 'AND' legal clause

How do German speakers decide what should be on the left side of the verb?

Is there some sort of French saying for "a person's signature move"?

GFI outlets tripped after power outage

These roommates throw strange parties

Is it right to use the ideas of non-winning designers in a design contest?



How to activate location when clicking on button using google maps flutter?


Flutter: How to add marker to Google Maps with new Marker API?How to save an Android Activity state using save instance state?How do I pass data between Activities in Android application?How do I create a transparent Activity on Android?How to disable mouse scroll wheel scaling with Google Maps APIHow to prevent a dialog from closing when a button is clickedGoogle Maps JS API v3 - Simple Multiple Marker ExampleHow to start new activity on button clickDilemma: when to use Fragments vs Activities:Google Maps & JavaFX: Display marker on the map after clicking JavaFX buttonHow to offset a scaffold widget in Flutter?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am new using Flutter and i am making a app. In that i need to use google maps and show my location only if activated by a button.



I am using the example in:



https://pub.dartlang.org/packages/google_maps_flutter



That give you a button that redirect the camera to a certain point, but how can i turn that button to be the trigger of using my location or not?



I made something like this but i get an error:



import 'dart:async';

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

void main() => runApp(Lugares());

class Lugares extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Lugares',
home: MapSample(),
debugShowCheckedModeBanner: false,
);



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);
,
myLocationEnabled: true,
),
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(CameraPosition(
target: getUserLocation() == null ? LatLng(0, 0) : getUserLocation(), zoom: 15.0)));


Future<LatLng> getUserLocation() async
var currentLocation = <String, double>;
final location = LocationManager.Location();
try
currentLocation = (await location.getLocation()) as Map<String, double>;
final lat = currentLocation["latitude"];
final lng = currentLocation["longitude"];
final center = LatLng(lat, lng);
return center;
on Exception
currentLocation = null;
return null;






UPDATE:



Thanks to @Vishal G. Gohel that share his code i could acomplish what i want, this is the code:



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

void main() => runApp(Lugares());

class Lugares extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Lugares',
home: MapSample(),
debugShowCheckedModeBanner: false,
);



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

@override
Widget build(BuildContext context)
return new Scaffold(
body: GoogleMap(
mapType: MapType.hybrid,
initialCameraPosition: _kGooglePlex,
onMapCreated: (GoogleMapController controller)
_controller.complete(controller);
,
myLocationEnabled: true,
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _currentLocation,
label: Text('Ir a mi Ubicacion!'),
icon: Icon(Icons.location_on),
),
);




void _currentLocation() async
final GoogleMapController controller = await _controller.future;
LocationData currentLocation;
var location = new Location();
try
currentLocation = await location.getLocation();
on Exception
currentLocation = null;


controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(currentLocation.latitude, currentLocation.longitude),
zoom: 17.0,
),
));












share|improve this question


























  • what error are you facing ?

    – Vishal G. Gohel
    Mar 28 at 5:26











  • @VishalG.Gohel i get this error: _TypeError (type 'Future<LatLng>' is not a subtype of type 'LatLng')

    – xtrios
    Mar 28 at 5:48












  • glad to hear that.

    – Vishal G. Gohel
    Mar 28 at 18:14

















1















I am new using Flutter and i am making a app. In that i need to use google maps and show my location only if activated by a button.



I am using the example in:



https://pub.dartlang.org/packages/google_maps_flutter



That give you a button that redirect the camera to a certain point, but how can i turn that button to be the trigger of using my location or not?



I made something like this but i get an error:



import 'dart:async';

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

void main() => runApp(Lugares());

class Lugares extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Lugares',
home: MapSample(),
debugShowCheckedModeBanner: false,
);



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);
,
myLocationEnabled: true,
),
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(CameraPosition(
target: getUserLocation() == null ? LatLng(0, 0) : getUserLocation(), zoom: 15.0)));


Future<LatLng> getUserLocation() async
var currentLocation = <String, double>;
final location = LocationManager.Location();
try
currentLocation = (await location.getLocation()) as Map<String, double>;
final lat = currentLocation["latitude"];
final lng = currentLocation["longitude"];
final center = LatLng(lat, lng);
return center;
on Exception
currentLocation = null;
return null;






UPDATE:



Thanks to @Vishal G. Gohel that share his code i could acomplish what i want, this is the code:



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

void main() => runApp(Lugares());

class Lugares extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Lugares',
home: MapSample(),
debugShowCheckedModeBanner: false,
);



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

@override
Widget build(BuildContext context)
return new Scaffold(
body: GoogleMap(
mapType: MapType.hybrid,
initialCameraPosition: _kGooglePlex,
onMapCreated: (GoogleMapController controller)
_controller.complete(controller);
,
myLocationEnabled: true,
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _currentLocation,
label: Text('Ir a mi Ubicacion!'),
icon: Icon(Icons.location_on),
),
);




void _currentLocation() async
final GoogleMapController controller = await _controller.future;
LocationData currentLocation;
var location = new Location();
try
currentLocation = await location.getLocation();
on Exception
currentLocation = null;


controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(currentLocation.latitude, currentLocation.longitude),
zoom: 17.0,
),
));












share|improve this question


























  • what error are you facing ?

    – Vishal G. Gohel
    Mar 28 at 5:26











  • @VishalG.Gohel i get this error: _TypeError (type 'Future<LatLng>' is not a subtype of type 'LatLng')

    – xtrios
    Mar 28 at 5:48












  • glad to hear that.

    – Vishal G. Gohel
    Mar 28 at 18:14













1












1








1


1






I am new using Flutter and i am making a app. In that i need to use google maps and show my location only if activated by a button.



I am using the example in:



https://pub.dartlang.org/packages/google_maps_flutter



That give you a button that redirect the camera to a certain point, but how can i turn that button to be the trigger of using my location or not?



I made something like this but i get an error:



import 'dart:async';

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

void main() => runApp(Lugares());

class Lugares extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Lugares',
home: MapSample(),
debugShowCheckedModeBanner: false,
);



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);
,
myLocationEnabled: true,
),
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(CameraPosition(
target: getUserLocation() == null ? LatLng(0, 0) : getUserLocation(), zoom: 15.0)));


Future<LatLng> getUserLocation() async
var currentLocation = <String, double>;
final location = LocationManager.Location();
try
currentLocation = (await location.getLocation()) as Map<String, double>;
final lat = currentLocation["latitude"];
final lng = currentLocation["longitude"];
final center = LatLng(lat, lng);
return center;
on Exception
currentLocation = null;
return null;






UPDATE:



Thanks to @Vishal G. Gohel that share his code i could acomplish what i want, this is the code:



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

void main() => runApp(Lugares());

class Lugares extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Lugares',
home: MapSample(),
debugShowCheckedModeBanner: false,
);



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

@override
Widget build(BuildContext context)
return new Scaffold(
body: GoogleMap(
mapType: MapType.hybrid,
initialCameraPosition: _kGooglePlex,
onMapCreated: (GoogleMapController controller)
_controller.complete(controller);
,
myLocationEnabled: true,
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _currentLocation,
label: Text('Ir a mi Ubicacion!'),
icon: Icon(Icons.location_on),
),
);




void _currentLocation() async
final GoogleMapController controller = await _controller.future;
LocationData currentLocation;
var location = new Location();
try
currentLocation = await location.getLocation();
on Exception
currentLocation = null;


controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(currentLocation.latitude, currentLocation.longitude),
zoom: 17.0,
),
));












share|improve this question
















I am new using Flutter and i am making a app. In that i need to use google maps and show my location only if activated by a button.



I am using the example in:



https://pub.dartlang.org/packages/google_maps_flutter



That give you a button that redirect the camera to a certain point, but how can i turn that button to be the trigger of using my location or not?



I made something like this but i get an error:



import 'dart:async';

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

void main() => runApp(Lugares());

class Lugares extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Lugares',
home: MapSample(),
debugShowCheckedModeBanner: false,
);



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);
,
myLocationEnabled: true,
),
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(CameraPosition(
target: getUserLocation() == null ? LatLng(0, 0) : getUserLocation(), zoom: 15.0)));


Future<LatLng> getUserLocation() async
var currentLocation = <String, double>;
final location = LocationManager.Location();
try
currentLocation = (await location.getLocation()) as Map<String, double>;
final lat = currentLocation["latitude"];
final lng = currentLocation["longitude"];
final center = LatLng(lat, lng);
return center;
on Exception
currentLocation = null;
return null;






UPDATE:



Thanks to @Vishal G. Gohel that share his code i could acomplish what i want, this is the code:



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

void main() => runApp(Lugares());

class Lugares extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Lugares',
home: MapSample(),
debugShowCheckedModeBanner: false,
);



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

@override
Widget build(BuildContext context)
return new Scaffold(
body: GoogleMap(
mapType: MapType.hybrid,
initialCameraPosition: _kGooglePlex,
onMapCreated: (GoogleMapController controller)
_controller.complete(controller);
,
myLocationEnabled: true,
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _currentLocation,
label: Text('Ir a mi Ubicacion!'),
icon: Icon(Icons.location_on),
),
);




void _currentLocation() async
final GoogleMapController controller = await _controller.future;
LocationData currentLocation;
var location = new Location();
try
currentLocation = await location.getLocation();
on Exception
currentLocation = null;


controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(currentLocation.latitude, currentLocation.longitude),
zoom: 17.0,
),
));









android google-maps flutter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 14:55







xtrios

















asked Mar 28 at 5:01









xtriosxtrios

389 bronze badges




389 bronze badges















  • what error are you facing ?

    – Vishal G. Gohel
    Mar 28 at 5:26











  • @VishalG.Gohel i get this error: _TypeError (type 'Future<LatLng>' is not a subtype of type 'LatLng')

    – xtrios
    Mar 28 at 5:48












  • glad to hear that.

    – Vishal G. Gohel
    Mar 28 at 18:14

















  • what error are you facing ?

    – Vishal G. Gohel
    Mar 28 at 5:26











  • @VishalG.Gohel i get this error: _TypeError (type 'Future<LatLng>' is not a subtype of type 'LatLng')

    – xtrios
    Mar 28 at 5:48












  • glad to hear that.

    – Vishal G. Gohel
    Mar 28 at 18:14
















what error are you facing ?

– Vishal G. Gohel
Mar 28 at 5:26





what error are you facing ?

– Vishal G. Gohel
Mar 28 at 5:26













@VishalG.Gohel i get this error: _TypeError (type 'Future<LatLng>' is not a subtype of type 'LatLng')

– xtrios
Mar 28 at 5:48






@VishalG.Gohel i get this error: _TypeError (type 'Future<LatLng>' is not a subtype of type 'LatLng')

– xtrios
Mar 28 at 5:48














glad to hear that.

– Vishal G. Gohel
Mar 28 at 18:14





glad to hear that.

– Vishal G. Gohel
Mar 28 at 18:14












1 Answer
1






active

oldest

votes


















1
















You can check below method which i made for get current location.



void _currentLocation() async 
Location _location = new Location();
Map<String, double> location;

try
location = await _location.getLocation();
on PlatformException catch (e)
print(e.message);
location = null;


mapController.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(location["latitude"], location["longitude"]),
zoom: 17.0,
),
));

mapController.addMarker(
MarkerOptions(
position: LatLng(location["latitude"], location["longitude"]),
),
);






share|improve this answer

























  • unfortunately the API changed, addMarker doesn't work any longer, click here to see how it's done now stackoverflow.com/questions/55000043/…

    – karlpy
    May 29 at 22:05










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55390480%2fhow-to-activate-location-when-clicking-on-button-using-google-maps-flutter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1
















You can check below method which i made for get current location.



void _currentLocation() async 
Location _location = new Location();
Map<String, double> location;

try
location = await _location.getLocation();
on PlatformException catch (e)
print(e.message);
location = null;


mapController.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(location["latitude"], location["longitude"]),
zoom: 17.0,
),
));

mapController.addMarker(
MarkerOptions(
position: LatLng(location["latitude"], location["longitude"]),
),
);






share|improve this answer

























  • unfortunately the API changed, addMarker doesn't work any longer, click here to see how it's done now stackoverflow.com/questions/55000043/…

    – karlpy
    May 29 at 22:05















1
















You can check below method which i made for get current location.



void _currentLocation() async 
Location _location = new Location();
Map<String, double> location;

try
location = await _location.getLocation();
on PlatformException catch (e)
print(e.message);
location = null;


mapController.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(location["latitude"], location["longitude"]),
zoom: 17.0,
),
));

mapController.addMarker(
MarkerOptions(
position: LatLng(location["latitude"], location["longitude"]),
),
);






share|improve this answer

























  • unfortunately the API changed, addMarker doesn't work any longer, click here to see how it's done now stackoverflow.com/questions/55000043/…

    – karlpy
    May 29 at 22:05













1














1










1









You can check below method which i made for get current location.



void _currentLocation() async 
Location _location = new Location();
Map<String, double> location;

try
location = await _location.getLocation();
on PlatformException catch (e)
print(e.message);
location = null;


mapController.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(location["latitude"], location["longitude"]),
zoom: 17.0,
),
));

mapController.addMarker(
MarkerOptions(
position: LatLng(location["latitude"], location["longitude"]),
),
);






share|improve this answer













You can check below method which i made for get current location.



void _currentLocation() async 
Location _location = new Location();
Map<String, double> location;

try
location = await _location.getLocation();
on PlatformException catch (e)
print(e.message);
location = null;


mapController.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(location["latitude"], location["longitude"]),
zoom: 17.0,
),
));

mapController.addMarker(
MarkerOptions(
position: LatLng(location["latitude"], location["longitude"]),
),
);







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 5:51









Vishal G. GohelVishal G. Gohel

7971 gold badge13 silver badges24 bronze badges




7971 gold badge13 silver badges24 bronze badges















  • unfortunately the API changed, addMarker doesn't work any longer, click here to see how it's done now stackoverflow.com/questions/55000043/…

    – karlpy
    May 29 at 22:05

















  • unfortunately the API changed, addMarker doesn't work any longer, click here to see how it's done now stackoverflow.com/questions/55000043/…

    – karlpy
    May 29 at 22:05
















unfortunately the API changed, addMarker doesn't work any longer, click here to see how it's done now stackoverflow.com/questions/55000043/…

– karlpy
May 29 at 22:05





unfortunately the API changed, addMarker doesn't work any longer, click here to see how it's done now stackoverflow.com/questions/55000043/…

– karlpy
May 29 at 22:05








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55390480%2fhow-to-activate-location-when-clicking-on-button-using-google-maps-flutter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript