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

תמונה של הצגת מפה בסיסית.

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

שנתחיל?

לפני שתוכלו לנסות את הקוד לדוגמה, עליכם להגדיר את סביבת הפיתוח. מידע נוסף זמין במאמר דוגמאות קוד ל-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. כך מתקינים את האפליקציה לדוגמה של Maps SDK ל-iOS ומנסים אותה.

  1. מריצים את הפקודה git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git כדי להעתיק את המאגר לדוגמה לספרייה מקומית.
  2. פותחים חלון טרמינל, עוברים לספרייה שבה קלונתם את קובצי הדוגמה ומתעדים את ספריית Google Maps:

    Swift

    cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift pod install open GoogleMapsSwiftDemos.xcworkspace

    Objective-C

    cd maps-sdk-for-ios-samples-main/GoogleMaps pod install open GoogleMapsDemos.xcworkspace
  3. ב-Xcode, לוחצים על לחצן הידור כדי ליצור את האפליקציה לפי התוכנית הנוכחית. תופיע הודעת שגיאה ב-build, ותתבקשו להזין את מפתח ה-API בקובץ SDKConstants.swift ל-Swift או בקובץ SDKDemoAPIKey.h ל-Objective-C.
  4. מקבלים מפתח API מהפרויקט עם ה-SDK של מפות Google ל-iOS מופעל.
  5. עורכים את הקובץ 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";
  6. בקובץ 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.
  7. פיתוח והרצה של הפרויקט. חלון הסימולטור של iOS מופיע עם רשימה של דמואים של SDK של מפות.
  8. בוחרים באחת מהאפשרויות שמוצגות כדי להתנסות בתכונה של SDK של מפות ל-iOS.
  9. אם מופיעה בקשה לתת ל-GoogleMapsDemos גישה למיקום שלכם, בוחרים באפשרות אישור.