2016-07-28 9 views
1

現在、私はピンと地図上に表示されている5つのハードコードの位置を設定し、マップビューを持っています。ユーザーがこれらのピンをクリックすると、位置の詳細を示すコールアウトがユーザーに表示されます。これらの吹き出し内では、ユーザーが押すことができるボタンを含めようとしていて、別のビューコントローラーにそれらを移動します。以下はのiOS - スウィフト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

あなたは、あなたが明示的にMKAnnotationViewをアンラップされ、次のコールアウト方式で小さなミスをしています!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

関連する問題