إضافة خريطة ذات محدّد موقع

يشرح هذا الدليل التوجيهي كيفية إضافة خريطة Google بسيطة مع محدد موقع إلى جهاز iOS. التطبيق. وهي تناسب الأشخاص الذين لديهم معرفة مبتدئ أو متوسط بـ Swift أو الهدف-ج إلى جانب المعرفة العامة بـ Xcode. للحصول على دليل متقدم وإنشاء الخرائط وقراءة تقارير المطورين الدليل.

ستنشئ الخريطة التالية باستخدام هذا البرنامج التعليمي. يتم وضع العلامة في سيدني، أستراليا.

لقطة شاشة تعرض خريطة عليها علامة فوق سيدني

الحصول على الرمز‏

استنساخ أو نزّل ملف مستودع نماذج iOS لتطبيق "خرائط Google" على 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

يمكن تثبيت حزمة تطوير البرامج بالاستناد إلى بيانات "خرائط Google" لنظام التشغيل iOS باستخدام Swift Package Manager.

  1. تأكّد من إزالة أي حزمة تطوير برامج (SDK) حالية لخرائط Google لتبعيات 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 واحذف ملف podfile.
  5. انتقِل إلى ملف > أضِف تبعيات الحزمة.
  6. أدخِل https://github.com/googlemaps/ios-maps-sdk كعنوان URL، واضغط على Enter لسحب الحزمة، ثم انقر على إضافة حزمة.
  7. قد تحتاج إلى إعادة تعيين ذاكرة التخزين المؤقت للحزمة باستخدام ملف > الحزم > إعادة ضبط ذاكرة التخزين المؤقت للحزمة

استخدام 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 update لتحديث Podfile وتثبيت أحدث حزمة تطوير برامج (SDK). لمزيد من التفاصيل، يمكنك الاطّلاع على دليل CocoaPods.
  6. افتح map-with-marker.xcworkspace للمشروع (بالنقر المزدوج). ملف لفتحه في Xcode. يجب استخدام ملف .xcworkspace لفتح المشروع.

الحصول على مفتاح واجهة برمجة تطبيقات وتفعيل واجهات برمجة التطبيقات اللازمة

لإكمال هذا الدليل التعليمي، يجب استخدام مفتاح واجهة برمجة تطبيقات من Google معتمد استخدام حزمة تطوير البرامج بالاستناد إلى بيانات "خرائط Google" لتطبيقات iOS. انقر فوق الزر التالي للحصول على مفتاحًا وتنشيط واجهة برمجة التطبيقات.

البدء

لمزيد من التفاصيل، يُرجى مراجعة الحصول على مفتاح واجهة برمجة التطبيقات

إضافة مفتاح واجهة برمجة التطبيقات إلى تطبيقك

أضِف مفتاح واجهة برمجة التطبيقات إلى AppDelegate.swift على النحو التالي:

  1. لاحظ أنه تمت إضافة عبارة الاستيراد التالية إلى الملف:
    import GoogleMaps
  2. تعديل السطر التالي في application(_:didFinishLaunchingWithOptions:) ، مع استبدال YOUR_API_KEY بمفتاح واجهة برمجة التطبيقات:
    GMSServices.provideAPIKey("YOUR_API_KEY")

إنشاء تطبيقك وتشغيله

  1. اربط جهاز iOS بجهاز الكمبيوتر، أو اختَر المحاكي من قائمة مخطط Xcode.
  2. وإذا كنت تستخدم جهازًا، فتأكَّد من تمكين خدمات الموقع الجغرافي. إذا كنت تستخدم محاكيًا، اختَر موقعًا جغرافيًا من الميزات. القائمة.
  3. في Xcode، انقر على خيار القائمة Product/Run (تشغيل) (أو زر التشغيل ).
    • ينشئ Xcode التطبيق، ثم يشغله على الجهاز أو على المحاكي.
    • من المفترض أن تظهر لك خريطة ذات علامة موضوعة في وسط سيدني على الساحل الشرقي لأستراليا، مماثلة للصورة المعروضة في هذه الصفحة.

تحرّي الخلل وإصلاحه:

  • إذا لم تظهر لك خريطة، تأكَّد من حصولك على مفتاح واجهة برمجة التطبيقات وإضافة بالتطبيق، كما هو موضح سابقًا. التحقُّق من الإجابة وحدة التحكّم في تصحيح الأخطاء في Xcode بحثًا عن رسائل الخطأ المتعلّقة بمفتاح واجهة برمجة التطبيقات.
  • في حال حظر مفتاح واجهة برمجة التطبيقات باستخدام معرّف حزمة 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;
          

بشكل افتراضي، تعرض حزمة تطوير البرامج بالاستناد إلى بيانات "خرائط Google" لتطبيقات iOS محتوى المعلومات. النافذة عندما ينقر المستخدم على علامة. ليست هناك حاجة إلى إضافة مستمع للنقرة إلى محدد الموقع إذا كنت تريد استخدام السلوك الافتراضي.

تهانينا! لقد أنشأتَ تطبيق iOS يعرض خريطة Google للإشارة إلى موقع معين. لقد تعلمت أيضًا كيفية استخدام حزمة تطوير البرامج بالاستناد إلى بيانات "خرائط Google" لتطبيقات iOS

الخطوات التالية

تعرّف على مزيد من المعلومات حول كائن الخريطة وما تفعله باستخدام العلامات.