MapKit中没有显示标签

问题描述 投票:-1回答:2

有人能告诉我为什么我的标签没有显示。我正在通过一个带坐标的数组运行一个循环。它显示我的3个针脚,其中1个是绿色,两个蓝色是我想要的但是我的标签没有显示,任何想法?

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var maps: MKMapView!
    var locationManager = CLLocationManager()
    var flag = true

    override func viewDidLoad() {
        super.viewDidLoad()
        let CoordinatesArray = ["blah,-blah, 11:45","blah, -blah,00:00", "blah,-blah, 12:45"];
        self.maps.delegate = self
        sendPoints(CoordinatesArray);

    }

    func sendPoints(array:[String]){
        let latDelta:CLLocationDegrees = 0.015 //difference of lats from one side of screen to another
        let longDelta:CLLocationDegrees = 0.015 //difference of lats from one side of screen to another
        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)

        for (var i=0;i<array.count;i++){

            var separateComma = array[i].componentsSeparatedByString(",")
            var location:CLLocationCoordinate2D

            if(flag){
                location = CLLocationCoordinate2DMake(separateComma[0].doubleValue,separateComma[1].doubleValue)
                let length:CLLocationDistance = 200
                let cir:MKCircle = MKCircle(centerCoordinate: location, radius: length)
                maps.addOverlay(cir)


            }else{
                location = CLLocationCoordinate2DMake(separateComma[0].doubleValue,separateComma[1].doubleValue)

            }

            let point = MKPointAnnotation()
            point.title = "Home"
            point.subtitle = "time for home"
            point.coordinate = location
            maps.addAnnotation(point)
            maps.selectAnnotation(point, animated: true)
            maps.setRegion(MKCoordinateRegionMake(point.coordinate, MKCoordinateSpanMake(latDelta,longDelta)), animated: true)

        }
    }


    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
        let overlayRenderer : MKCircleRenderer = MKCircleRenderer(overlay: overlay);
        overlayRenderer.lineWidth = 150
        overlayRenderer.fillColor = UIColor.blueColor()

        overlayRenderer.alpha = 0.15

        return overlayRenderer
    }

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

        if (annotation.isKindOfClass(MKUserLocation)){
            return nil
        }
        var myPin = mapView.dequeueReusableAnnotationViewWithIdentifier("MyIdentifier") as? MKPinAnnotationView
        if myPin != nil {
            return myPin
        }

        myPin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "MyIdentifier")
        if(flag){
            myPin?.pinTintColor = UIColor.greenColor()
        }else{
            myPin?.pinTintColor = UIColor.blueColor()
        }
        flag = false;
        return myPin
    }

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

extension String {
    var doubleValue: Double {
        return (self as NSString).doubleValue
    }
}
ios swift mapkit
2个回答
0
投票

你必须设置canShowCallout属性。

    myPin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "MyIdentifier")
    myPin?.canShowCallout = true

enter image description here


0
投票

在我的情况下,我在被叫之前委托地图。

所以...那是我面临的错误。

self.m_map = MKMapView()
self.m_map?.delegate = self

希望它有助于某人:-)

© www.soinside.com 2019 - 2024. All rights reserved.