2017-03-28 2 views
0

시설과 사용자를지도에 표시하는 앱을 만들었습니다. 시설과 사용자 모두 사용자 정의 주석 (MKPointAnnotation 확장)입니다. 또한 주석 유형의 두 유형에 대한 설명 선, 왼쪽 및 오른쪽 설명 선 액세서리보기를 사용 설정했습니다. 이러한 개체의 지리 정보 및 기타 메타 데이터는 백그라운드에서 내 구문 분석 데이터베이스에서로드됩니다. viewDidLoad()에있는 모든 특수 효과를로드해야한다고 여러 곳에서 권장 사항을 보았습니다. 이것이 합리적인 것처럼 보이지만, 사용자의 현재 지역에있는 항목을 신속하게로드하고 표시하려는 올바른 접근 방식인지 궁금합니다.iOS 맞춤 어노테이션로드 중

내 현재 접근 방식은 다음과 같습니다. 사용자의 현재 위치를 기준으로 viewDidLoad()에 일련의 특수 효과를로드합니다. MapView 대리인에 대한 내 regionDidChange 메서드에서 검색을 수행하고 모든 주석을 제거한 다음 다시 추가합니다. 이것은 주로 작동하지만 때로는 사용자의 주석에 시설의 액세서리보기가 할당됩니다. 필자는 타이밍과 관련이있는 것으로 여러 번 확인했으며 주석이 추가/표시되는 동안 사용자가지도를 팬하면 발생합니다. 디버거에서는 발생하지 않습니다. 사용자가 자신의 지역을 변경할 때 특수 효과를 다시로드하고있는 것으로 보입니다.

필자가 주석을 근본적으로로드하는 경우 피드백을 받고 싶습니다. 사용자가 위치를 이동하거나지도에서 이동하면서 주석을 동적으로로드/표시하도록 모델링해야합니다. 사용자가 여러 유형의 사용자 정의 주석을 가지고 있는지 여부는 알 수 없지만 사용자가지도를 이동하면 Redfin 속성을로드하는 것을 보았습니다.

감사합니다.

인 Rajesh

답변

0

http://swift3devlopment.blogspot.in/ 여기 당신이 실제로 필요한 몇 가지 물건 ..

+0

감사합니다. 그러나 mu로드에 대한 특정 참조를 찾을 수 없습니다. 지도를 통해 사용자가 이동하면서 사용자 정의 주석을 동적으로 표시합니다. – rajdave

0

1 단계가 : 당신의 Viewcontroller.swift에 가서 코드를 작성 : 2 단계

class custumAnnotation : MKPointAnnotation 
    { 
     var imgName : String? 

    } 

아래처럼 클래스를 만들기를 다음과 같이

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) 

    } 
}