رویدادهای نشانگر را مدیریت کنید

این مثال نقشه‌ای با دو نشانگر را نمایش می‌دهد. وقتی روی یک نشانگر ضربه زده می‌شود، دوربین زوم می‌کند و می‌چرخد تا روی نشانگر انتخاب شده فوکوس کند.

شروع کنید

قبل از اینکه بتوانید کد نمونه را امتحان کنید، باید محیط توسعه خود را پیکربندی کنید. برای اطلاعات بیشتر، برای نمونه کدهای iOS به Maps SDK مراجعه کنید.

کد را مشاهده کنید

سویفت

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
  }
}
      

هدف-سی

#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
      

اجرای کامل برنامه نمونه به صورت محلی

برنامه نمونه Maps SDK برای iOS به صورت آرشیو دانلود از GitHub در دسترس است. برای نصب و امتحان کردن برنامه نمونه Maps SDK برای iOS، این مراحل را دنبال کنید.

  1. git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git را اجرا کنید تا مخزن نمونه‌ها در یک دایرکتوری محلی کلون شود.
  2. یک پنجره ترمینال باز کنید، به دایرکتوری که فایل‌های نمونه را در آن کلون کرده‌اید بروید و به دایرکتوری GoogleMaps بروید:

    سویفت

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

    هدف-سی

    cd maps-sdk-for-ios-samples-main/GoogleMaps
    open GoogleMapsDemos.xcodeproj
  3. در پروژه Xcode، به File > Add Package Dependencies بروید. https://github.com/googlemaps/ios-maps-sdk به عنوان URL وارد کنید، Enter را بزنید تا بسته را دریافت کنید و روی Add Package کلیک کنید.
  4. در Xcode، دکمه کامپایل را فشار دهید تا برنامه با طرح فعلی ساخته شود . هنگام ساخت، خطایی ایجاد می‌شود که از شما می‌خواهد کلید API خود را در فایل SDKConstants.swift برای Swift یا فایل SDKDemoAPIKey.h برای Objective-C وارد کنید.
  5. یک کلید API از پروژه خود که Maps SDK برای iOS در آن فعال است، دریافت کنید.
  6. فایل SDKConstants.swift برای Swift یا فایل SDKDemoAPIKey.h را برای Objective-C ویرایش کنید و کلید API خود را در تعریف ثابت apiKey یا kAPIKey قرار دهید. برای مثال:

    سویفت

    static let apiKey = "YOUR_API_KEY"

    هدف-سی

    static NSString *const kAPIKey = @"YOUR_API_KEY";
  7. در فایل SDKConstants.swift (Swift) یا فایل SDKDemoAPIKey.h (Objective-C)، خط زیر را حذف کنید، زیرا برای ثبت مشکل تعریف‌شده توسط کاربر استفاده می‌شود:

    سویفت

    #error (Register for API Key and insert here. Then delete this line.)

    هدف-سی

    #error Register for API Key and insert here.
  8. پروژه را بسازید و اجرا کنید. پنجره شبیه‌ساز iOS ظاهر می‌شود و لیستی از نسخه‌های نمایشی Maps SDK را نشان می‌دهد.
  9. برای آزمایش یکی از ویژگی‌های Maps SDK برای iOS، یکی از گزینه‌های نمایش داده شده را انتخاب کنید.
  10. اگر از شما خواسته شد که به GoogleMapsDemos اجازه دسترسی به موقعیت مکانی خود را بدهید، Allow را انتخاب کنید.
،
این مثال نقشه‌ای با دو نشانگر را نمایش می‌دهد. وقتی روی یک نشانگر ضربه زده می‌شود، دوربین زوم می‌کند و می‌چرخد تا روی نشانگر انتخاب شده فوکوس کند.

شروع کنید

قبل از اینکه بتوانید کد نمونه را امتحان کنید، باید محیط توسعه خود را پیکربندی کنید. برای اطلاعات بیشتر، برای نمونه کدهای iOS به Maps SDK مراجعه کنید.

کد را مشاهده کنید

سویفت

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
  }
}
      

هدف-سی

#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
      

اجرای کامل برنامه نمونه به صورت محلی

