Swift 编程:地图视图上的多个位置

Swift programming: Multiple Positions on Map View

我有一个简单的问题,我是 IOS 的新手 - 我想在同一张地图上显示多个 GPS 数据流。请帮助如何做到这一点

当前代码有效:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad()
{
    super.viewDidLoad()

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true
    self.mapView.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func locationmanager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let location = locations.last

    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.mapView.setRegion(region, animated: true)

    self.locationManager.stopUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
    print("Errors: " + error.localizedDescription)
}

}

但这只给了我一个位置。

您显示了一张地图,您希望在该地图上显示两个位置。

第一个位置是设备的位置,因此您可以从设备的 GPS 获取坐标。

第二个位置是别人的位置。你从哪里得到他们的坐标?您无法访问他们的 GPS。他们设备上的应用程序必须将他们的坐标写入某处的服务器。然后,您的应用程序从服务器读取这些坐标并使用 MKPointAnnotation 显示它们。除了 运行 所在的设备之外,应用程序没有 "GPS stream"。

同样,您设备上的应用程序必须编写您自己的坐标,这样它才能出现在他们的应用程序上。

如果您没有服务器,可以使用 Apple 的 CloudKit(例如)进行调查。