2015-09-27 81 views
-1

有人可以让我知道为什么我的标签没有显示。我正在运行一个循环,通过一个坐标在其中的数组。它显示我的3个引脚,1是绿色,两个蓝色,这是我想要的,但我的标签没有显示,有什么想法?没有标签显示在MapKit

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 
    } 
} 
+0

有没有“标签”,在你的代码,那么究竟你要问什么? – matt

+0

point.title和point.subtitle应显示标签? – Jack

+0

不,他们设置标注的标注,但他们不会奇迹般地出现。用户必须点击注释。 – matt

回答

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 

希望它可以帮助别人:-)