2014-10-18 3 views
4

Swift에서 마크 포인트의 이미지를 변경하는 방법을 이해하지 못합니다. 나는좌표에 대한 특수 효과의 이미지는 어떻게 변경합니까?

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! 

의 몇 가지 변화를 시도했지만 호출되지는 결코 보인다 나는 그것을 호출 할 강제하는 방법을 모르겠어요.

나는 프로그래밍 방식으로 MKMapView을 추가 했으므로 iPhone을 사용하는 데 따라 크기 (가로 및 세로)를 전환 할 수 있습니다. (난 아직도 믿을 수 없어이 엑스 코드에 직관적하지 않고 프로그래머는 주위 프로그래밍 할 수 있습니다.)

여기

내 코드입니다 :

@IBAction func addStroke(sender: UIButton) { 
    NSLog("Add Stroke Touched") 
    NSLog("X %f Y %f", manager.location.coordinate.latitude, manager.location.coordinate.longitude) 
    GPSspot = manager.location.coordinate 

    annotation.setCoordinate(GPSspot) 
    annotation.title = "Stroke" 

    let useID = "test" 

    var myAnnotation = mapHole.dequeueReusableAnnotationViewWithIdentifier(useID) 
    if (myAnnotation == nil) { 
     myAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: useID) 
     myAnnotation.image = UIImage(named: "ballSmall.png") 
     myAnnotation.canShowCallout = true 
     myAnnotation.enabled = true 
    } else { 
     myAnnotation.annotation = annotation 
    } 

    mapHole.viewForAnnotation(annotation) 
    mapHole.addAnnotation(annotation) 
} 

//MARK: - Map Kit 
var mapRect = MMRect(xLoc: 502, yLoc:20, yWidth:218, xHeight:386) 
var mapHole:MKMapView! //= MKMapView() as MKMapView 
var annotation = MKPointAnnotation() 
var MAView:MKAnnotationView! 

//MARK: - GPS Locations 
var manager:CLLocationManager! 
var GPSspot:CLLocationCoordinate2D! 
var span:MKCoordinateSpan! 
var region:MKCoordinateRegion! 

//MARK: - Default Initializer 
override func viewDidLoad() { 
    super.viewDidLoad() 

    //MARK: Locations 
    manager = CLLocationManager() 
    if (CLLocationManager.locationServicesEnabled()) { 
     NSLog("locations services started") 
     manager.delegate = self 
     manager.desiredAccuracy = kCLLocationAccuracyBest 
     manager.requestAlwaysAuthorization() 
     manager.requestWhenInUseAuthorization() 
     manager.startUpdatingLocation() 
    } else { 
     NSLog("location services failed") 
    } 


    //MARK: MKMapView 
    mapHole = MKMapView(frame: mapRect.textRect) 
    span = MKCoordinateSpanMake(0.001, 0.001) 
    GPSspot = manager.location.coordinate 
    region = MKCoordinateRegion(center: GPSspot, span: span) 
    mapHole.setRegion(region, animated: true) 
    mapHole.mapType = MKMapType.Satellite 
    mapHole.showsUserLocation = true 
    self.view.addSubview(mapHole) 
} 

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if annotation is MKUserLocation { 
     return nil 
    } 

    let reuseId = "test" 

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
    if anView == nil { 
     anView = MKAnnotationView (annotation: annotation, reuseIdentifier: reuseId) 
     anView.image = UIImage(named:"ballSmall.png") 
     anView.canShowCallout = true 
    } else { 
     anView.annotation = annotation 
    } 

    return anView 
} 

누군가가 도와 드릴까요? 고맙습니다! 나는이 일을하고 일주일 정도 책을 읽었으므로 나는 그것을 얻지 못했다.

답변

2

지도보기가 viewDidLoad에 프로그래밍 방식으로 만들어 질 때지도보기의 delegate이 설정되지 않기 때문에 viewForAnnotation 메서드가 호출되지 않습니다. 당신이 아직없는 경우

mapHole = MKMapView(frame: mapRect.textRect) 
mapHole.delegate = self      // <-- add this line 
span = MKCoordinateSpanMake(0.001, 0.001) 

는 또한, 당신이보기 컨트롤러가 MKMapViewDelegate 프로토콜을 구현하는 것을 선언해야

:

지도보기를 생성 한 후, 그 delegate 속성을 설정합니다. 지도보기에 delegate 속성을 설정하는 뷰 컨트롤러가 프로토콜을 구현하는 선언뿐만 아니라 수행해야한다는

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 

참고 : 예를 들면.


당신은 또한 효과가 없습니다, 거기에 속하지 않기 때문에 addStroke 방법에서 관련 코드보기를 제거하고 무의미합니다

annotation.title = "Stroke" 

/* This code doesn't belong here... 
let useID = "test" 

var myAnnotation = mapHole.dequeueReusableAnnotationViewWithIdentifier(useID) 
if (myAnnotation == nil) { 
    myAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: useID) 
    myAnnotation.image = UIImage(named: "ballSmall.png") 
    myAnnotation.canShowCallout = true 
    myAnnotation.enabled = true 
} else { 
    myAnnotation.annotation = annotation 
} 

mapHole.viewForAnnotation(annotation) 
*/ 

mapHole.addAnnotation(annotation) 


는 또한 viewDidLoad이 코드주의 :

GPSspot = manager.location.coordinate 
region = MKCoordinateRegion(center: GPSspot, span: span) 
mapHole.setRegion(region, animated: true) 
manager.location 여전히 S 그래서이 시점에서 nil을 할 수 있기 때문에

잠재적으로 충돌 할 수 o 전화는 startUpdatingLocation입니다. manager.locationnil이 아닌지 먼저 확인하거나 해당 코드를 didUpdateLocations 대리자 메서드로 옮기는 것이 안전합니다. 마지막 관한


:

... 전 그래서 아이폰 사용되고있는 기초 (수평 및 수직)의 크기를 전환 할 수있는 프로그래밍 MKMapView 추가. (나는 아직도이 엑스 코드에 직관적하지 않고 프로그래머 이 주위 프로그램이 믿을 수 없어 .)

참고 엑스 코드 당신이 쓸 수와 코드를 컴파일 할 수있는 IDE 단지입니다. 그것은 어떤 아이폰이 사용되고 있는지를 알 수있는 iOS SDK입니다. SDK는 화면 크기에 따라보기의 크기를 자동으로 조정할 수 있습니다.문서, WWDC 비디오, SO 또는 기타 리소스에서 "자동 레이아웃"및/또는 "크기 클래스"를 검색하십시오.

+0

감사합니다 !!!!! 나는 델리게이트를 추가하는 것을 놓쳤다. 고맙습니다! (다음 주 해결할 긴 문제를 해결할 예정입니다.) – Scuttle

관련 문제