मार्कर के लिए जानकारी विंडो दिखाएं

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

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

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

कोड देखना

Swift

import GoogleMaps
import UIKit

final class MarkerInfoWindowViewController: UIViewController {

  private let sydneyMarker = GMSMarker(
    position: CLLocationCoordinate2D(latitude: -33.8683, longitude: 151.2086))

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

  private let brisbaneMarker = GMSMarker(
    position: CLLocationCoordinate2D(latitude: -27.4710107, longitude: 153.0234489))

  private lazy var contentView: UIImageView = {
    return UIImageView(image: UIImage(named: "aeroplane"))
  }()

  override func loadView() {
    let cameraPosition = GMSCameraPosition(latitude: -37.81969, longitude: 144.966085, zoom: 4)
    let mapView = GMSMapView(frame: .zero, camera: cameraPosition)
    mapView.delegate = self
    view = mapView

    sydneyMarker.title = "Sydney"
    sydneyMarker.snippet = "Population: 4,605,992"
    sydneyMarker.map = mapView

    melbourneMarker.title = "Melbourne"
    melbourneMarker.snippet = "Population: 4,169,103"
    melbourneMarker.map = mapView

    brisbaneMarker.title = "Brisbane"
    brisbaneMarker.snippet = "Population: 2,189,878"
    brisbaneMarker.map = mapView
  }
}

extension MarkerInfoWindowViewController: GMSMapViewDelegate {
  func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
    if marker == sydneyMarker {
      return contentView
    }
    return nil
  }

  func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {
    if marker == brisbaneMarker {
      return contentView
    }
    return nil
  }

  func mapView(_ mapView: GMSMapView, didCloseInfoWindowOf marker: GMSMarker) {
    showToast(message: "Info window for marker \(marker.title ?? "") closed.")
  }

  func mapView(_ mapView: GMSMapView, didLongPressInfoWindowOf marker: GMSMarker) {
    showToast(message: "Info window for marker \(marker.title ?? "") long pressed.")
  }
}

extension UIViewController {
  func showToast(message: String) {
    let toast = UIAlertController(title: nil, message: message, preferredStyle: .alert)
    present(
      toast, animated: true,
      completion: {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
          toast.dismiss(animated: true)
        }
      })
  }
}
      

Objective-C

#import "GoogleMapsDemos/Samples/MarkerInfoWindowViewController.h"

#import "GoogleMapsDemos/UIViewController+GMSToastMessages.h"
#import <GoogleMaps/GoogleMaps.h>

@interface MarkerInfoWindowViewController () <GMSMapViewDelegate>

@end

@implementation MarkerInfoWindowViewController {
  GMSMarker *_sydneyMarker;
  GMSMarker *_melbourneMarker;
  GMSMarker *_brisbaneMarker;
  UIView *_contentView;
}

- (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.map = mapView;
  NSLog(@"sydneyMarker: %@", _sydneyMarker);

  _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 = mapView;
  NSLog(@"melbourneMarker: %@", _melbourneMarker);

  _brisbaneMarker.map = nil;
  _brisbaneMarker = [[GMSMarker alloc] init];
  _brisbaneMarker.title = @"Brisbane";
  _brisbaneMarker.snippet = @"Population: 2,189,878";
  _brisbaneMarker.position = CLLocationCoordinate2DMake(-27.4710107, 153.0234489);
  _brisbaneMarker.map = mapView;
  NSLog(@"brisbaneMarker: %@", _brisbaneMarker);

  _contentView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"aeroplane"]];

  mapView.delegate = self;
  self.view = mapView;
}

#pragma mark GMSMapViewDelegate

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
  if (marker == _sydneyMarker) {
    return _contentView;
  }
  return nil;
}

- (UIView *)mapView:(GMSMapView *)mapView markerInfoContents:(GMSMarker *)marker {
  if (marker == _brisbaneMarker) {
    return _contentView;
  }
  return nil;
}

- (void)mapView:(GMSMapView *)mapView didCloseInfoWindowOfMarker:(GMSMarker *)marker {
  NSString *message =
      [NSString stringWithFormat:@"Info window for marker %@ closed.", marker.title];
  [self gms_showToastWithMessage:message];
}

- (void)mapView:(GMSMapView *)mapView didLongPressInfoWindowOfMarker:(GMSMarker *)marker {
  NSString *message =
      [NSString stringWithFormat:@"Info window for marker %@ long pressed.", marker.title];
  [self gms_showToastWithMessage:message];
}

@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 को आपकी जगह की जानकारी ऐक्सेस करने की अनुमति देने के लिए कहा जाए, तो अनुमति दें को चुनें.