2017-03-28 1 views
0

私はマップ上に施設とユーザーを表示するアプリを構築しました。施設とユーザーの両方がカスタム注釈です(私はMKPointAnnotationを拡張しました)。私はまた、注釈の両方の種類のコールアウト、左右のコールアウトアクセサリビューを有効にしました。これらのオブジェクトのジオおよびその他のメタデータは、バックグラウンドで私のParseデータベースからロードされます。 viewDidLoad()にすべての注釈を読み込む必要があることが多くの場所で推奨されています。それは合理的ですが、私はそれがユーザーの現在の地域にあるものをすばやく読み込んで表示するための正しいアプローチであるかどうか疑問に思っています。iOSカスタムアノテーションを読み込んでいます

私の現在のアプローチは次のとおりです。ユーザの現在地に基づいてviewDidLoad()に一連の注釈を読み込みます。 MapViewデリゲートの私のregionDidChangeメソッドでは、検索を実行し、すべての注釈を削除して、それらを再度追加します。これは主に機能しますが、時には施設のアクセサリビューがユーザーの注釈に割り当てられます。私は何度もすべてをチェックしたが、それはタイミングに関係しているように見え、注釈が追加/表示されている間にユーザーが地図をパ​​ンすると起こる。デバッガでは発生しません。私はそれがユーザーの地域を変更したときに注釈をリロードしていると思います。

アノテーションを基本的に間違って読み込んでいて、ユーザーが場所を移動したり地図上を移動したりするときに注釈を動的にロード/表示する方法を教えてください。ユーザーが複数の種類のカスタム注釈を持っているかどうかわからないものの、ユーザーがマップをパンすると、Redfinロードプロパティが表示されています。

ありがとうございました!ここ

ラジェッシュ

答えて

0

http://swift3devlopment.blogspot.in/は、あなたが実際に必要ないくつかのものを持っている...

+0

ありがとうございますが、私はmuを読み込むための具体的な参考文献を見つけることができませんでしたユーザーがマップをパンすると、カスタム注釈が動的に表示されます。 – rajdave

0

ステップ1:あなたのViewcontroller.swiftに移動し、コードを書く:

class custumAnnotation : MKPointAnnotation 
    { 
     var imgName : String? 

    } 

ステップ2以下のようなクラスを作成します。次のように

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController,MKMapViewDelegate { 

    @IBOutlet var mapView : MKMapView! 
    var CLManager = CLLocationManager() 
    var flag : Int = 0 
    var tag : Int = 0 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     CLManager.requestWhenInUseAuthorization() 
     mapView.mapType = .hybridFlyover 
     mapView.showsUserLocation = true 

     let center = CLLocationCoordinate2D(latitude: 18.4575 
      , longitude: 73.8677) 

     let center1 = CLLocationCoordinate2D(latitude: 18.5597 
      , longitude: 73.7799) 
     let point1 = custumAnnotation() 
     point1.coordinate = center 
     point1.imgName = "1.png" 
     point1.title = "First" 
     point1.subtitle = "Subtitle1" 

     let point2 = custumAnnotation() 
     point2.coordinate = center1 
     point2.imgName = "2.png" 
     point2.title = "Second" 
     point2.subtitle = "Subtitle2" 

     mapView.addAnnotation(point1) 
     mapView.addAnnotation(point2) 
     mapView.delegate = self 
    } 

    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { 

     if(flag == 0) 
     { 
      flag=1 
      let userLocation = userLocation.location?.coordinate 

      let center = CLLocationCoordinate2D(latitude: (userLocation?.latitude)! 
       , longitude: (userLocation?.longitude)!) 

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

      mapView.setRegion(region, animated: true) 
      mapView.showsUserLocation = false 

      let annotation = MKPointAnnotation() 
      annotation.coordinate = center 
      annotation.title = "ME" 
      annotation.subtitle = "CD" 
      mapView.addAnnotation(annotation) 
     } 
    } 

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 



     if !(annotation is custumAnnotation) { 
      return nil 
     } 

     let reuseId = "test" 

     var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) 
     if anView == nil 
     { 
      anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      anView?.canShowCallout = true 
     } 
     else 
     { 
      anView?.annotation = annotation 

     } 

     let cpa = annotation as! custumAnnotation 
     anView?.image = UIImage(named:cpa.imgName!) 
     anView?.tag = tag 
     tag = tag + 1 
     return anView 
    } 
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 

     print(view.tag) 

    } 
} 
関連する問題