for result in swiftyData.arrayValue {
let classId = result[0].stringValue
let className = result[1].stringValue
let longitude = result[2].doubleValue
let latitude = result[3].doubleValue
let newClass = NearestClasses()
newClass.classId = classId
newClass.className = className
newClass.longitude = longitude
newClass.latitude = latitude
nearestClasses?.append(newClass)
let locationSec = CLLocationCoordinate2DMake(CLLocationDegrees(latitude), CLLocationDegrees(longitude))
dropPin.coordinate = newYorkLocation
dropPin.title = className
self.mapeView.addAnnotation(dropPin)*/
let dropPin = MKPointAnnotation()
dropPin.coordinate = locationSec
dropPin.title = className
self.mapeView.addAnnotation(dropPin)
}
如果它是一个循环,这个函数在swift 3中完美地工作。但是如果它的多个循环,地图上没有显示标记。谢谢
答案 0 :(得分:1)
在Swift 3中
@IBOutlet weak var mapView: MKMapView!
var latitude:Double!
var longitude:Double!
var locationManager = CLLocationManager()
var green:Bool = false
var red:Bool = false
var yellow:Bool = false
var blue:Bool = false
var darkBlue:Bool = false
var liteGreen:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
if (CLLocationManager.locationServicesEnabled())
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if ((UIDevice.current.systemVersion as NSString).floatValue >= 8)
{
locationManager.requestWhenInUseAuthorization()
}
locationManager.startUpdatingLocation()
}
else
{
#if debug
println("Location services are not enabled");
#endif
}
addAnnotationPoint()
}
添加注释功能
func addAnnotationPoint()
{
for data in yourArray
{
let point = MKPointAnnotation()
let pointlatitude = Double(data.latitude!)!
let pointlongitude = Double(data.longitude!)!
point.title = data.event_name
point.coordinate = CLLocationCoordinate2DMake(pointlatitude ,pointlongitude)
//print(point.coordinate)
mapView.addAnnotation(point)
}
let span = MKCoordinateSpanMake(0.075, 0.075)
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), span: span)
mapView.setRegion(region, animated: true)
}
注释视图方法
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !(annotation is MKUserLocation) else {
return nil
}
// Better to make this class property
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
if let annotationView = annotationView {
// annotationView.canShowCallout = true
if blue == true
{
blue = false
green = true
yellow = false
red = false
darkBlue = false
liteGreen = false
annotationView.image = imageLiteral(resourceName: "Bluepin")
}
else if green == true
{
blue = false
green = false
darkBlue = false
liteGreen = false
yellow = true
red = false
annotationView.image = imageLiteral(resourceName: "Greenpin")
}
else if yellow == true
{
blue = false
green = false
yellow = false
darkBlue = false
liteGreen = false
red = true
annotationView.image = imageLiteral(resourceName: "Yellopin")
}
else if red == true
{
blue = false
green = false
yellow = false
darkBlue = true
liteGreen = false
red = false
annotationView.image = imageLiteral(resourceName: "Redpin")
}
else if darkBlue == true
{
blue = false
green = false
yellow = false
darkBlue = false
liteGreen = true
red = false
annotationView.image = imageLiteral(resourceName: "DarkBlue")
}
else if liteGreen == true
{
blue = true
green = false
yellow = false
darkBlue = false
liteGreen = false
red = false
annotationView.image = imageLiteral(resourceName: "LiteGreen")
}
else
{
blue = true
green = false
yellow = false
red = false
annotationView.image = imageLiteral(resourceName: "Pinkpin")
}
}
return annotationView
}