Location Map Using Flutter
Location Map Using Flutter
Flutter has just achieved its latest milestone with the release of stable
1.0 version. It has many amazing features that provide a foundation
for amazing future ahead as it matures. Here are several of the great
features:
You can download the source code of the project in the GitHub
repository below:
alfianlosari/flutter_placez
Display & Search your nearby places.
This app uses Flutter 1.0
PlatformView to display Google Map
as a Flutter Widget…
github.com
• Building Detail Screen that displays the place location inside the
Map with associated information (name, address, photos, etc)
Our app uses Google Maps & Google Places services under the hood to
display map view and fetch nearby places, so we need Google API Key
for Map & Places. Make sure to create the API key from the site below.
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import "GoogleMaps/GoogleMaps.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)
launchOptions {
[GMSServices provideAPIKey:@"YOUR KEY HERE"];
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application
didFinishLaunchingWithOptions:launchOptions];
}
@end
3. Android platform need to specify your API key and add permissions
in the application manifest
android/app/src/main/AndroidManifest.xml
<manifest ...
<uses-permission
android:name="android.permission.INTERNET"/> <uses-
permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<application ...
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR KEY HERE"/>
Building Trending Around Places
List Screen
In our main screen, we will ask user to grant GPS access and access
their location, then we will ask the Google Places API to get nearby
places to visit around. Make sure to paste your Google API key in the
YOUR_API_KEY placeholder.
with MapView as the first item, the second item wraps the
ListView in an Expanded widget so it can flex all the remaining
vertical space of the screen.
2. When the MapView has been created, a method is invoked to
assign the MapControlller as the widget property, then it calls
the refresh method that will asks user their current location
using location plugin, then using the location it will invoke
getNearbyPlaces to retrieve the nearby places using
flutter_google_places plugin, then assigned the places as the
state of the widget.
This screen displays the detail of the Places when user taps on the row
of the main list screen with a MapView and the pin marker. Make sure
to paste your Google API key in the YOUR_API_KEY placeholder
When user taps on the search ActionBar un the main list AppBar . We
will display a search bar where user can enter the name of the place
they want to search. Then it will provide Autocomplete suggestions
using Google Places autocomplete API for user to choose using a
dropdown.
When they tap on the place, we just navigate to the place detail widget
passing the placeId just like the nearby places item in the main
widget ListView.
Conclusion
1 import 'package:google_maps_webservice/places.dart';
2 import 'package:flutter/material.dart';
That’s
3 a wrap!.
import In such a short time we have been able to build cross
'package:google_maps_flutter/google_maps_flutter.dart'
platform
4 native mobile application with high performance and rapid
development
5 constthat runs on Android
kGoogleApiKey & iOS. Flutter provides unlimited
= "YOUR_API_KEY";
potential
6 to us mobile
GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);
developers to create the most delightful app to our users.
7 The sky is the
limit!
8 .
class PlaceDetailWidget extends StatefulWidget {
9 String placeId;
10
11 PlaceDetailWidget(String placeId) {
12 this.placeId = placeId;
13 }
14
15 @override
16 State<StatefulWidget> createState() {
17 return PlaceDetailState();
18 }
19 }
20
21 class PlaceDetailState extends State<PlaceDetailWidget> {
22 GoogleMapController mapController;
23 PlacesDetailsResponse place;
24 bool isLoading = false;
25 String errorLoading;
26
27 @override
28 void initState() {
29 fetchPlaceDetail();
30 super.initState();
31 }
32
33 @override
34 Widget build(BuildContext context) {
35 Widget bodyChild;
36 String title;
37 if (isLoading) {
38 title = "Loading";
39 bodyChild = Center(
40 child: CircularProgressIndicator(
41 value: null,
42 ),
43 );
44 } else if (errorLoading != null) {
45 title = "";
46 bodyChild = Center(
47 child: Text(errorLoading),
48 );
49 } else {
50 final placeDetail = place.result;
51 final location = place.result.geometry.location;
52 final lat = location.lat;
53 final lng = location.lng;
54 final center = LatLng(lat, lng);
55