Swift 将地质点转换为地图上的注释
我试图将Parse server中保存的PFGeopoint对象转换为注释,但我似乎无法理解,也不知道出了什么问题。 我试图应用这个问题的代码:但我仍然无法理解 这是我的密码:Swift 将地质点转换为地图上的注释,swift,parse-platform,annotations,maps,Swift,Parse Platform,Annotations,Maps,我试图将Parse server中保存的PFGeopoint对象转换为注释,但我似乎无法理解,也不知道出了什么问题。 我试图应用这个问题的代码:但我仍然无法理解 这是我的密码: import UIKit import MapKit import CoreLocation import Parse class MapVC: UIViewController, MKMapViewDelegate { fileprivate let locationManager = CLLocationMan
import UIKit
import MapKit
import CoreLocation
import Parse
class MapVC: UIViewController, MKMapViewDelegate {
fileprivate let locationManager = CLLocationManager()
fileprivate var startedLoadingPOIs = false
fileprivate var places = [Place]()
var descLocation: PFGeoPoint = PFGeoPoint()
var mapHasCenteredOnce = false
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
locationManager.requestWhenInUseAuthorization()
mapView.userTrackingMode = MKUserTrackingMode.followWithHeading
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView: MKAnnotationView?
if annotation.isKind(of: MKUserLocation.self) {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "User")
annotationView?.image = UIImage(named: "loc.png")
}
return annotationView
}
}
extension MapVC: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//1
if locations.count > 0 {
let location = locations.last!
print("Accuracy: \(location.horizontalAccuracy)")
//2
if location.horizontalAccuracy < 100 {
//3
manager.stopUpdatingLocation()
let span = MKCoordinateSpan(latitudeDelta: 0.014, longitudeDelta: 0.014)
let region = MKCoordinateRegion(center: location.coordinate, span: span)
mapView.region = region
if !startedLoadingPOIs {
startedLoadingPOIs = true
let loader = PlacesLoader()
loader.loadPOIS(location: location, radius: 1000) { placesDict, error in
if let dict = placesDict {
let query = PFQuery(className: "posts")
if let latitude = (PFUser.current()?["location"] as AnyObject).latitude {
if let longitude = (PFUser.current()?["location"] as AnyObject).longitude {
let geoPoint = PFGeoPoint(latitude: latitude, longitude: longitude)
query.whereKey("postLocation", nearGeoPoint: geoPoint, withinKilometers: 1)
}
}
query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
if error == nil {
for object in objects! {
let pfObject = object as PFObject
let caption = pfObject["title"] as! String
self.descLocation = object["postLocation"] as! PFGeoPoint
let latitude: CLLocationDegrees = self.descLocation.latitude
let longitude: CLLocationDegrees = self.descLocation.longitude
let location = CLLocation(latitude: latitude, longitude: longitude)
let place = Place(location: location, name: caption )
self.places.append(place)
let annotation = PlaceAnnotation(location: place.location!.coordinate, title: place.placeName)
DispatchQueue.main.async {
self.mapView.addAnnotation(annotation)
}
}
}
}
}
}
}
}
}
}
}
导入UIKit
导入地图套件
导入核心定位
导入解析
类MapVC:UIViewController,MKMapViewDelegate{
fileprivate let locationManager=CLLocationManager()
fileprivate var startedLoadingPOIs=false
fileprivate变量places=[Place]()
变量描述位置:PFGeoPoint=PFGeoPoint()
var mapHasCenteredOnce=false
@ibvar映射视图:MKMapView!
重写func viewDidLoad(){
super.viewDidLoad()
mapView.delegate=self
locationManager.delegate=self
locationManager.desiredAccuracy=KCallocationAccuracyNearesttenmeters
locationManager.startUpdatingLocation()
locationManager.RequestWhenUseAuthorization()
mapView.userTrackingMode=MKUserTrackingMode.followWithHeading
}
func mapView(uMapView:MKMapView,viewFor annotation:MKAnnotation)->MKAnnotationView{
var annotationView:MKAnnotationView?
if annotation.isKind(of:MKUserLocation.self){
annotationView=MKAnnotationView(注释:注释,重用标识符:“用户”)
annotationView?.image=UIImage(名为:“loc.png”)
}
返回注释视图
}
}
扩展映射VC:CLLocationManagerDelegate{
func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation]){
//1
如果locations.count>0{
让location=locations.last!
打印(“精度:\(位置.水平精度)”)
//2
如果位置水平精度<100{
//3
manager.stopUpdateLocation()
设span=MKCoordinateSpan(相对偏差:0.014,纵向偏差:0.014)
let region=MKCoordinateRegion(中心:location.coordinate,span:span)
mapView.region=区域
如果!开始加载POI{
startedLoadingPOIs=true
let loader=PlacesLoader()
loader.loadPOIS(位置:位置,半径:1000){placesDict,错误在
如果让dict=位置dict{
let query=PFQuery(类名:“posts”)
如果让纬度=(PFUser.current()?[“location”]作为任何对象)。纬度{
如果让经度=(PFUser.current()?[“location”]作为任何对象)。经度{
let geoPoint=PFGeoPoint(纬度:纬度,经度:经度)
query.whereKey(“postLocation”,nearGeoPoint:geoPoint,withinKilometers:1)
}
}
query.findObjectsInBackground{(对象:[PFObject]?,错误:error?)位于
如果错误==nil{
对于对象中的对象{
将pfObject=对象设为pfObject
将caption=pfObject[“title”]设为!字符串
self.descLocation=对象[“postLocation”]作为!PFGeoPoint
让纬度:CLLocationDegrees=self.descLocation.latitude
让经度:CLLocationDegrees=self.descLocation.longitude
let location=CLLocation(纬度:纬度,经度:经度)
let place=place(位置:位置,名称:标题)
self.places.append(位置)
let annotation=PlaceAnnotation(位置:place.location!.coordinate,标题:place.placeName)
DispatchQueue.main.async{
self.mapView.addAnnotation(注释)
}
}
}
}
}
}
}
}
}
}
}
我刚刚有两条uanessacry线路阻塞了该操作,将它们删除后,它对我起了作用
loader.loadPOIS(位置:位置,半径:1000){placesDict,错误在
如果让dict=placesDict{欢迎使用SO!请检查以下内容并添加您收到的错误,以帮助他人回答: