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 MapKit
import 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: 28.6139, longitude: 77.2090),
title: "नई दिल्ली",
subtitle: "इंडिया गेट"
)
mapView.addAnnotation(annotation)
3. जियोकोडिंग: पते को कोऑर्डिनेट में बदलना
CLGeocoder क्लास टेक्स्ट एड्रेस को कोऑर्डिनेट (फॉरवर्ड जियोकोडिंग) और इसके उलट (रिवर्स) में बदलता है।
import CoreLocation
let geocoder = CLGeocoder()
geocoder.geocodeAddressString("नई दिल्ली, इंडिया गेट") { placemarks, error in
guard let placemark = placemarks?.first else {
print("पता नहीं मिला")
return
}
print("अक्षांश: \(placemark.location?.coordinate.latitude ?? 0)")
print("देशांतर: \(placemark.location?.coordinate.longitude ?? 0)")
}