1. परिचय
Flutter, Google का मोबाइल ऐप्लिकेशन SDK टूल है. इसकी मदद से, iOS और Android के लिए अच्छी क्वालिटी वाले नेटिव ऐप्लिकेशन बहुत कम समय में बनाए जा सकते हैं.
Google Maps Flutter प्लगिन की मदद से, अपने ऐप्लिकेशन में Google Maps के डेटा पर आधारित मैप जोड़े जा सकते हैं. यह प्लगिन, Google Maps सर्वर को ऐक्सेस करने, मैप दिखाने, और उपयोगकर्ता के जेस्चर (जैसे, क्लिक और ड्रैग) का जवाब देने की प्रोसेस को अपने-आप मैनेज करता है. मैप में मार्कर भी जोड़े जा सकते हैं. इन ऑब्जेक्ट से, मैप पर मौजूद जगहों के बारे में अतिरिक्त जानकारी मिलती है. साथ ही, इनसे उपयोगकर्ता को मैप के साथ इंटरैक्ट करने की सुविधा मिलती है.
आपको क्या बनाने को मिलेगा
इस कोडलैब में, Flutter SDK का इस्तेमाल करके Google मैप वाला मोबाइल ऐप्लिकेशन बनाया जाएगा. आपका ऐप्लिकेशन:
|
|
Flutter क्या है?
Flutter में तीन मुख्य सुविधाएं हैं.
- तेज़ी से डेवलप किया जा सकता है: स्टेटफ़ुल हॉट रिलोड की मदद से, Android और iOS ऐप्लिकेशन को कुछ ही मिलीसेकंड में बनाया जा सकता है.
- ज़्यादा जानकारी देने वाला और लचीला: असली उपयोगकर्ताओं के अनुभव पर ध्यान देते हुए, सुविधाओं को तुरंत शिप करें.
- iOS और Android, दोनों पर नेटिव परफ़ॉर्मेंस: Flutter के विजेट में, प्लैटफ़ॉर्म के सभी अहम अंतर शामिल होते हैं. जैसे, स्क्रोलिंग, नेविगेशन, आइकॉन, और फ़ॉन्ट. इससे पूरी नेटिव परफ़ॉर्मेंस मिलती है.
Google Maps में ये सुविधाएं हैं:
- दुनिया के 99% हिस्से का कवरेज: 200 से ज़्यादा देशों और इलाकों के लिए, भरोसेमंद और पूरी जानकारी वाला डेटा इस्तेमाल करके ऐप्लिकेशन, टूल वगैरह बनाएं.
- हर दिन 2.5 करोड़ अपडेट: सटीक और रीयल-टाइम में जगह की जानकारी पर भरोसा करें.
- हर महीने एक अरब से ज़्यादा ऐक्टिव उपयोगकर्ता: Google Maps के इन्फ़्रास्ट्रक्चर की मदद से, भरोसे के साथ अपने कारोबार को बढ़ाएं.
इस कोडलैब में, iOS और Android, दोनों के लिए Flutter ऐप्लिकेशन में Google Maps को इंटिग्रेट करने का तरीका बताया गया है.
आपको क्या सीखने को मिलेगा
- नया Flutter ऐप्लिकेशन बनाने का तरीका.
- Google Maps Flutter प्लगिन को कॉन्फ़िगर करने का तरीका.
- वेब सेवा से मिली जगह की जानकारी का इस्तेमाल करके, मैप में मार्कर जोड़ने का तरीका.
इस कोडलैब में, Flutter ऐप्लिकेशन में Google मैप जोड़ने पर फ़ोकस किया गया है. इसमें काम के न होने वाले कॉन्सेप्ट और कोड ब्लॉक को शामिल नहीं किया गया है. इन्हें सिर्फ़ कॉपी और चिपकाने के लिए उपलब्ध कराया गया है.
आपको इस कोडलैब से क्या सीखना है?
2. Flutter एनवायरमेंट सेट अप करना
इस लैब को पूरा करने के लिए, आपको दो सॉफ़्टवेयर की ज़रूरत होगी: Flutter SDK और एक एडिटर. इस कोडलैब में Android Studio का इस्तेमाल किया गया है. हालांकि, अपनी पसंद के एडिटर का इस्तेमाल किया जा सकता है.
इस कोडलैब को इनमें से किसी भी डिवाइस पर चलाया जा सकता है:
- आपके कंप्यूटर से कनेक्ट किया गया कोई फ़िज़िकल डिवाइस (Android या iOS), जिस पर डेवलपर मोड सेट किया गया हो.
- iOS सिम्युलेटर. (इसके लिए, Xcode टूल इंस्टॉल करना ज़रूरी है.)
- Android एम्युलेटर. (इसके लिए, Android Studio में सेटअप करना ज़रूरी है.)
3. शुरू करना
Flutter का इस्तेमाल शुरू करना
Flutter का इस्तेमाल शुरू करने का सबसे आसान तरीका है कि आप flutter कमांड लाइन टूल का इस्तेमाल करें. इससे आपको कोडिंग शुरू करने के लिए ज़रूरी कोड मिल जाएगा.
$ flutter create google_maps_in_flutter --platforms android,ios,web Creating project google_maps_in_flutter... Resolving dependencies in `google_maps_in_flutter`... Downloading packages... Got dependencies in `google_maps_in_flutter`. Wrote 81 files. All done! You can find general documentation for Flutter at: https://docs.flutter.dev/ Detailed API documentation is available at: https://api.flutter.dev/ If you prefer video documentation, consider: https://www.youtube.com/c/flutterdev In order to run your application, type: $ cd google_maps_in_flutter $ flutter run Your application code is in google_maps_in_flutter/lib/main.dart.
Google Maps Flutter प्लगिन को डिपेंडेंसी के तौर पर जोड़ना
Pub पैकेज का इस्तेमाल करके, Flutter ऐप्लिकेशन में अतिरिक्त सुविधाएं आसानी से जोड़ी जा सकती हैं. इस कोडलैब में, प्रोजेक्ट डायरेक्ट्री से यह कमांड चलाकर Google Maps Flutter प्लगिन के बारे में बताया गया है.
$ cd google_maps_in_flutter $ flutter pub add google_maps_flutter Resolving dependencies... Downloading packages... + csslib 1.0.0 + flutter_plugin_android_lifecycle 2.0.19 + flutter_web_plugins 0.0.0 from sdk flutter + google_maps 7.1.0 + google_maps_flutter 2.6.1 + google_maps_flutter_android 2.8.0 + google_maps_flutter_ios 2.6.0 + google_maps_flutter_platform_interface 2.6.0 + google_maps_flutter_web 0.5.7 + html 0.15.4 + js 0.6.7 (0.7.1 available) + js_wrapping 0.7.4 leak_tracker 10.0.4 (10.0.5 available) leak_tracker_flutter_testing 3.0.3 (3.0.5 available) material_color_utilities 0.8.0 (0.11.1 available) meta 1.12.0 (1.14.0 available) + plugin_platform_interface 2.1.8 + sanitize_html 2.1.0 + stream_transform 2.1.0 test_api 0.7.0 (0.7.1 available) + web 0.5.1 Changed 16 dependencies! 6 packages have newer versions incompatible with dependency constraints. Try `flutter pub outdated` for more information.
iOS platform को कॉन्फ़िगर करना
iOS पर Google Maps SDK का नया वर्शन पाने के लिए, प्लेटफ़ॉर्म का कम से कम iOS 14 वर्शन होना ज़रूरी है. ios/Podfile कॉन्फ़िगरेशन फ़ाइल के सबसे ऊपर दिए गए कॉन्फ़िगरेशन में इस तरह बदलाव करें.
ios/Podfile
# Google Maps SDK requires platform version 14
# https://developers.google.com/maps/flutter-package/config#ios
platform :ios, '14.0'
# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Android minSDK को कॉन्फ़िगर करना
Android पर Google Maps SDK का इस्तेमाल करने के लिए, minSdk को 21 पर सेट करना ज़रूरी है. android/app/build.gradle कॉन्फ़िगरेशन फ़ाइल में इस तरह बदलाव करें.
android/app/build.gradle
android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.example.google_maps_in_flutter"
// Minimum Android version for Google Maps SDK
// https://developers.google.com/maps/flutter-package/config#android
minSdk = 21
targetSdk = flutter.targetSdkVersion
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName
}
}
4. ऐप्लिकेशन में Google Maps जोड़ना
एपीआई पासकोड के बारे में जानकारी
अपने Flutter ऐप्लिकेशन में Google Maps का इस्तेमाल करने के लिए, आपको Google Maps Platform के साथ एक एपीआई प्रोजेक्ट कॉन्फ़िगर करना होगा. इसके लिए, Android के लिए Maps SDK में एपीआई पासकोड का इस्तेमाल करना, iOS के लिए Maps SDK में एपीआई पासकोड का इस्तेमाल करना, और Maps JavaScript API में एपीआई पासकोड का इस्तेमाल करना लेख पढ़ें. एपीआई कुंजियां मिलने के बाद, Android और iOS, दोनों ऐप्लिकेशन को कॉन्फ़िगर करने के लिए यह तरीका अपनाएं.
Android ऐप्लिकेशन के लिए एपीआई पासकोड जोड़ना
Android ऐप्लिकेशन में एपीआई पासकोड जोड़ने के लिए, android/app/src/main में मौजूद AndroidManifest.xml फ़ाइल में बदलाव करें. application नोड में, पिछले चरण में बनाया गया एपीआई पासकोड शामिल करके एक meta-data एंट्री जोड़ें.
android/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="google_maps_in_flutter"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- TODO: Add your Google Maps API key here -->
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR-KEY-HERE"/>
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>
iOS ऐप्लिकेशन के लिए एपीआई पासकोड जोड़ना
iOS ऐप्लिकेशन में एपीआई पासकोड जोड़ने के लिए, ios/Runner में मौजूद AppDelegate.swift फ़ाइल में बदलाव करें. Android के उलट, iOS पर एपीआई पासकोड जोड़ने के लिए, Runner ऐप्लिकेशन के सोर्स कोड में बदलाव करने पड़ते हैं. AppDelegate, ऐप्लिकेशन को शुरू करने की प्रोसेस का मुख्य सिंगलटन होता है.
इस फ़ाइल में दो बदलाव करो. Google Maps के हेडर को पुल करने के लिए, सबसे पहले #import स्टेटमेंट जोड़ें. इसके बाद, GMSServices सिंग्लेटन के provideAPIKey() तरीके को कॉल करें. इस एपीआई पासकोड की मदद से, Google Maps सही तरीके से मैप टाइल दिखा पाता है.
ios/Runner/AppDelegate.swift
import Flutter
import UIKit
import GoogleMaps // Add this import
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// TODO: Add your Google Maps API key
GMSServices.provideAPIKey("YOUR-API-KEY") // Add this line
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
वेब ऐप्लिकेशन के लिए एपीआई पासकोड जोड़ना
वेब ऐप्लिकेशन में एपीआई पासकोड जोड़ने के लिए, web में मौजूद index.html फ़ाइल में बदलाव करें. हेड सेक्शन में, Maps JavaScript स्क्रिप्ट का रेफ़रंस जोड़ें. साथ ही, अपना एपीआई पासकोड भी जोड़ें.
web/index.html
<!DOCTYPE html>
<html>
<head>
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.
The path provided below has to start and end with a slash "/" in order for
it to work correctly.
For more details:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
This is a placeholder for base href that will be replaced by the value of
the `--base-href` argument provided to `flutter build`.
-->
<base href="$FLUTTER_BASE_HREF">
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="A new Flutter project.">
<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="google_maps_in_flutter">
<link rel="apple-touch-icon" href="icons/Icon-192.png">
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>
<!-- Add your Google Maps API key here -->
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR-KEY-HERE"></script>
<title>google_maps_in_flutter</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>
स्क्रीन पर मैप दिखाना
अब स्क्रीन पर मैप देखने का समय है. lib/main.dart के कॉन्टेंट को इससे बदलें.
lib/main.dart
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late GoogleMapController mapController;
final LatLng _center = const LatLng(45.521563, -122.677433);
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
useMaterial3: true,
colorSchemeSeed: Colors.green[700],
),
home: Scaffold(
appBar: AppBar(
title: const Text('Maps Sample App'),
elevation: 2,
),
body: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
),
),
);
}
}
ऐप्लिकेशन चलाना
पोर्टलैंड के बीच में मौजूद मैप का एक व्यू देखने के लिए, iOS या Android में Flutter ऐप्लिकेशन चलाएं. इसके अलावा, Android एम्युलेटर या iOS सिम्युलेटर चलाएं. मैप के सेंटर में अपनी पसंद की जगह सेट करें.
$ flutter run
|
|
5. Google को मैप पर दिखाना
Google के दुनिया भर में कई ऑफ़िस हैं. ये ऑफ़िस उत्तरी अमेरिका, लैटिन अमेरिका, यूरोप, एशिया पैसिफ़िक, और अफ़्रीका और मध्य पूर्व में मौजूद हैं. इन मैप की सबसे अच्छी बात यह है कि इनमें एपीआई एंडपॉइंट का इस्तेमाल करना आसान है. इससे, ऑफ़िस की जगह की जानकारी JSON फ़ॉर्मैट में उपलब्ध कराई जा सकती है. इस चरण में, आपको ऑफ़िस की इन जगहों को मैप पर दिखाना होता है. इस चरण में, JSON को पार्स करने के लिए कोड जनरेट करने की सुविधा का इस्तेमाल किया जाएगा.
प्रोजेक्ट में तीन नई Flutter डिपेंडेंसी इस तरह जोड़ें. आसानी से एचटीटीपी अनुरोध करने के लिए, http पैकेज जोड़ें. JSON दस्तावेज़ों को दिखाने के लिए, ऑब्जेक्ट स्ट्रक्चर का एलान करने के लिए json_serializable और json_annotation जोड़ें. कोड जनरेट करने की सुविधा के लिए, build_runner जोड़ें.
$ flutter pub add http json_annotation json_serializable dev:build_runner Resolving dependencies... Downloading packages... + _fe_analyzer_shared 67.0.0 (68.0.0 available) + analyzer 6.4.1 (6.5.0 available) + args 2.5.0 + build 2.4.1 + build_config 1.1.1 + build_daemon 4.0.1 + build_resolvers 2.4.2 + build_runner 2.4.9 + build_runner_core 7.3.0 + built_collection 5.1.1 + built_value 8.9.2 + checked_yaml 2.0.3 + code_builder 4.10.0 + convert 3.1.1 + crypto 3.0.3 + dart_style 2.3.6 + file 7.0.0 + fixnum 1.1.0 + frontend_server_client 4.0.0 + glob 2.1.2 + graphs 2.3.1 + http 1.2.1 + http_multi_server 3.2.1 + http_parser 4.0.2 + io 1.0.4 js 0.6.7 (0.7.1 available) + json_annotation 4.9.0 + json_serializable 6.8.0 leak_tracker 10.0.4 (10.0.5 available) leak_tracker_flutter_testing 3.0.3 (3.0.5 available) + logging 1.2.0 material_color_utilities 0.8.0 (0.11.1 available) meta 1.12.0 (1.14.0 available) + mime 1.0.5 + package_config 2.1.0 + pool 1.5.1 + pub_semver 2.1.4 + pubspec_parse 1.2.3 + shelf 1.4.1 + shelf_web_socket 1.0.4 + source_gen 1.5.0 + source_helper 1.3.4 test_api 0.7.0 (0.7.1 available) + timing 1.0.1 + typed_data 1.3.2 + watcher 1.1.0 + web_socket_channel 2.4.5 + yaml 3.1.2 Changed 42 dependencies! 8 packages have newer versions incompatible with dependency constraints. Try `flutter pub outdated` for more information.
कोड जनरेट करने की सुविधा के साथ JSON को पार्स करना
आपने शायद ध्यान दिया हो कि एपीआई एंडपॉइंट से मिले JSON डेटा का स्ट्रक्चर एक जैसा होता है. डेटा को ऐसे ऑब्जेक्ट में बदलने के लिए कोड जनरेट करना आसान होगा जिनका इस्तेमाल कोड में किया जा सकता है.
lib/src डायरेक्ट्री में, locations.dart फ़ाइल बनाएं और दिखाए गए JSON डेटा के स्ट्रक्चर के बारे में यहां दी गई जानकारी दें:
lib/src/locations.dart
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:http/http.dart' as http;
import 'package:json_annotation/json_annotation.dart';
part 'locations.g.dart';
@JsonSerializable()
class LatLng {
LatLng({
required this.lat,
required this.lng,
});
factory LatLng.fromJson(Map<String, dynamic> json) => _$LatLngFromJson(json);
Map<String, dynamic> toJson() => _$LatLngToJson(this);
final double lat;
final double lng;
}
@JsonSerializable()
class Region {
Region({
required this.coords,
required this.id,
required this.name,
required this.zoom,
});
factory Region.fromJson(Map<String, dynamic> json) => _$RegionFromJson(json);
Map<String, dynamic> toJson() => _$RegionToJson(this);
final LatLng coords;
final String id;
final String name;
final double zoom;
}
@JsonSerializable()
class Office {
Office({
required this.address,
required this.id,
required this.image,
required this.lat,
required this.lng,
required this.name,
required this.phone,
required this.region,
});
factory Office.fromJson(Map<String, dynamic> json) => _$OfficeFromJson(json);
Map<String, dynamic> toJson() => _$OfficeToJson(this);
final String address;
final String id;
final String image;
final double lat;
final double lng;
final String name;
final String phone;
final String region;
}
@JsonSerializable()
class Locations {
Locations({
required this.offices,
required this.regions,
});
factory Locations.fromJson(Map<String, dynamic> json) =>
_$LocationsFromJson(json);
Map<String, dynamic> toJson() => _$LocationsToJson(this);
final List<Office> offices;
final List<Region> regions;
}
Future<Locations> getGoogleOffices() async {
const googleLocationsURL = 'https://about.google/static/data/locations.json';
// Retrieve the locations of Google offices
try {
final response = await http.get(Uri.parse(googleLocationsURL));
if (response.statusCode == 200) {
return Locations.fromJson(
json.decode(response.body) as Map<String, dynamic>);
}
} catch (e) {
if (kDebugMode) {
print(e);
}
}
// Fallback for when the above HTTP request fails.
return Locations.fromJson(
json.decode(
await rootBundle.loadString('assets/locations.json'),
) as Map<String, dynamic>,
);
}
इस कोड को जोड़ने के बाद, आपके आईडीई (अगर इसका इस्तेमाल किया जा रहा है) में कुछ लाल रंग के स्क्विगल दिखने चाहिए. ऐसा इसलिए होता है, क्योंकि यह एक ऐसी सिबलिंग फ़ाइल को रेफ़रंस करता है जो मौजूद नहीं है, locations.g.dart. यह जनरेट की गई फ़ाइल, टाइप न किए गए JSON स्ट्रक्चर और नाम वाले ऑब्जेक्ट के बीच कनवर्ट करती है. इसे बनाने के लिए, build_runner को इस तरह चलाएं:
$ dart run build_runner build --delete-conflicting-outputs [INFO] Generating build script... [INFO] Generating build script completed, took 357ms [INFO] Creating build script snapshot...... [INFO] Creating build script snapshot... completed, took 10.5s [INFO] There was output on stdout while compiling the build script snapshot, run with `--verbose` to see it (you will need to run a `clean` first to re-snapshot). [INFO] Initializing inputs [INFO] Building new asset graph... [INFO] Building new asset graph completed, took 646ms [INFO] Checking for unexpected pre-existing outputs.... [INFO] Deleting 1 declared outputs which already existed on disk. [INFO] Checking for unexpected pre-existing outputs. completed, took 3ms [INFO] Running build... [INFO] Generating SDK summary... [INFO] 3.4s elapsed, 0/3 actions completed. [INFO] Generating SDK summary completed, took 3.4s [INFO] 4.7s elapsed, 2/3 actions completed. [INFO] Running build completed, took 4.7s [INFO] Caching finalized dependency graph... [INFO] Caching finalized dependency graph completed, took 36ms [INFO] Succeeded after 4.8s with 2 outputs (7 actions)
अब आपका कोड फिर से सही तरीके से विश्लेषण किया जा सकेगा. इसके बाद, हमें getGoogleOffices फ़ंक्शन में इस्तेमाल की गई फ़ॉलबैक locations.json फ़ाइल जोड़नी चाहिए. इस फ़ंक्शन में लोड किया जा रहा स्टैटिक डेटा, सीओआरएस हेडर के बिना दिखाया जाता है. इसलिए, यह वेब ब्राउज़र में लोड नहीं होगा. इस वजह से, इस फ़ंक्शन को फ़ॉलबैक के तौर पर शामिल किया गया है. Android और iOS के Flutter ऐप्लिकेशन को सीओआरएस हेडर की ज़रूरत नहीं होती. हालांकि, मोबाइल डेटा ऐक्सेस करने में कभी-कभी समस्या आ सकती है.
अपने ब्राउज़र में https://about.google/static/data/locations.json पर जाएं और कॉन्टेंट को ऐसेट डायरेक्ट्री में सेव करें. इसके अलावा, कमांड लाइन का इस्तेमाल इस तरह किया जा सकता है.
$ mkdir assets
$ cd assets
$ curl -o locations.json https://about.google/static/data/locations.json
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 30348 100 30348 0 0 75492 0 --:--:-- --:--:-- --:--:-- 75492
ऐसेट फ़ाइल डाउनलोड हो जाने के बाद, उसे अपनी pubspec.yaml फ़ाइल के फ़्लटर सेक्शन में जोड़ें.
pubspec.yaml
flutter:
uses-material-design: true
assets:
- assets/locations.json
मैप का डेटा पाने का अनुरोध करने के लिए, main.dart फ़ाइल में बदलाव करें. इसके बाद, मिली जानकारी का इस्तेमाल करके मैप में ऑफ़िस जोड़ें:
lib/main.dart
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'src/locations.dart' as locations;
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final Map<String, Marker> _markers = {};
Future<void> _onMapCreated(GoogleMapController controller) async {
final googleOffices = await locations.getGoogleOffices();
setState(() {
_markers.clear();
for (final office in googleOffices.offices) {
final marker = Marker(
markerId: MarkerId(office.name),
position: LatLng(office.lat, office.lng),
infoWindow: InfoWindow(
title: office.name,
snippet: office.address,
),
);
_markers[office.name] = marker;
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
useMaterial3: true,
colorSchemeSeed: Colors.green[700],
),
home: Scaffold(
appBar: AppBar(
title: const Text('Google Office Locations'),
elevation: 2,
),
body: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: const CameraPosition(
target: LatLng(0, 0),
zoom: 2,
),
markers: _markers.values.toSet(),
),
),
);
}
}
यह कोड कई कार्रवाइयां करता है:
_onMapCreatedमें, पिछले चरण के JSON पार्सिंग कोड का इस्तेमाल किया जाता है. यह तब तकawaitकरता है, जब तक कि यह लोड न हो जाए. इसके बाद, यह मिले हुए डेटा का इस्तेमाल करके,setState()कॉलबैक मेंMarkerबनाता है. ऐप्लिकेशन को नए मार्कर मिलने के बाद, setState फ़्लैग, Flutter को स्क्रीन फिर से रेंडर करने के लिए कहता है. इससे ऑफ़िस की जगहें दिखती हैं.- मार्कर,
Mapमें सेव किए जाते हैं. यहGoogleMapविजेट से जुड़ा होता है. इससे मार्कर, सही मैप से लिंक हो जाते हैं. आपके पास एक से ज़्यादा मैप बनाने और हर मैप में अलग-अलग मार्कर दिखाने का विकल्प होता है.

