मैप में मार्कर जोड़ना

इस उदाहरण में, ऑस्ट्रेलिया के सिडनी और मेलबर्न शहरों के मार्कर के लिए पिन वाला मैप दिखाया गया है.

अपनी प्रोफ़ाइल बनाना शुरू करें

सैंपल कोड आज़माने से पहले, आपको अपना डेवलपमेंट एनवायरमेंट कॉन्फ़िगर करना होगा. ज़्यादा जानकारी के लिए, iOS के लिए Maps SDK के कोड सैंपल देखें.

कोड देखना

Swift

import GoogleMaps
import UIKit

// Sample code for adding a marker.
class MarkersViewController: UIViewController {
  private lazy var sydneyMarker = GMSMarker(
    position: CLLocationCoordinate2D(latitude: -33.8683, longitude: 151.2086))

  private lazy var melbourneMarker = GMSMarker(
    position: CLLocationCoordinate2D(latitude: -37.81969, longitude: 144.966085))

  private lazy var mapView: GMSMapView = {
    let camera = GMSCameraPosition(latitude: -37.81969, longitude: 144.966085, zoom: 4)
    return GMSMapView(frame: .zero, camera: camera)
  }()

  override func loadView() {
    view = mapView

    sydneyMarker.title = "Sydney"
    sydneyMarker.snippet = "Population: 4,605,992"
    sydneyMarker.isFlat = false
    sydneyMarker.rotation = 30
    print("sydneyMarker: \(sydneyMarker)")

    let australiaMarker = GMSMarker(
      position: CLLocationCoordinate2D(latitude: -27.994401, longitude: 140.07019))
    australiaMarker.title = "Australia"
    australiaMarker.appearAnimation = .pop
    australiaMarker.isFlat = true
    australiaMarker.isDraggable = true
    australiaMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
    australiaMarker.icon = UIImage(named: "australia")
    australiaMarker.map = mapView

    mapView.selectedMarker = sydneyMarker
    navigationItem.rightBarButtonItem = UIBarButtonItem(
      barButtonSystemItem: .add, target: self, action: #selector(tapAdd))
  }

  @objc func tapAdd() {
    if sydneyMarker.map == nil {
      sydneyMarker.map = mapView
    } else {
      sydneyMarker.map = nil
    }
    melbourneMarker.title = "Melbourne"
    melbourneMarker.snippet = "Population: 4,169,103"
    melbourneMarker.map = mapView
  }
}
      

Objective-C

#import "GoogleMapsDemos/Samples/MarkersViewController.h"

#import <GoogleMaps/GoogleMaps.h>

@implementation MarkersViewController {
  GMSMarker *_sydneyMarker;
  GMSMarker *_melbourneMarker;
  GMSMarker *_fadeInMarker;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-37.81969
                                                          longitude:144.966085
                                                               zoom:4];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

  _sydneyMarker = [[GMSMarker alloc] init];
  _sydneyMarker.title = @"Sydney";
  _sydneyMarker.snippet = @"Population: 4,605,992";
  _sydneyMarker.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
  _sydneyMarker.flat = NO;
  _sydneyMarker.rotation = 30.0;
  NSLog(@"sydneyMarker: %@", _sydneyMarker);

  GMSMarker *australiaMarker = [[GMSMarker alloc] init];
  australiaMarker.title = @"Australia";
  australiaMarker.position = CLLocationCoordinate2DMake(-27.994401, 140.07019);
  australiaMarker.appearAnimation = kGMSMarkerAnimationPop;
  australiaMarker.flat = YES;
  australiaMarker.draggable = YES;
  australiaMarker.groundAnchor = CGPointMake(0.5, 0.5);
  australiaMarker.icon = [UIImage imageNamed:@"australia"];
  australiaMarker.map = mapView;

