2016-07-28 6 views
1

현재지도가 핀으로 표시된 5 개의 하드 코드 된 위치로 설정된지도보기가 있습니다. 사용자가이 핀을 클릭하면 위치에 대한 세부 정보가있는 콜 아웃이 사용자에게 표시됩니다. 이 콜 아웃 내에서 사용자가 누른 다음 다른보기 컨트롤러로 가져갈 수있는 버튼을 포함하려고합니다.iOS - Swift - MKPointAnnotaton에 버튼을 추가하는 방법 - MapKit

다음은 현재 코드입니다.하지만 아무 버튼도 표시되지 않은 경우 누구나 내가 누락되었거나 버튼을 구현하기 위해해야 ​​할 일이 무엇인지 알 수 있습니까?

위치 및 현재 사용자 위치에 대한 내 코드는 다음과 같습니다. 여기

import UIKit 
import MapKit 
import CoreLocation 
import SceneKit 

class SecondViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate 
{ 
    @IBOutlet weak var mapView: MKMapView! 

//Location manager property 
let locationManager = CLLocationManager() 

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    mapView.delegate = self 

    //Find the current location as soon as the view is loaded 
    self.locationManager.delegate = self 

    //Setting the accuracy 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 

    //So only location services are used within the app 
    self.locationManager.requestWhenInUseAuthorization() 

    //Start up the location manager and find current location 
    self.locationManager.startUpdatingLocation() 

    //Use blue pin to show the location to the user 
    self.mapView.showsUserLocation = true 

    //Array of dictionaries to store the locations 
    let locationData = [ 

     //Radio City Tower 
     ["name": "Radio City Tower", 
     "latitude": 53.4071551, 
     "longitude": -2.980798600000071], 

     //Metropolitian Cathedral 
     ["name": "Metropolitian Catherdral", 
     "latitude": 53.40494649999999, 
     "longitude": -2.9684022999999797], 

     //Walker Art Gallery 
     ["name": "Walker Art Gallery", 
      "latitude": 53.410068, 
      "longitude": -2.979666], 

     //Liver Buildings 
     ["name": "Liver Buildings", 
      "latitude": 53.405808, 
      "longitude": -2.995859], 

     //St George's Hall 
     ["name": "St George's Hall", 
      "latitude": 53.409277, 
      "longitude": -2.979946] 
    ] 

    //Object for each dictionary in the locations array 
    var annotations = [MKPointAnnotation]() 

    //Populating the map with the location pins 
    for Dictionary in locationData { 
     let latitude = CLLocationDegrees(Dictionary["latitude"] as! Double) 
     let longitude = CLLocationDegrees(Dictionary["longitude"] as! Double) 
     let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 
     let name = Dictionary["name"] as! String 
     let annotation = MKPointAnnotation() 

     //Apply to annotation 
     annotation.coordinate = coordinate 
     annotation.title = "\(name)" 
     annotations.append(annotation) 


    } 

    mapView.addAnnotations(annotations) 

} 

override func didReceiveMemoryWarning() 
{ 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Location Delegate Methods 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
{ 
    //Gets the most current location 
    let location = locations.last 

    //gets the center of the last location 
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 

    //Creatng a region - Map will zoom to this 
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: Double(0.004), longitudeDelta: Double(0.004))) 

    //Apply the map view to the region 
    self.mapView.setRegion(region, animated: true) 

    //Stop updating the location 
    self.locationManager.stopUpdatingLocation() 

    let currentLocation = CLLocationCoordinate2DMake(location!.coordinate.latitude, location!.coordinate.longitude) 

    //Adding an annotation to current user location 
    var pinAnnotation = MKPointAnnotation() 

    //Set pin coordinate to the users current location 
    pinAnnotation.coordinate = currentLocation 

    //Annotation title 
    pinAnnotation.title = "Current Location" 

    //Annotation subtitle 
    pinAnnotation.subtitle = "You are here!" 

    //Add the pin to the mapView 
    mapView.addAnnotation(pinAnnotation) 

} 

그리고

는 아마 정말 간단 뭔가를 놓친 거지하지만 확실하지 오전

func MapView(mapview: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryTapped control: UIControl!) { 
    if control == view.rightCalloutAccessoryView { 
     print(view.annotation?.title) 
     print(view.annotation?.subtitle) 

     //Perform segue to navigate to viewcontroller 
    } 
} 

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

    let reuseID = "pin" 
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView 

    if(pinView == nil) { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID) 
     pinView!.canShowCallout = true 
     pinView!.animatesDrop = true 
    } 

    var button = UIButton(type: .DetailDisclosure) as UIButton 

    pinView?.rightCalloutAccessoryView = button 

    return pinView 

} 

을 (작동하지 않습니다) 콜 아웃 내에서 버튼 구현을위한 코드입니다. 나는 iOS/Swift 개발과 MapKit을 처음 접해 보았으니 나와 함께 견뎌주십시오. 나는 또한이 질문이 여러 번 질문되었지만 아직도 해결할 수 없다는 것을 이해합니다. 어떤 도움이라도 고맙습니다. 고마워요.

+0

확인이 관련 포스트 밖으로 ...> http://stackoverflow.com/questions/28225296/how-to-add-a-button-to-mkpointannotation – HowsThatMonkey

+0

안녕, 난 이미이 게시물을 사용하고 통합했다 코드를 내 응용 프로그램에 추가하고 필요한 변경 작업을 수행했습니다. 감사의 말에 감사드립니다. – starrybolt

답변

0

MFCnnotationView 명시 적으로 래핑되지 않은 다음 설명 선 메서드에서 작은 실수를하는 중입니다!MKMapView!. 제가 테스트 한 교정 된 것이 있습니다.

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    print("viewForAnnotation") 
    if(annotation is MKUserLocation) { 
     return nil 
    } 
    let reuseID = "pin" 
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView 
    if(pinView == nil) { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID) 
     pinView!.canShowCallout = true 
     pinView!.animatesDrop = true 
    } 
    let button = UIButton() 
    button.frame = CGRect(x: 0, y: 0, width: 30, height: 30) 
    let image = UIImage(named: "img.png") 
    button.setImage(image, forState: .Normal) 
    pinView?.rightCalloutAccessoryView = button 
    return pinView 
} 
+0

안녕하세요, 도움을 주셔서 감사합니다. 나는 당신이 당신의 포스트에서 진술 한 변경을 만들었지 만 그것이 여전히 작동하지 않는다, 그것은 단지 콜 아웃과 정보만을 보여 주며, 버튼은 없다. 내가 잘못 가고있는 것이 있습니까? – starrybolt

+0

안녕하세요, 지금이 문제가 해결되어서 마지막 코멘트를 무시하십시오. 도와 주셔서 대단히 감사합니다. 나는이 문제를 해결하기 위해 2 일을 보냈습니다. – starrybolt

+0

어떻게 해결 했습니까? – Burf2000

관련 문제