2015-01-14 2 views
0

나는 Swift를 배우며 MKMapKit의 하위 클래스를 만들어 두 지점 간의 거리를 확인하고 맞춤 주석을 만들고 모든지도 코드를 하나의 클래스로 분리하는 등의 특정 기능을 캡슐화하려고합니다.신속한 맞춤형지도 클래스

I 클래스 작성한

:

class GameMapViewController: MKMapView, MKMapViewDelegate{...} 

는 I 메인 뷰 컨트롤러 코드의 클래스를 개시 (및 I를 제어 할 수 있도록보다 쉽게이고 스토리 보드 뷰에 하위 뷰로 추가를) :

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {...} 
: 모든 위로 확인하고 나는 사용자 정의 주석에서 SEGUE를 트리거 할 때를 제외하고 모든 일을 설정

gameMap = GameMapViewController(container: mapViewHolder) 

6,

didSelectAnnotationView은 내가 주석 선에 탭 때 호출하지만 아무것도 모든 solutionssimilar에 대한 질문은 내가 사용한다 제안 있음을, 내가 찾고 있어요 방법 performSegueWithIdentifier이 없습니다됩니다 ....

는 (I 퍼팅 시도 MapKit 스토리 보드에보기 및 해당 클래스를 변경하여 GameMapViewController을 사용하지만 초기화 기능이 전혀 실행되지 않음)

내가 사용자 정의 클래스를 초기화하는 방법을 추측하고 있습니까?

MainViewController.swift :

override func viewDidLoad() { 
super.viewDidLoad() 
.... 
// Create the game map 
gameMap = GameMapViewController(container: mapViewHolder) 
mapViewHolder.addSubview(gameMap) 

... 

} 

GameMapViewController.swift : 당신이 뷰와 뷰 컨트롤러를 혼동하고 있기 때문에

import UIKit 
import MapKit 


class GameMapViewController: MKMapView, MKMapViewDelegate{ 

var spanQuestion:MKCoordinateSpan = MKCoordinateSpanMake(180, 180) 
var spanAnswer:MKCoordinateSpan = MKCoordinateSpanMake(180, 180) 
var hasUserCityLocationGuess: Bool = false 

var containingView: UIView 

override init(){ 
    println ("GameMapViewController init") 
    containingView = UIView() 
    super.init(frame: CGRect(x: 0, y: 0, width: 1000, height: 1000)) 

    self.delegate=self 
    var latDeltaAnswer:CLLocationDegrees = 50 
    var lngDeltaAnswer:CLLocationDegrees = 50 
    spanAnswer = MKCoordinateSpanMake(latDeltaAnswer, lngDeltaAnswer) 

    var latDeltaQuestion:CLLocationDegrees = 180 
    var lngDeltaQuestion:CLLocationDegrees = 180 
    spanQuestion = MKCoordinateSpanMake(latDeltaQuestion, lngDeltaQuestion) 



} 

required init(coder aDecoder: NSCoder) { 
    containingView = UIView() 
    super.init(coder: aDecoder) 
    self.delegate = nil 
    println ("GameMapViewController init with decoder") 
} 


convenience init(container: UIView) { 
    println ("GameMapViewController convenience") 
    self.init() 
    self.delegate = self 
    containingView = container 


} 

func mapViewDidFinishLoadingMap(mapView: MKMapView!) { 
    println("mapViewDidFinishLoadingMap") 
} 

func mapViewWillStartLoadingMap(mapView: MKMapView!) { 

    self.frame = CGRect (x: 0, y: 0, width: containingView.frame.width, height: containingView.frame.height) 
    self.contentMode = UIViewContentMode.ScaleAspectFill 
    superview?.sizeToFit() 
    var guessPlaceRecognizer = UILongPressGestureRecognizer(target: self, action: "guessPlace:") 
    guessPlaceRecognizer.minimumPressDuration = 1.0 
    mapView.addGestureRecognizer(guessPlaceRecognizer) 
    mapView.mapType = MKMapType.Satellite 

} 

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { 
    if overlay is MKCircle { 
     var circleRenderer = MKCircleRenderer(overlay: overlay) 
     circleRenderer.strokeColor = UIColor.redColor() 
     circleRenderer.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1) 
     circleRenderer.lineWidth = 1 
     //userOverlayCircleRender = circleRenderer 
     return circleRenderer 
    } else { 
     return nil 
    } 
} 

