अपनी प्रोफ़ाइल बनाना शुरू करें
सैंपल कोड आज़माने से पहले, आपको अपना डेवलपमेंट एनवायरमेंट कॉन्फ़िगर करना होगा. ज़्यादा जानकारी के लिए, iOS के लिए Maps SDK के कोड सैंपल देखें.
कोड देखना
Swift
import GoogleMaps import UIKit final class MarkerEventsViewController: UIViewController { private lazy var mapView: GMSMapView = { let camera = GMSCameraPosition(latitude: -37.81969, longitude: 144.966085, zoom: 4) return GMSMapView(frame: .zero, camera: camera) }() private var melbourneMarker = GMSMarker( position: CLLocationCoordinate2D(latitude: -37.81969, longitude: 144.966085)) override func loadView() { let sydneyMarker = GMSMarker( position: CLLocationCoordinate2D(latitude: -33.8683, longitude: 151.2086)) sydneyMarker.map = mapView melbourneMarker.map = mapView mapView.delegate = self view = mapView } } extension MarkerEventsViewController: GMSMapViewDelegate { func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? { if marker == melbourneMarker { return UIImageView(image: UIImage(named: "Icon")) } return nil } func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { // Animate to the marker CATransaction.begin() CATransaction.setAnimationDuration(3) let camera = GMSCameraPosition(target: marker.position, zoom: 8, bearing: 50, viewingAngle: 60) mapView.animate(to: camera) CATransaction.commit() // Melbourne marker has a InfoWindow so return false to allow markerInfoWindow to // fire. Also check that the marker isn't already selected so that the InfoWindow // doesn't close. if marker == melbourneMarker && mapView.selectedMarker != melbourneMarker { return false } return true } }
Objective-C
#import "GoogleMapsDemos/Samples/MarkerEventsViewController.h" #import <QuartzCore/QuartzCore.h> #import <GoogleMaps/GoogleMaps.h> @implementation MarkerEventsViewController { GMSMapView *_mapView; GMSMarker *_melbourneMarker; BOOL _rotating; } - (void)viewDidLoad { [super viewDidLoad]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-37.81969 longitude:144.966085 zoom:4]; _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; GMSMarker *sydneyMarker = [[GMSMarker alloc] init]; sydneyMarker.position = CLLocationCoordinate2DMake(-33.8683, 151.2086); sydneyMarker.map = _mapView; _melbourneMarker = [[GMSMarker alloc] init]; _melbourneMarker.position = CLLocationCoordinate2DMake(-37.81969, 144.966085); _melbourneMarker.map = _mapView; _mapView.delegate = self; self.view = _mapView; } #pragma mark - GMSMapViewDelegate - (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker { if (marker == _melbourneMarker) { return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Icon"]]; } return nil; } - (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker { GMSCameraPosition *camera = [[GMSCameraPosition alloc] initWithTarget:marker.position zoom:8 bearing:50 viewingAngle:60]; // Animate to the marker [CATransaction begin]; [CATransaction setAnimationDuration:3.f]; // 3 second animation [CATransaction setCompletionBlock:^{ if (_rotating) { // Animation was interrupted by orientation change. [CATransaction setDisableActions:true]; // Disable animation to avoid interruption from rotation. [_mapView animateToCameraPosition:camera]; } }]; [mapView animateToCameraPosition:camera]; [CATransaction commit]; // Melbourne marker has a InfoWindow so return NO to allow markerInfoWindow to fire. Also check // that the marker isn't already selected so that the InfoWindow doesn't close. if (marker == _melbourneMarker && mapView.selectedMarker != _melbourneMarker) { return NO; } // The Tap has been handled so return YES return YES; } - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { _rotating = true; [coordinator animateAlongsideTransition:nil completion:^( id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { _rotating = false; }]; [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; } @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 को आपकी जगह की जानकारी ऐक्सेस करने की अनुमति देने के लिए कहा जाए, तो अनुमति दें को चुनें.