小编Ste*_*eve的帖子

Swift MKMapView多边形叠加故障

在极少数情况下,我的地图上的叠加(小蓝点)会产生奇怪的眩光(右边的大蓝色区域)(如图所示).有时放大或缩小会修复它,但并非总是如此.找不到任何关于为什么会这样的事情.它与渲染方式有关吗?

在此输入图像描述

func drawLocations(_ loc: CLLocation)
    {
        let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
        let lat: CLLocationDegrees = center.latitude
        let long: CLLocationDegrees = center.longitude
        var points = [CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long)]
        let polygon = MKPolygon(coordinates: &points, count: points.count)
        DispatchQueue.main.async(execute: {
            self.mapView.add(polygon)
        })
    }
func mapView(_ mapView: MKMapView!, rendererFor overlay: MKOverlay!) -> MKOverlayRenderer!
    {
        if overlay is MKPolygon
        {
            let polygonView = MKPolygonRenderer(overlay: overlay)
            polygonView.lineWidth = 4
            polygonView.strokeColor = UIColor(red: 30/255.0, green: 12/255.0, blue: 242/255.0, alpha: 0.4)
            return polygonView
        }
        return nil
    }
Run Code Online (Sandbox Code Playgroud)

overlay function mkmapview ios swift

29
推荐指数
1
解决办法
664
查看次数

无法更换CloudKit记录

我得到致命的错误:"无法在" if (NSKeyedUnarchiver.unarchiveObject(with: loadedData as! Data) as? CKRecord) != nil" 行上将'NSTaggedPointerString'类型的值转换为'NSData' ".

还有另一个致命错误:"当我尝试将locationRecord保存为默认值时,尝试将非属性列表对象设置为键locationData的NSUserDefaults/CFPreferences值".

var locationRecord = CKRecord(recordType: "location")

