הוספת מפה עם סמן

המדריך הזה מראה איך להוסיף מפת Google פשוטה עם סמן לאפליקציה ל-iOS. הוא מתאים לאנשים עם ידע מתחיל או בינוני ב-Swift או ב-Objective-C וגם לידע כללי ב-Xcode. למדריך מתקדם ליצירת מפות, קראו את המדריך למפתחים.

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

צילום מסך שבו מוצגת מפה עם סמן מעל סידני

קבל את הקוד

משכפלים או מורידים את מאגר הדוגמאות של מפות Google ל-iOS ב-GitHub.

לחלופין, לוחצים על הלחצן הבא כדי להוריד את קוד המקור:

אני רוצה לקבל את הקוד

Swift

/*
 * Copyright 2020 Google Inc. All rights reserved.
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import UIKit
import GoogleMaps

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: self.view.frame, camera: camera)
        self.view.addSubview(mapView)

        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
  }
}

      

Objective-C

/*
* Copyright 2020 Google Inc. All rights reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

#import "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  // Do any additional setup after loading the view.
  // Create a GMSCameraPosition that tells the map to display the
  // coordinate -33.86,151.20 at zoom level 6.
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                          longitude:151.20
                                                               zoom:6];
  GMSMapView *mapView = [GMSMapView mapWithFrame:self.view.frame camera:camera];
  mapView.myLocationEnabled = YES;
  [self.view addSubview:mapView];

  // Creates a marker in the center of the map.
  GMSMarker *marker = [[GMSMarker alloc] init];
  marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
  marker.title = @"Sydney";
  marker.snippet = @"Australia";
  marker.map = mapView;
}

@end

      

שנתחיל?

מנהל חבילות SWIFT

ניתן להתקין את ה-SDK של מפות Google ל-iOS באמצעות Swift Package Manager.

  1. יש לוודא שהסרתם את כל יחסי התלות הקיימים של Maps SDK עבור iOS.
  2. פותחים חלון טרמינל ומנווטים לספרייה tutorials/map-with-marker.
  3. צריך לוודא שסביבת העבודה של Xcode סגורה ומריצים את הפקודות הבאות:
    sudo gem install cocoapods-deintegrate cocoapods-clean
    pod deintegrate
    pod cache clean --all
    rm Podfile
    rm map-with-marker.xcworkspace
  4. פותחים את פרויקט ה-Xcode ומוחקים את קובץ ה-pod.
  5. נכנסים אל קובץ > הוספת יחסי תלות של חבילה.
  6. מזינים את הכתובת https://github.com/googlemaps/ios-maps-sdk בתור כתובת ה-URL, מקישים על Enter כדי למשוך את החבילה ולוחצים על הוספת חבילה.
  7. ייתכן שתצטרכו לאפס את מטמון החבילה באמצעות File > Packages > איפוס Package Cache.

שימוש ב-CocoaPods

  1. מורידים ומתקינים את Xcode גרסה 15.0 ואילך.
  2. אם עדיין אין לכם את אפליקציית CocoaPods, תוכלו להתקין אותו ב-macOS על ידי הרצת הפקודה הבאה מהטרמינל:
    sudo gem install cocoapods
  3. עוברים לספרייה tutorials/map-with-marker.
  4. מריצים את הפקודה pod install. הפעולה הזו תתקין את ה-SDK של מפות Google שצוין ב-Podfile, יחד עם כל יחסי התלות.
  5. מריצים את הפקודה pod outdated כדי להשוות בין גרסת ה-Pod מותקנת לבין עדכונים חדשים. אם זוהתה גרסה חדשה, צריך להריץ את הפקודה pod update כדי לעדכן את Podfile ולהתקין את הגרסה העדכנית ביותר של ה-SDK. לפרטים נוספים, אפשר לעיין במדריך של CocoaPods.
  6. פותחים (לחיצה כפולה) את הקובץ map-with-marker.xcworkspace של הפרויקט כדי לפתוח אותו ב-Xcode. כדי לפתוח את הפרויקט, צריך להשתמש בקובץ .xcworkspace.

קבלת מפתח API והפעלת ממשקי ה-API הנדרשים

כדי להשלים את המדריך הזה, תצטרכו מפתח Google API שמורשה להשתמש ב-SDK של מפות Google ל-iOS. כדי לקבל מפתח ולהפעיל את ה-API, לוחצים על הלחצן הבא.

שנתחיל?

מידע נוסף זמין במאמר קבלת מפתח API.

הוספה של מפתח ה-API לאפליקציה

מוסיפים את מפתח ה-API ל-AppDelegate.swift באופן הבא:

  1. שימו לב שהצהרת הייבוא הבאה נוספה לקובץ:
    import GoogleMaps
  2. עורכים את השורה הבאה ב-method application(_:didFinishLaunchingWithOptions:) ומחליפים את YOUR_API_KEY במפתח ה-API:
    GMSServices.provideAPIKey("YOUR_API_KEY")

יצירה והפעלה של אפליקציה

  1. מחברים מכשיר iOS למחשב או בוחרים סימולטור מתפריט סכמת Xcode.
  2. אם אתם משתמשים במכשיר, מוודאים ששירותי המיקום מופעלים. אם משתמשים בסימולטור, בוחרים מיקום מתפריט תכונות.
  3. ב-Xcode, לוחצים על אפשרות התפריט Product/Run (או על סמל לחצן ההפעלה).
    • Xcode יוצר את האפליקציה ולאחר מכן מפעיל אותה במכשיר או בסימולטור.
    • אתם אמורים לראות מפה עם סמן במרכז סידני בחוף המזרחי של אוסטרליה, בדומה לתמונה שבדף הזה.

פתרון בעיות:

  • אם המפה לא מופיעה, צריך לוודא שקיבלת מפתח API והוספת אותו לאפליקציה, כפי שמתואר למעלה. במסוף לניפוי באגים של Xcode יש הודעות שגיאה לגבי מפתח ה-API.
  • אם הגבלת את מפתח ה-API על ידי מזהה החבילה של iOS, צריך לערוך את המפתח כדי להוסיף את מזהה החבילה של האפליקציה: com.google.examples.map-with-marker.
  • מוודאים שיש לכם חיבור טוב ל-Wi-Fi או ל-GPS.
  • אפשר להשתמש בכלים לניפוי באגים של Xcode כדי לצפות ביומנים ולנפות באגים באפליקציה.

הסבר על הקוד

  1. אפשר ליצור מפה ולהגדיר אותה כתצוגה ב-viewDidLoad().

    Swift

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView
          

    Objective-C

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6.0];
    GMSMapView *mapView = [[GMSMapView alloc] initWithFrame: CGRectZero camera:camera];
    self.view = mapView;
          
  2. הוספת סמן למפה בviewDidLoad().

    Swift

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView
          

    Objective-C

    // Creates a marker in the center of the map.
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.title = @"Sydney";
    marker.snippet = @"Australia";
    marker.map = mapView;
          

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

מזל טוב! יצרתם אפליקציה ל-iOS שמציגה את מפת Google עם סמן כדי לציין מיקום מסוים. למדתם גם איך להשתמש ב- SDK של מפות ל-iOS.

השלבים הבאים

מידע נוסף על אובייקט המפה ועל הפעולות שאפשר לבצע באמצעות סמנים.