आपने जो काम पूरा किया है उसका स्क्रीनशॉट यहां दिया गया है. इस समय कई दिलचस्प बदलाव किए जा सकते हैं. उदाहरण के लिए, ऑफ़िस की एक सूची का व्यू जोड़ा जा सकता है. जब उपयोगकर्ता किसी ऑफ़िस पर क्लिक करता है, तो मैप मूव होता है और ज़ूम होता है. हालांकि, जैसा कि कहा गया है, यह काम पढ़ने वाले व्यक्ति के लिए छोड़ दिया गया है!
6. अगले चरण
बधाई हो!
आपने कोडलैब पूरा कर लिया है और Google मैप वाला Flutter ऐप्लिकेशन बना लिया है! आपने JSON वेब सर्विस के साथ भी इंटरैक्ट किया है.
अन्य अगले चरण
इस कोडलैब में, मैप पर कई पॉइंट दिखाने का तरीका बताया गया है. ऐसे कई मोबाइल ऐप्लिकेशन हैं जो इस सुविधा का इस्तेमाल करके, लोगों की अलग-अलग ज़रूरतों को पूरा करते हैं. यहां कुछ अन्य संसाधन दिए गए हैं जिनसे आपको इस बारे में ज़्यादा जानकारी मिल सकती है:
- Build Mobile Apps With Flutter and Google Maps (Cloud Next ‘19 में दिया गया एक भाषण)
- Hadrien Lejard का
google_maps_webserviceपैकेज, Google Maps Web Services को इस्तेमाल करना बहुत आसान बना देता है. जैसे, Directions API, Distance Matrix API, और Places API. - अगर आपको JSON REST के ज़रिए एपीआई इस्तेमाल करने के अलग-अलग विकल्प देखने हैं, तो JSON REST API के साथ काम करने के कई विकल्पों के लिए, ऐंड्रयू ब्रोगडन की Medium पोस्ट देखें.