func getRecordToUpdate(_ locations:CLLocation)
{
    if defaults1.object(forKey: "locationData") == nil{
        locationRecord.setObject(locations, forKey: "location")
        defaults1.set(locationRecord, forKey: "locationData")
        self.updateLocationRecord(locations: locations)
    }else{
        if let loadedData = defaults1.object(forKey: "locationData") {
            print("continue")
            if (NSKeyedUnarchiver.unarchiveObject(with: loadedData as! Data)) != nil
            {
                let publicDB = CKContainer.default().publicCloudDatabase
                publicDB.fetch(withRecordID: locationRecord.recordID,completionHandler: {
                    (record, error) in
                    if error == nil
                    {
                        publicDB.delete(withRecordID: (record?.recordID)!, completionHandler: {
                            (record, error) in
                            if(error == nil){
                                print("old record …
Run Code Online (Sandbox Code Playgroud)

function nsuserdefaults ios swift cloudkit

11
推荐指数
1
解决办法
163
查看次数

位置许可检查和授权

我希望我的程序在地图上显示用户位置.它起初工作然后随机停止所以我添加下面的代码试图解决它但我遇到了问题.提前致谢!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        if CLLocationManager.locationServicesEnabled()
        {

            let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
            if status == CLAuthorizationStatus.NotDetermined
            {
                locationManager.requestAlwaysAuthorization()
            }
            } else {

                print("locationServices disenabled")
            }
            locationManager.startUpdatingLocation()
            self.locationManager.startUpdatingLocation()
            self.mapView.showsUserLocation = true
            mapView.delegate = self
            centerMapOnLocation(initialLocation)
            addBoundry()
    }
Run Code Online (Sandbox Code Playgroud)

permissions location mapkit ios swift

7
推荐指数
3
解决办法
2万
查看次数

Swift PNG图像以不正确的方向保存

如果我在保存图像之前使用它是正常的.但如果我保存它并使用它以后是90度转动.我怎样才能确保它不会横向保存?

func saveEvent(_ center1: CLLocation, title2: String, imagePicked1: UIImage)
    {
        let data = UIImagePNGRepresentation(imagePicked1);///
        let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(NSUUID().uuidString+".dat")
        do {
            try data!.write(to: url!, options: [])
        } catch let e as NSError {
            print("Error! \(e)");
            return
        }
        let image11 = CKAsset(fileURL: url!)

        self.eventRecord.setObject(image11 as CKAsset, forKey: "Picture")
        let publicData = CKContainer.default().publicCloudDatabase
            publicData.save(self.eventRecord, completionHandler: { record, error in
                if error == nil
                {
                    print("Image saved")
                }else{
                    print(error!)
                }
        })
    }
Run Code Online (Sandbox Code Playgroud)

png uiimagepickercontroller uiimage ios swift

7
推荐指数
1
解决办法
1130
查看次数

如何使用Mapkit和Swift在设置的位置叠加一个圆

我在尝试弄清楚如何在用户位置唯一的所需位置显示透明圆形或矩形时遇到麻烦。我是mapkit的初学者,在此先感谢。

class FirstViewController: 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.requestAlwaysAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true

}

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: 1, longitudeDelta: 1))
    self.mapView.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()//
}

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

}
Run Code Online (Sandbox Code Playgroud)

location overlay mapkit ios swift

4
推荐指数
1
解决办法
4001
查看次数

如何在Swift中向MKPointAnnotation添加按钮

我希望能够在地图注释上放置一个按钮和标签.标题,副标题和坐标的注释完美无缺,但我无法显示按钮.

func drawEvents(_ loc: CLLocation, title1: String)
    {
        mapView.delegate = self//
        let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
        let lat: CLLocationDegrees = center.latitude
        let long: CLLocationDegrees = center.longitude
        self.pointAnnotation1 = MKPointAnnotation()
        self.pointAnnotation1.title = title1
        self.pointAnnotation1.subtitle = "Event"
        self.pointAnnotation1.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
        self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation1, reuseIdentifier: nil)
        self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
    }
Run Code Online (Sandbox Code Playgroud)

annotations uibutton mkmapview ios swift

4
推荐指数
1
解决办法
2531
查看次数

如果NSUserDefault存在于Swift中,则尝试更新CLLocation

最初,我试图将CLLocation另存为NSUserDefault值,然后再将其存储为CKRecord在CLoudkit中,但是出现错误:“ defaults.setObject(locationRecord.recordID,forKey:“ locationRecordID”)“,原因是“试图将非属性列表对象设置为键locationRecordID的NSUserDefaults / CFPreferences值。因此,现在我尝试将lat和long保存为默认值,并替换Cloudkit中的旧位置。我当前在“ publicDB.fetchRecordWithID((defaults.objectForKey(“ Location”)as!CKRecordID),completionHandler: “ 行上收到“ Thread 1 SIGABRT ”错误,原因是“ 无法转换类型为'__NSCFDictionary'的值(0x1a1bec968)为“ CKRecordID”。”

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations.last
        self.loc1 = location!
        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.004, longitudeDelta: 0.004))
        self.mapView.setRegion(region, animated: true)
        self.locationManager.stopUpdatingLocation()
        self.locationManager.startMonitoringSignificantLocationChanges()

        self.getRecordToUpdate(locations)

        let lat: CLLocationDegrees = center.latitude
        self.lat1 = lat
        let long: CLLocationDegrees = center.longitude
        self.long1 = long

        locationRecord.setObject(location, forKey: "location")
        let publicData = CKContainer.defaultContainer().publicCloudDatabase
        publicData.saveRecord(locationRecord) { …
Run Code Online (Sandbox Code Playgroud)

function nsuserdefaults ios swift cloudkit

2
推荐指数
1
解决办法
503
查看次数

如何从cloudkit中获取所有记录

我无法让我的应用程序获取所有类型的记录并让每个用户记录放入一个数组.

这是保存记录的代码

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
   let id = CKRecordID(recordName: "01")
   let locationRecord = CKRecord(recordType: "location", recordID: id)
   locationRecord.setObject(location, forKey: "location")
   let publicData = CKContainer.defaultContainer().publicCloudDatabase
   publicData.saveRecord(locationRecord) { record, error in
     //..
   }
}
Run Code Online (Sandbox Code Playgroud)

arrays ios swift cloudkit ckrecord

1
推荐指数
1
解决办法
1836
查看次数

在 Swift 中将 CLLocationCoordinate2D 转换为 CLLocation 时出错

我正在尝试将 cllocationcoordinate2d 转换为 cllocation。我在下面代码的第 12 行收到错误“容器文字中的预期表达式”。我还在第 13 行收到错误“无法将 cllocationcoordinate2d 类型的值转换为期望值 cllocation”,但这是因为第 12 行工作不正常。

@IBAction func makeEvent(sender: UIButton)
    {
        let center = CLLocationCoordinate2D(latitude: loc1.coordinate.latitude, longitude: loc1.coordinate.longitude)
        let lat: CLLocationDegrees = center.latitude
        let long: CLLocationDegrees = center.longitude
        self.pointAnnotation1 = MKPointAnnotation()
        self.pointAnnotation1.title = "Event"
        self.pointAnnotation1.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
        self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation1, reuseIdentifier: nil)
        self.mapView?.centerCoordinate = self.pointAnnotation1.coordinate
        self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
        CLLocation *center = [[CLLocation alloc] initWithLatitude:latt longitude:longg]
        eventRecord.setObject(center, forKey: "event")
        let publicData = CKContainer.defaultContainer().publicCloudDatabase
        publicData.saveRecord(eventRecord) { record, error in
        }
        if error == …
Run Code Online (Sandbox Code Playgroud)

xcode cllocationmanager ios swift cloudkit

1
推荐指数
1
解决办法
4759
查看次数