برنامه نمونه Maps SDK برای iOS به صورت آرشیو دانلود از GitHub در دسترس است. برای نصب و امتحان کردن برنامه نمونه Maps SDK برای iOS، این مراحل را دنبال کنید.

  1. git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git را اجرا کنید تا مخزن نمونه‌ها در یک دایرکتوری محلی کلون شود.
  2. یک پنجره ترمینال باز کنید، به دایرکتوری که فایل‌های نمونه را در آن کلون کرده‌اید بروید و به دایرکتوری GoogleMaps بروید:

    سویفت

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

    هدف-سی

    cd maps-sdk-for-ios-samples-main/GoogleMaps
    open GoogleMapsDemos.xcodeproj
  3. در پروژه Xcode، به File > Add Package Dependencies بروید. https://github.com/googlemaps/ios-maps-sdk به عنوان URL وارد کنید، Enter را بزنید تا بسته را دریافت کنید و روی Add Package کلیک کنید.
  4. در Xcode، دکمه کامپایل را فشار دهید تا برنامه با طرح فعلی ساخته شود . هنگام ساخت، خطایی ایجاد می‌شود که از شما می‌خواهد کلید API خود را در فایل SDKConstants.swift برای Swift یا فایل SDKDemoAPIKey.h برای Objective-C وارد کنید.
  5. یک کلید API از پروژه خود که Maps SDK برای iOS در آن فعال است، دریافت کنید.
  6. فایل SDKConstants.swift برای Swift یا فایل SDKDemoAPIKey.h را برای Objective-C ویرایش کنید و کلید API خود را در تعریف ثابت apiKey یا kAPIKey قرار دهید. برای مثال:

    سویفت

    static let apiKey = "YOUR_API_KEY"

    هدف-سی

    static NSString *const kAPIKey = @"YOUR_API_KEY";
  7. در فایل SDKConstants.swift (Swift) یا فایل SDKDemoAPIKey.h (Objective-C)، خط زیر را حذف کنید، زیرا برای ثبت مشکل تعریف‌شده توسط کاربر استفاده می‌شود:

    سویفت

    #error (Register for API Key and insert here. Then delete this line.)

    هدف-سی

    #error Register for API Key and insert here.
  8. پروژه را بسازید و اجرا کنید. پنجره شبیه‌ساز iOS ظاهر می‌شود و لیستی از نسخه‌های نمایشی Maps SDK را نشان می‌دهد.
  9. برای آزمایش یکی از ویژگی‌های Maps SDK برای iOS، یکی از گزینه‌های نمایش داده شده را انتخاب کنید.
  10. اگر از شما خواسته شد که به GoogleMapsDemos اجازه دسترسی به موقعیت مکانی خود را بدهید، Allow را انتخاب کنید.
،
این مثال نقشه‌ای با دو نشانگر را نمایش می‌دهد. وقتی روی یک نشانگر ضربه زده می‌شود، دوربین زوم می‌کند و می‌چرخد تا روی نشانگر انتخاب شده فوکوس کند.

شروع کنید

قبل از اینکه بتوانید کد نمونه را امتحان کنید، باید محیط توسعه خود را پیکربندی کنید. برای اطلاعات بیشتر، برای نمونه کدهای iOS به Maps SDK مراجعه کنید.

کد را مشاهده کنید

سویفت

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
  }
}
      

هدف-سی

#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
      

اجرای کامل برنامه نمونه به صورت محلی

برنامه نمونه Maps SDK برای iOS به صورت آرشیو دانلود از GitHub در دسترس است. برای نصب و امتحان کردن برنامه نمونه Maps SDK برای iOS، این مراحل را دنبال کنید.

  1. git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git را اجرا کنید تا مخزن نمونه‌ها در یک دایرکتوری محلی کلون شود.
  2. یک پنجره ترمینال باز کنید، به دایرکتوری که فایل‌های نمونه را در آن کلون کرده‌اید بروید و به دایرکتوری GoogleMaps بروید:

    سویفت

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

    هدف-سی

    cd maps-sdk-for-ios-samples-main/GoogleMaps
    open GoogleMapsDemos.xcodeproj
  3. در پروژه Xcode، به File > Add Package Dependencies بروید. https://github.com/googlemaps/ios-maps-sdk به عنوان URL وارد کنید، Enter را بزنید تا بسته را دریافت کنید و روی Add Package کلیک کنید.
  4. در Xcode، دکمه کامپایل را فشار دهید تا برنامه با طرح فعلی ساخته شود . هنگام ساخت، خطایی ایجاد می‌شود که از شما می‌خواهد کلید API خود را در فایل SDKConstants.swift برای Swift یا فایل SDKDemoAPIKey.h برای Objective-C وارد کنید.
  5. یک کلید API از پروژه خود که Maps SDK برای iOS در آن فعال است، دریافت کنید.
  6. فایل SDKConstants.swift برای Swift یا فایل SDKDemoAPIKey.h را برای Objective-C ویرایش کنید و کلید API خود را در تعریف ثابت apiKey یا kAPIKey قرار دهید. برای مثال:

    سویفت

    static let apiKey = "YOUR_API_KEY"

    هدف-سی

    static NSString *const kAPIKey = @"YOUR_API_KEY";
  7. در فایل SDKConstants.swift (Swift) یا فایل SDKDemoAPIKey.h (Objective-C)، خط زیر را حذف کنید، زیرا برای ثبت مشکل تعریف‌شده توسط کاربر استفاده می‌شود:

    سویفت

    #error (Register for API Key and insert here. Then delete this line.)

    هدف-سی

    #error Register for API Key and insert here.
  8. پروژه را بسازید و اجرا کنید. پنجره شبیه‌ساز iOS ظاهر می‌شود و لیستی از نسخه‌های نمایشی Maps SDK را نشان می‌دهد.
  9. برای آزمایش یکی از ویژگی‌های Maps SDK برای iOS، یکی از گزینه‌های نمایش داده شده را انتخاب کنید.
  10. اگر از شما خواسته شد که به GoogleMapsDemos اجازه دسترسی به موقعیت مکانی خود را بدهید، Allow را انتخاب کنید.
