הצגת מפה בסיסית

מפה בסיסית שמתמקדת בסיאטל, וושינגטון.

בדוגמה הזו נוצרת מפה שמתמקדת בסיאטל, וושינגטון.

שנתחיל?

לפני שמנסים את הקוד לדוגמה, צריך להגדיר את סביבת הפיתוח. מידע נוסף זמין במאמר דוגמאות קוד של SDK של מפות ל-iOS.

צפייה בקוד

Swift

import GoogleMaps
import UIKit

class BasicMapViewController: UIViewController {
  var statusLabel: UILabel!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Seattle coordinates
    let camera = GMSCameraPosition(latitude: 47.6089945, longitude: -122.3410462, zoom: 14)
    let mapView = GMSMapView(frame: view.bounds, camera: camera)
    mapView.delegate = self
    view = mapView
    navigationController?.navigationBar.isTranslucent = false

    statusLabel = UILabel(frame: .zero)
    statusLabel.alpha = 0.0
    statusLabel.backgroundColor = .blue
    statusLabel.textColor = .white
    statusLabel.textAlignment = .center
    view.addSubview(statusLabel)
    statusLabel.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      statusLabel.topAnchor.constraint(equalTo: view.topAnchor),
      statusLabel.heightAnchor.constraint(equalToConstant: 30),
      statusLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor),
      statusLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    ])
  }
}

extension BasicMapViewController: GMSMapViewDelegate {
  func mapViewDidStartTileRendering(_ mapView: GMSMapView) {
    statusLabel.alpha = 0.8
    statusLabel.text = "Rendering"
  }

  func mapViewDidFinishTileRendering(_ mapView: GMSMapView) {
    statusLabel.alpha = 0.0
  }
}
      

Objective-C

#import "GoogleMapsDemos/Samples/BasicMapViewController.h"

#import <GoogleMaps/GoogleMaps.h>

@implementation BasicMapViewController {
  UILabel *_statusLabel;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  // Seattle coordinates
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.6089945
                                                          longitude:-122.3410462
                                                               zoom:14];
  GMSMapView *view = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  view.delegate = self;
  self.view = view;

  // Add status label, initially hidden.
  _statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
  _statusLabel.alpha = 0.0f;
  _statusLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  _statusLabel.backgroundColor = [UIColor blueColor];
  _statusLabel.textColor = [UIColor whiteColor];
  _statusLabel.textAlignment = NSTextAlignmentCenter;

  [view addSubview:_statusLabel];
}

- (void)mapViewDidStartTileRendering:(GMSMapView *)mapView {
  _statusLabel.alpha = 0.8f;
  _statusLabel.text = @"Rendering";
}

- (void)mapViewDidFinishTileRendering:(GMSMapView *)mapView {
  _statusLabel.alpha = 0.0f;
}

@end
      

הרצת האפליקציה המלאה לדוגמה באופן מקומי

האפליקציה לדוגמה של SDK של מפות ל-iOS זמינה כארכיון להורדה מ-GitHub. כדי להתקין את האפליקציה לדוגמה של SDK של מפות ל-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 (הוספת יחסי תלות בין חבילות). מזינים את כתובת ה-URL‏ https://github.com/googlemaps/ios-maps-sdk, מקישים על Enter כדי למשוך את החבילה ולוחצים על Add Package.
  4. ב-Xcode, לוחצים על כפתור הקימפול כדי לבנות את האפליקציה עם הסכימה הנוכחית. ה-build יוצר שגיאה, ומבקש מכם להזין את מפתח ה-API בקובץ SDKConstants.swift ל-Swift או בקובץ SDKDemoAPIKey.h ל-Objective-C.
  5. מקבלים מפתח API מהפרויקט שבו מופעל ה-SDK של מפות ל-iOS.
  6. עורכים את הקובץ SDKConstants.swift ל-Swift או את הקובץ SDKDemoAPIKey.h ל-Objective-C ומדביקים את מפתח ה-API בהגדרה של הקבוע 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. בוחרים אחת מהאפשרויות שמוצגות כדי להתנסות בתכונה של SDK של מפות ל-iOS.
  10. אם מוצגת בקשה לתת ל-GoogleMapsDemos הרשאת גישה למיקום, בוחרים באפשרות אישור.