  _fadeInMarker = [[GMSMarker alloc] init];
  _fadeInMarker.title = @"Australia";
  _fadeInMarker.position = CLLocationCoordinate2DMake(-29.9959, 145.0719);
  _fadeInMarker.appearAnimation = kGMSMarkerAnimationFadeIn;
  _fadeInMarker.icon = [UIImage imageNamed:@"australia"];

  // Set the marker in Sydney to be selected
  mapView.selectedMarker = _sydneyMarker;

  self.view = mapView;
  self.navigationItem.rightBarButtonItem =
      [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                    target:self
                                                    action:@selector(didTapAdd)];
}

- (void)didTapAdd {
  if (_sydneyMarker.map == nil) {
    _sydneyMarker.map = (GMSMapView *)self.view;
  } else {
    _sydneyMarker.map = nil;
  }

  _melbourneMarker.map = nil;
  _melbourneMarker = [[GMSMarker alloc] init];
  _melbourneMarker.title = @"Melbourne";
  _melbourneMarker.snippet = @"Population: 4,169,103";
  _melbourneMarker.position = CLLocationCoordinate2DMake(-37.81969, 144.966085);
  _melbourneMarker.map = (GMSMapView *)self.view;

  if (_fadeInMarker.map) {
    _fadeInMarker.map = nil;
  } else {
    _fadeInMarker.map = (GMSMapView *)self.view;
  }
}

@end
      

पूरे सैंपल ऐप्लिकेशन को स्थानीय तौर पर चलाएं

iOS के लिए Maps SDK का सैंपल ऐप्लिकेशन, GitHub से डाउनलोड किए जा सकने वाले संग्रह के तौर पर उपलब्ध है. Maps SDK for iOS के सैंपल ऐप्लिकेशन को इंस्टॉल करने और आज़माने के लिए, यह तरीका अपनाएं.

  1. सैंपल रिपॉज़िटरी को किसी लोकल डायरेक्ट्री में क्लोन करने के लिए, git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git चलाएं.
  2. टर्मिनल विंडो खोलें. इसके बाद, उस डायरेक्ट्री पर जाएं जहां आपने सैंपल फ़ाइलों को क्लोन किया है. इसके बाद, GoogleMaps डायरेक्ट्री में जाएं:

    Swift

    cd maps-sdk-for-ios-samples/GoogleMaps-Swift
    open GoogleMapsSwiftXCFrameworkDemos.xcodeproj

    Objective-C

    cd maps-sdk-for-ios-samples-main/GoogleMaps
    open GoogleMapsDemos.xcodeproj
  3. Xcode प्रोजेक्ट में, File > Add Package Dependencies पर जाएं. यूआरएल के तौर पर https://github.com/googlemaps/ios-maps-sdk डालें. इसके बाद, पैकेज को पुल करने के लिए Enter दबाएं और पैकेज जोड़ें पर क्लिक करें.
  4. Xcode में, कंपाइल बटन दबाकर मौजूदा स्कीम के साथ ऐप्लिकेशन बनाएं. बिल्ड में गड़बड़ी होती है. इसलिए, आपको Swift के लिए SDKConstants.swift फ़ाइल या Objective-C के लिए SDKDemoAPIKey.h फ़ाइल में अपनी एपीआई कुंजी डालनी होगी.
  5. Maps SDK for iOS की सुविधा चालू करके, अपने प्रोजेक्ट से एपीआई पासकोड पाएं.
  6. 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";
  7. 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.
  8. प्रोजेक्ट बनाएं और उसे चलाएं. iOS सिम्युलेटर विंडो दिखती है. इसमें Maps SDK के डेमो की सूची दिखती है.
  9. iOS के लिए Maps SDK टूल की किसी सुविधा को आज़माने के लिए, दिखाए गए विकल्पों में से कोई एक विकल्प चुनें.
  10. अगर GoogleMapsDemos को आपकी जगह की जानकारी ऐक्सेस करने की अनुमति देने के लिए कहा जाए, तो अनुमति दें को चुनें.