Created
August 9, 2019 13:44
-
-
Save thekarel/3d91cbde168bbd9a5623bcac91ee1600 to your computer and use it in GitHub Desktop.
Get lat/lon of a place selected via autocomplete dialog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using https://github.com/fluttercommunity/flutter_google_places | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_google_places/flutter_google_places.dart'; | |
import 'package:google_maps_webservice/places.dart'; | |
const googleApiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: googleApiKey); | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold(appBar: AppBar(title: Text('Map App π')), body: Map()); | |
} | |
} | |
class Map extends StatelessWidget { | |
Future<void> _findPlace(BuildContext ctx) async { | |
Prediction placePrediction = await PlacesAutocomplete.show( | |
context: ctx, | |
apiKey: googleApiKey, | |
mode: Mode.overlay, // Mode.fullscreen | |
language: "en", | |
components: [ | |
Component(Component.country, "us"), | |
Component(Component.country, "ca") | |
]); | |
PlacesDetailsResponse geoInfo = | |
await _places.getDetailsByPlaceId(placePrediction.placeId); | |
// This is it πππ | |
print(geoInfo.result.geometry.location.lat); | |
print(geoInfo.result.geometry.location.lng); | |
} | |
@override | |
Widget build(BuildContext ctx) { | |
return Container( | |
child: RaisedButton.icon( | |
icon: Icon(Icons.map), | |
label: Text('Map it'), | |
onPressed: () { | |
_findPlace(ctx); | |
}, | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment