gpt4 book ai didi

ios - 如何向 MKPointAnnotation 添加按钮并打开 map ?

转载 作者:行者123 更新时间:2023-11-28 08:49:38 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于ios - 如何向 MKPointAnnotation 添加按钮并打开 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34535861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com