【问题标题】:How can I add a button to MKPointAnnotation and open Maps?如何向 MKPointAnnotation 添加按钮并打开地图?
【发布时间】:2015-12-30 19:24:09
【问题描述】:

我必须为我的特定入口点添加一个按钮,单击该按钮以打开已经传递所选点坐标的地图应用程序。有谁知道该怎么做?我的代码是这样的。图片就是我想要的。

我的代码:

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)
 }
}

【问题讨论】:

标签: ios swift annotations maps mapkit


【解决方案1】:

您可以打开一个 URL 以在特定点/地址打开地图应用程序: https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

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

【讨论】:

    猜你喜欢
    • 2015-03-29
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2023-01-09
    相关资源
    最近更新 更多