func guessPlace(gestureRecognizer:UIGestureRecognizer){ 

    let guessPlaceFirst = NSUserDefaults.standardUserDefaults().boolForKey("guess_place_preference") 

    if guessPlaceFirst { 
     var touchPoint = gestureRecognizer.locationInView(self) 
     var newCoord:CLLocationCoordinate2D = self.convertPoint(touchPoint, toCoordinateFromView: self) 
     var userAnnotation = UserPointAnnotation() 
     userAnnotation.coordinate = newCoord 
     self.addAnnotation(userAnnotation) 


     var getLat: CLLocationDegrees = newCoord.latitude 
     var getLon: CLLocationDegrees = newCoord.longitude 
     var circleCenter: CLLocation = CLLocation(latitude: getLat, longitude: getLon) 
     addRadiusCircle(circleCenter) 
     hasUserCityLocationGuess = true 
    } 

} 

func showCity() { 
    let location = CLLocationCoordinate2D(latitude: (currentCity["latitude"]! as CLLocationDegrees), longitude: (currentCity["longitude"]! as CLLocationDegrees)) 
    let region:MKCoordinateRegion = MKCoordinateRegionMake(location, self.spanAnswer) 
    let city: String = currentCity["city"]! as String 
    let conditions: String = currentCity["description"] as String 
    let country: String = currentCity["country"]! as String 
    let address = "\(city), \(country)" 
    let cityAnnotation = CityPointAnnotation() 

    cityAnnotation.title = address 
    cityAnnotation.subtitle = "\(conditions)" 
    cityAnnotation.coordinate = location 


    self.setRegion(region, animated: true) 
    self.addAnnotation(cityAnnotation) 
    self.selectAnnotation(cityAnnotation, animated: true) 


} 

func cityInfoClick(sender:UIButton){ 
    //sender.performSegueWithIdentifier("segueCityWebView") 
} 




func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    // Handle any custom annotations. 

    if annotation is CityPointAnnotation { 

     // Try to dequeue an existing pin view first. 
     let reuseId = "CityPointAnnotationView" 
     var annotationView = self.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
     if annotationView == nil { 
      annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      annotationView.image = UIImage(named: "marker.png") 
      annotationView.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoDark) as UIButton 
      annotationView.canShowCallout = true 
      return annotationView; 

     } else { 

      annotationView.annotation = annotation 
     } 

     return annotationView 

    } 
    return nil; 
} 

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) { 
    println("didSelectAnnotationView") 
} 

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) { 
    println("calloutAccessoryControlTapped1") 

    /////////////////// 
    // I want to do a segue here 
    // but nothing has the method performSegueWithIdentifier (self, mapView, control....) 
    /////////////////// 


} 


func resetMap(){ 
    self.removeAnnotations(self.annotations) 
    self.removeOverlays(self.overlays) 
    var region:MKCoordinateRegion = MKCoordinateRegionMake(self.centerCoordinate, spanQuestion) 
    self.setRegion(region, animated: true) 
    hasUserCityLocationGuess = false 

} 
func addRadiusCircle(location: CLLocation){ 

    var radius = NSUserDefaults.standardUserDefaults().doubleForKey("guess_place_radius") as CLLocationDistance 
    var circle = MKCircle(centerCoordinate: location.coordinate, radius: radius) 

    self.removeOverlays(self.overlays) 
    self.addOverlay(circle) 


} 

func doGeoCode(cityObject:PFObject) -> Bool { 
    .... 
} 

func userCityLocationGuess(userGuessTemp:Int)->NSDictionary { 
    .... 

} 

}

답변

2

그것입니다. 보기 (하위 클래스는 MKMapView이지만 컨트롤러의 이름을 지정하고 컨트롤러로 사용하려고합니다.) 또한 컨트롤러의 작업을 수행합니다.

따라서 실제로 컨트롤러를 소유하고있는보기 컨트롤러가 있어야합니다. 지도보기 (일반 MKMapView)를 구성한 다음 각 부분과 상호 작용할 수 있습니다.

+0

예, 실제로 혼란 스러웠습니다. 입력 해 주셔서 감사합니다. – bba13