अपनी प्रोफ़ाइल बनाना शुरू करें
सैंपल कोड आज़माने से पहले, आपको अपना डेवलपमेंट एनवायरमेंट कॉन्फ़िगर करना होगा. ज़्यादा जानकारी के लिए, iOS के लिए Maps SDK के कोड सैंपल देखें.
कोड देखना
Swift
import GoogleMaps import UIKit class GestureControlViewController: UIViewController { private let holderHeight: CGFloat = 60 private let zoomLabelInset: CGFloat = 16 private lazy var mapView: GMSMapView = { let camera = GMSCameraPosition(latitude: -25.5605, longitude: 133.605097, zoom: 3) return GMSMapView(frame: .zero, camera: camera) }() private lazy var zoomSwitch: UISwitch = UISwitch(frame: .zero) override func viewDidLoad() { super.viewDidLoad() view.addSubview(mapView) let holder = UIView(frame: .zero) holder.backgroundColor = UIColor(white: 1, alpha: 0.8) view.addSubview(holder) let zoomLabel = UILabel(frame: .zero) zoomLabel.text = "Zoom gestures" zoomLabel.font = .boldSystemFont(ofSize: 18) holder.addSubview(zoomLabel) // Control zooming. holder.addSubview(zoomSwitch) zoomSwitch.addTarget(self, action: #selector(toggleZoom), for: .valueChanged) zoomSwitch.isOn = true [mapView, holder, zoomLabel, zoomSwitch].forEach({ $0.translatesAutoresizingMaskIntoConstraints = false }) NSLayoutConstraint.activate([ mapView.leftAnchor.constraint(equalTo: view.leftAnchor), mapView.rightAnchor.constraint(equalTo: view.rightAnchor), mapView.topAnchor.constraint(equalTo: view.topAnchor), mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor), holder.heightAnchor.constraint(equalToConstant: holderHeight), holder.centerXAnchor.constraint(equalTo: view.centerXAnchor), holder.widthAnchor.constraint(equalTo: view.widthAnchor), zoomLabel.leftAnchor.constraint(equalTo: holder.leftAnchor, constant: zoomLabelInset), zoomLabel.centerYAnchor.constraint(equalTo: holder.centerYAnchor), zoomSwitch.rightAnchor.constraint(equalTo: holder.rightAnchor, constant: -zoomLabelInset), zoomSwitch.centerYAnchor.constraint( equalTo: holder.centerYAnchor), ]) NSLayoutConstraint.activate([ holder.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor) ]) } @objc func toggleZoom() { mapView.settings.zoomGestures = zoomSwitch.isOn } }
Objective-C
#import "GoogleMapsDemos/Samples/GestureControlViewController.h" #import <GoogleMaps/GoogleMaps.h> @implementation GestureControlViewController { GMSMapView *_mapView; UISwitch *_zoomSwitch; } - (void)viewDidLoad { [super viewDidLoad]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-25.5605 longitude:133.605097 zoom:3]; _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; _mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _mapView.accessibilityIdentifier = @"gestureControlDemoMapView"; self.view = [[UIView alloc] initWithFrame:CGRectZero]; [self.view addSubview:_mapView]; UIView *holder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 59)]; holder.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; holder.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8f]; [self.view addSubview:holder]; // Zoom label. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, 200, 29)]; label.text = @"Zooming?"; label.font = [UIFont boldSystemFontOfSize:18.0f]; label.textAlignment = NSTextAlignmentLeft; label.backgroundColor = [UIColor clearColor]; label.layer.shadowColor = [[UIColor whiteColor] CGColor]; label.layer.shadowOffset = CGSizeMake(0.0f, 1.0f); label.layer.shadowOpacity = 1.0f; label.layer.shadowRadius = 0.0f; [holder addSubview:label]; // Control zooming. _zoomSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(-90, 16, 0, 0)]; _zoomSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; [_zoomSwitch addTarget:self action:@selector(didChangeZoomSwitch) forControlEvents:UIControlEventValueChanged]; _zoomSwitch.on = YES; [holder addSubview:_zoomSwitch]; } - (void)didChangeZoomSwitch { _mapView.settings.zoomGestures = _zoomSwitch.isOn; } @end
पूरे सैंपल ऐप्लिकेशन को स्थानीय तौर पर चलाएं
iOS के लिए Maps SDK का सैंपल ऐप्लिकेशन, GitHub से डाउनलोड किए जा सकने वाले संग्रह के तौर पर उपलब्ध है. Maps SDK for iOS के सैंपल ऐप्लिकेशन को इंस्टॉल करने और आज़माने के लिए, यह तरीका अपनाएं.
- सैंपल रिपॉज़िटरी को किसी लोकल डायरेक्ट्री में क्लोन करने के लिए,
git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.gitचलाएं. टर्मिनल विंडो खोलें. इसके बाद, उस डायरेक्ट्री पर जाएं जहां आपने सैंपल फ़ाइलों को क्लोन किया है. इसके बाद, GoogleMaps डायरेक्ट्री में जाएं:
Swift
cd maps-sdk-for-ios-samples/GoogleMaps-Swift
open GoogleMapsSwiftXCFrameworkDemos.xcodeprojObjective-C
cd maps-sdk-for-ios-samples-main/GoogleMaps
open GoogleMapsDemos.xcodeproj- Xcode प्रोजेक्ट में, File > Add Package Dependencies पर जाएं.
यूआरएल के तौर पर
https://github.com/googlemaps/ios-maps-sdkडालें. इसके बाद, पैकेज को पुल करने के लिए Enter दबाएं और पैकेज जोड़ें पर क्लिक करें. - Xcode में, कंपाइल बटन दबाकर मौजूदा स्कीम के साथ ऐप्लिकेशन बनाएं. बिल्ड में गड़बड़ी होती है. इसलिए, आपको Swift के लिए
SDKConstants.swiftफ़ाइल या Objective-C के लिएSDKDemoAPIKey.hफ़ाइल में अपनी एपीआई कुंजी डालनी होगी. - Maps SDK for iOS की सुविधा चालू करके, अपने प्रोजेक्ट से एपीआई पासकोड पाएं.
- Swift के लिए
SDKConstants.swiftफ़ाइल या Objective-C के लिएSDKDemoAPIKey.hफ़ाइल में बदलाव करें. इसके बाद, अपनी एपीआई कुंजी कोapiKeyयाkAPIKeyकॉन्स्टेंट की परिभाषा में चिपकाएं. उदाहरण के लिए:Swift
static let apiKey = "YOUR_API_KEY"Objective-C
static NSString *const kAPIKey = @"YOUR_API_KEY";
SDKConstants.swiftफ़ाइल (Swift) याSDKDemoAPIKey.hफ़ाइल (Objective-C) में, यहां दी गई लाइन हटाएं. इसका इस्तेमाल, उपयोगकर्ता की तय की गई समस्या को रजिस्टर करने के लिए किया जाता है:Swift
#error (Register for API Key and insert here. Then delete this line.)
Objective-C
#error Register for API Key and insert here.
- प्रोजेक्ट बनाएं और उसे चलाएं. iOS सिम्युलेटर विंडो दिखती है. इसमें Maps SDK के डेमो की सूची दिखती है.
- iOS के लिए Maps SDK टूल की किसी सुविधा को आज़माने के लिए, दिखाए गए विकल्पों में से कोई एक विकल्प चुनें.
- अगर GoogleMapsDemos को आपकी जगह की जानकारी ऐक्सेस करने की अनुमति देने के लिए कहा जाए, तो अनुमति दें को चुनें.