الموقع الجغرافي والخرائط في Swift: نظرة عامة على CoreLocation وMapKit
غالبًا ما يتطلب تطوير التطبيقات لنظامي iOS وmacOS العمل مع البيانات الجغرافية وعرض الخرائط. في نظام Apple البيئي، يوجد إطاران قويان لهذا الغرض: CoreLocation (لتحديد الموقع، الترميز الجغرافي، ومناطق المراقبة) وMapKit (لعرض الخرائط، إضافة التعليقات التوضيحية، وإنشاء المسارات). في هذه المقالة، سنشرح بالتفصيل إمكانياتهما ونقدم أمثلة عملية بلغة Swift.
1. CoreLocation: الحصول على إحداثيات الجهاز
يوفر CoreLocation الوصول إلى أجهزة تحديد الموقع: GPS، Wi-Fi، والشبكات الخلوية. للبدء، يجب استيراد الإطار وطلب الإذن من المستخدم.
إعداد الأذونات
أضف المفاتيح التالية إلى ملف Info.plist:
NSLocationWhenInUseUsageDescription— لاستخدام التطبيق في الوضع النشطNSLocationAlwaysAndWhenInUseUsageDescription— للمراقبة في الخلفية
مثال للحصول على الموقع الحالي
import CoreLocation
class LocationManager: NSObject, CLLocationManagerDelegate { private let manager = CLLocationManager() override init() { super.init() manager.delegate = self manager.desiredAccuracy = kCLLocationAccuracyBest } func requestPermission() { manager.requestWhenInUseAuthorization() } func startUpdating() { manager.startUpdatingLocation() } // MARK: - Delegate func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let location = locations.last else { return } print("خط العرض: \\(location.coordinate.latitude), خط الطول: \\(location.coordinate.longitude)") } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("خطأ: \\(error.localizedDescription)") }}2. MapKit: عرض الخرائط والتعليقات التوضيحية
يتيح MapKit تضمين خرائط Apple Maps في التطبيق. الفئة الرئيسية هي MKMapView. سننظر في الإعداد الأساسي وإضافة العلامات.
إنشاء خريطة في الكود
import MapKitimport SwiftUI
struct MapView: UIViewRepresentable { @Binding var region: MKCoordinateRegion func makeUIView(context: Context) -> MKMapView { let mapView = MKMapView() mapView.delegate = context.coordinator return mapView } func updateUIView(_ uiView: MKMapView, context: Context) { uiView.setRegion(region, animated: true) } // Coordinator لمعالجة الأحداث func makeCoordinator() -> Coordinator { Coordinator(self) } class Coordinator: NSObject, MKMapViewDelegate { var parent: MapView init(_ parent: MapView) { self.parent = parent } }}إضافة تعليق توضيحي (دبوس على الخريطة)
import MapKit
class CustomAnnotation: NSObject, MKAnnotation { var coordinate: CLLocationCoordinate2D var title: String? var subtitle: String? init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) { self.coordinate = coordinate self.title = title self.subtitle = subtitle }}
// الاستخدام:let annotation = CustomAnnotation( coordinate: CLLocationCoordinate2D(latitude: 55.7558, longitude: 37.6173), title: "موسكو", subtitle: "الساحة الحمراء")mapView.addAnnotation(annotation)3. الترميز الجغرافي: تحويل العنوان إلى إحداثيات
تتيح الفئة CLGeocoder تحويل عنوان نصي إلى إحداثيات (ترميز جغرافي مباشر) والعكس (ترميز جغرافي عكسي).
import CoreLocation
l