我正在尝试制作一个包含地图和一些固定注释的简单应用。我想点击那些注释,他们的按钮,并获得一个带有图片的菜单和一些其他文本,如Apple Maps点的工作方式。
到目前为止,这是我的代码,我可以一直到DetailDisclosure按钮,但是当我点击它时我无法以这种方式工作。有人能帮我吗?提前谢谢。
(还有一个名为Shops
的课程,但我没有参加此帖子)
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIPopoverPresentationControllerDelegate{
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var mapItemData:MKMapItem!
override func viewDidLoad() {
super.viewDidLoad()
// Initiate GPS
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
//Pin coordinates
mapView.delegate = self
let J = Shops(title: "Jingle", coordinate: CLLocationCoordinate2D(latitude: 44.631076, longitude: 22.946770), info: "H", address:"Π", subtitle:"κ")
let K = Shops(title: "R", coordinate: CLLocationCoordinate2D(latitude: 43.65, longitude: 6.43), info: "F", address:"Π", subtitle:"κ")
let L = Shops(title: "P", coordinate: CLLocationCoordinate2D(latitude: 58.89, longitude: 2.3508), info: "O", address:"Π", subtitle:"κ")
let B = Shops(title: "R", coordinate: CLLocationCoordinate2D(latitude: 26, longitude: 17), info: "H", address:"Π", subtitle:"κ")
let W = Shops(title: "W", coordinate: CLLocationCoordinate2D(latitude: 88.43111, longitude: -37.035663), info: "N", address:"Π", subtitle:"κ")
mapView.addAnnotations([J, K, L, B, W])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Locate through GPS
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "Shops"
if annotation.isKindOfClass(Shops.self) {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
let btn = UIButton(type: .InfoLight)
annotationView!.rightCalloutAccessoryView = btn
}else{
annotationView!.annotation = annotation
}
return annotationView
}
return nil
}
//Zoom out to enclose all annotations
func fitMapViewToAnnotaionList(annotations: [MKPointAnnotation]) -> Void {
let mapEdgePadding = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
var zoomRect:MKMapRect = MKMapRectNull
for index in 0..<annotations.count {
let annotation = annotations[index]
let aPoint:MKMapPoint = MKMapPointForCoordinate(annotation.coordinate)
let rect:MKMapRect = MKMapRectMake(aPoint.x, aPoint.y, 0.1, 0.1)
if MKMapRectIsNull(zoomRect) {
zoomRect = rect
} else {
zoomRect = MKMapRectUnion(zoomRect, rect)
}
}
mapView.setVisibleMapRect(zoomRect, edgePadding: mapEdgePadding, animated: true)
}
}