2017-12-04 17 views
3

マップに注釈を追加すると、それらが時々表示されることがあります。彼らは同じ家にいる場合は、1つは表示されません言うことができます。どのように私はそれらの両方を表示させるのですか?カスタムアノテーションクラスを作成する必要がありますか? ios11には機能が集中していると聞きましたが、それを使用する必要がありますか?ここでは、コード(要約し)です:MKAnnotationViewにios11 MKAnnotationが両方の注釈を表示していません

import UIKit 
import MapKit 
import Firebase 

class GameViewController: UIViewController { 


@IBOutlet weak var mapView: MKMapView! 

fileprivate var locations = [CLLocation]() 
fileprivate var userLocations = [(loc: CLLocation, name: String, team: String)]() 
fileprivate var userAnnotations = [MKAnnotation]() 
fileprivate var hasBeenUP = false 

var ref: FIRDatabaseReference! 
let uid = FIRAuth.auth()!.currentUser!.uid 

var timer = Timer() 
var timeLeft = 0.0 
var firstTimer = Timer() 

var name = "" 
var team = "" 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let center = CLLocationCoordinate2D(latitude: 47.786769, longitude: -20.413634) 
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 
    self.mapView.setRegion(region, animated: true) 
    mapView.mapType = .hybrid 
    locationManager.startUpdatingLocation() 

    ref = FIRDatabase.database().reference() 

    setupULSending() 
    getMetaInfo() 

    ref.child("realtimeLocations").observe(FIRDataEventType.value, with: { (snapshot) in 
     self.userLocations = [] 
     for rest in snapshot.children.allObjects as! [FIRDataSnapshot] { 
      guard let snapshotValue = snapshot.value as? NSDictionary, let snapVal = snapshotValue[rest.key] as? NSDictionary else { 
       break 
      } 
      let name = snapVal["name"] as! String 
      let team = snapVal["team"] as? String ?? "" 
      if let lat = snapVal["lat"] as? Double, 
       let long = snapVal["long"] as? Double { 
       let location = CLLocation(latitude: lat, longitude: long) 
       self.userLocations.append((loc: location, name: name, team: team)) 
      }else { 

      } 
     } 
     DispatchQueue.main.async { 
      self.updateUserLocation() 
     } 
    }) 
} 

private lazy var locationManager: CLLocationManager = { 
    let manager = CLLocationManager() 
    manager.allowsBackgroundLocationUpdates = true 
    manager.desiredAccuracy = kCLLocationAccuracyBest 
    manager.delegate = self 
    manager.requestAlwaysAuthorization() 
    return manager 
}() 

func updateUserLocation() { 

    for an in self.mapView.annotations { 
     mapView.removeAnnotation(an) 
    } 
    for loc in userLocations { 
     let annotation = MKPointAnnotation() 
     annotation.coordinate = loc.loc.coordinate 
     annotation.title = loc.name 
     annotation.subtitle = "local" 
     mapView.addAnnotation(annotation) 

    } 
} 

} 

// MARK: - CLLocationManagerDelegate 
extension GameViewController: CLLocationManagerDelegate { 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations 
    locations: [CLLocation]) { 
    let location = locations.last as! CLLocation 
    self.locations.append(location) 
} 
} 

答えて

2

、あなたは「必要」にMKFeatureDisplayPriorityを設定する必要があります。注釈ビューを変更するには、MKMapViewDelegateとmapView(MKMapView, viewFor: MKAnnotation)を実装します。このような何か:このため

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     if annotation is MKUserLocation { return nil } 
     var view = mapView.dequeueReusableAnnotationView(withIdentifier: "yourIdentifier") 
     if view == nil { 
      view = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: "yourIdentifier") 
     } 
     view?.displayPriority = .required 
     return view 
} 

その他のオプションは、WWDC 2017のビデオ237「MapKitの新機能」で説明されてい

関連する問題