如何向MKPointAnnotation添加按钮并打开地图?

时间:2015-12-30 19:24:10

标签: ios swift annotations maps mapkit

我必须为我的特定入口点添加一个按钮,我点击此按钮打开已经传递所选点坐标的地图应用程序。有谁知道怎么办?我的代码就是这个。图像就是我所拥有的。

enter image description here

我的代码:

import Foundation
import UIKit
import MapKit
import CoreLocation

class PuntiVenditaController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var menuButton:UIBarButtonItem!
@IBOutlet weak var mapView: MKMapView!
let regionRadius: CLLocationDistance = 1000
let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    if revealViewController() != nil {
        menuButton.target = revealViewController()
        menuButton.action = "revealToggle:"
        view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }

    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    let location = CLLocationCoordinate2D(latitude: 41.225891, longitude: 16.291489)

    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegion(center: location, span: span)
    mapView.setRegion(region, animated: true)
    mapView.showsPointsOfInterest = false
    mapView.showsUserLocation = true
    displayMarkers()

}

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

func centerMapOnLocation(location: CLLocation) {
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
        regionRadius * 2.0, regionRadius * 2.0)
    mapView.setRegion(coordinateRegion, animated: true)
}

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if control == view.rightCalloutAccessoryView{
        println(view.annotation.title) // annotation's title
        println(view.annotation.subtitle) // annotation's subttitle

        //Perform a segue here to navigate to another viewcontroller
        // On tapping the disclosure button you will get here
    }
}

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

    println("viewForannotation")
    if annotation is MKUserLocation {
        //return nil
        return nil
    }

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

    if pinView == nil {
        //println("Pinview was nil")
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
    }

    var button = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton // button with info sign in it

    pinView?.rightCalloutAccessoryView = button


    return pinView
}

func displayMarkers() -> Void
{
    let annotationView = MKAnnotationView()
    // Adding button here wont do anything  so remove these two lines
    let detailButton: UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton
    annotationView.rightCalloutAccessoryView = detailButton

    // For adding button we have to use a method named as viewForAnnotation
    let annotation = MKPointAnnotation()
    let name = "Title"
    let latitude = 41.225891
    let longitude = 16.291489
    annotation.title = name
    var markerLatitude: Double = latitude
    var markerLongitude: Double = longitude
    let location = CLLocationCoordinate2D(latitude: markerLatitude, longitude: markerLongitude)
    annotation.coordinate = location
    annotation.subtitle = "Subtitle"
    mapView.addAnnotation(annotation)
 }
}

1 个答案:

答案 0 :(得分:0)

您可以打开一个URL,在特定的点/地址打开Maps App: https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

但我不认为,您可以在地图应用程序中设计自定义标记