،
این مثال نقشه‌ای با دو نشانگر را نمایش می‌دهد. وقتی روی یک نشانگر ضربه زده می‌شود، دوربین زوم می‌کند و می‌چرخد تا روی نشانگر انتخاب شده فوکوس کند.

شروع کنید

قبل از اینکه بتوانید کد نمونه را امتحان کنید، باید محیط توسعه خود را پیکربندی کنید. برای اطلاعات بیشتر، برای نمونه کدهای iOS به Maps SDK مراجعه کنید.

کد را مشاهده کنید

سویفت

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
  }
}
      

هدف-سی

#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
      

اجرای کامل برنامه نمونه به صورت محلی

برنامه نمونه Maps SDK برای iOS به صورت آرشیو دانلود از GitHub در دسترس است. برای نصب و امتحان کردن برنامه نمونه Maps SDK برای iOS، این مراحل را دنبال کنید.

  1. git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git را اجرا کنید تا مخزن نمونه‌ها در یک دایرکتوری محلی کلون شود.
  2. یک پنجره ترمینال باز کنید، به دایرکتوری که فایل‌های نمونه را در آن کلون کرده‌اید بروید و به دایرکتوری GoogleMaps بروید:

    سویفت

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

    هدف-سی

    cd maps-sdk-for-ios-samples-main/GoogleMaps
    open GoogleMapsDemos.xcodeproj
  3. در پروژه Xcode، به File > Add Package Dependencies بروید. https://github.com/googlemaps/ios-maps-sdk به عنوان URL وارد کنید، Enter را بزنید تا بسته را دریافت کنید و روی Add Package کلیک کنید.
  4. در Xcode، دکمه کامپایل را فشار دهید تا برنامه با طرح فعلی ساخته شود . هنگام ساخت، خطایی ایجاد می‌شود که از شما می‌خواهد کلید API خود را در فایل SDKConstants.swift برای Swift یا فایل SDKDemoAPIKey.h برای Objective-C وارد کنید.
  5. یک کلید API از پروژه خود که Maps SDK برای iOS در آن فعال است، دریافت کنید.
  6. فایل SDKConstants.swift برای Swift یا فایل SDKDemoAPIKey.h را برای Objective-C ویرایش کنید و کلید API خود را در تعریف ثابت apiKey یا kAPIKey قرار دهید. برای مثال:

    سویفت

    static let apiKey = "YOUR_API_KEY"

    هدف-سی

    static NSString *const kAPIKey = @"YOUR_API_KEY";
  7. در فایل SDKConstants.swift (Swift) یا فایل SDKDemoAPIKey.h (Objective-C)، خط زیر را حذف کنید، زیرا برای ثبت مشکل تعریف‌شده توسط کاربر استفاده می‌شود:

    سویفت

    #error (Register for API Key and insert here. Then delete this line.)

    هدف-سی

    #error Register for API Key and insert here.
  8. پروژه را بسازید و اجرا کنید. پنجره شبیه‌ساز iOS ظاهر می‌شود و لیستی از نسخه‌های نمایشی Maps SDK را نشان می‌دهد.
  9. برای آزمایش یکی از ویژگی‌های Maps SDK برای iOS، یکی از گزینه‌های نمایش داده شده را انتخاب کنید.
  10. اگر از شما خواسته شد که به GoogleMapsDemos اجازه دسترسی به موقعیت مکانی خود را بدهید، Allow را انتخاب کنید.