Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Debugger 333
Debugger 333

Posted on

     

Adding google maps to flutter app

* Note: All the code discussed in this blog can only work on a android emulator or realtime android device

Setup

The first step is to add the Google Maps Flutter plugin as a dependency in the pubspec.yaml file.
  dependencies:    ...  google_maps_flutter: ^0.4.0
Enter fullscreen modeExit fullscreen mode

The next step is getting an API key for both Android and iOS but we are going to deal with Android, follow the instructions athttps://developers.google.com/maps/documentation/android-sdk/get-api-key to get API Key. Once you have your API key, add it to your Flutter app in the application manifest (android/app/src/main/AndroidManifest.xml), as follows:

  <manifest ...
<application ...
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR ANDROID API KEY HERE"/>
Enter fullscreen modeExit fullscreen mode




Adding a GoogleMap widget

Now you are ready to add a GoogleMap widget! Run flutter clean to make sure the API key changes are picked up on the next build. Then, add a GoogleMap widget that covers the entire screen:

import 'dart:async';

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

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

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
Completer<GoogleMapController> _controller = Completer();

static const LatLng _center = const LatLng(45.521563, -122.677433);

void _onMapCreated(GoogleMapController controller) {
_controller.complete(controller);
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Maps Sample App'),
backgroundColor: Colors.green[700],
),
body: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
),
),
);
}
}

Enter fullscreen modeExit fullscreen mode




Testing the app

After you have completed doing all this stuff type "flutter run" to run your app.

The app should look as follows:

Alt Text

Alt Text

https://github.com/neev123dev
https://dev.to/neev123dev

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
getjv profile image
Jhonatan Morais
Experienced in high-traffic systems, microservices, and team leadership. Strong in decision-making, process optimization, and committed to driving innovation and efficiency in projects and teams.
  • Location
    Brazil
  • Work
    Software Engineer
  • Joined

ty

CollapseExpand
 
debugger333 profile image
Debugger 333
😴 sleeping
  • Location
    India
  • Work
    Flutter Dev
  • Joined

means?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

😴 sleeping
  • Location
    India
  • Work
    Flutter Dev
  • Joined

More fromDebugger 333

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp