Flutter: Байршил хэрхэн тодорхойлох вэ?

Jagaa
2 min readOct 22, 2020

Google location services + Firebase

Алхам 1: firebase_core, google_maps_flutter package

Алхам 2: API key авахын тулд Firebase app үүсгэнэ.

Алхам 3: Android

google-services.json-оос API key авч

"api_key": [
{
"current_key": "AIzaSyC-saKl-KaHT2OAMgxcLU_ZlsqL5EBcRvE"
}
],

AndroidManifest.xml-ын <application> tag дотор нэмнэ.

<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
android:label="flutter_location_demo">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyC-saKl-KaHT2OAMgxcLU_ZlsqL5EBcRvE" />
...

Алхам 4: iOS

GoogleService-Info.plist-ээс API_KEY авч

<key>API_KEY</key>
<string>AIzaSyBCY-Y505zuvOphwFLpqhMb6GIhKVOMqxI</string>

ios/Runner/AppDelegate.swift файлд дараах кодыг нэмнэ.

import UIKit
import Flutter
import Firebase
import GoogleMaps@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// Custom code: Google firebase
if FirebaseApp.app() == nil {
FirebaseApp.configure()
}
// Custome code: Google map
GMSServices.provideAPIKey("AIzaSyApKVXsvEzfNgjp9LdR4Ev4QpyCmqLQHI0")

return super.application(application, didFinishLaunchingWithOptions: launchOptions) }
}

Алхам 4: Android, iOS SDK идэвхижүүлэх

Алхам 5: Кодын хэсэг

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

--

--