Swift 2 在注释中添加标注
Swift 2 Adding callouts to annotations
我很难将标注添加到我的注释中。我已经删除了从下面的代码中添加标注的所有尝试。
在updateVisiblePins函数底部添加注解
import UIKit
import MapKit
import CoreLocation
import Foundation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
var landmarkToPass: String!
var rowIndex: Int!
struct MyVars {
static var prevLoc = CLLocation()
static var region = MKCoordinateRegion()
static var landmarks = [Landmark]()
static var landmark: Landmark = Landmark(title: String(), locationName: String(), discipline: String(), coordinate: CLLocationCoordinate2D())
}
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
var landmarks: [Landmark] = [Landmark]()
loadInitialData()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var prevLoc = MyVars.prevLoc
let userLoction: CLLocation = locations[0]
let distance = calculateDisatnceBetweenTwoLocations(prevLoc, destination: userLoction)
if prevLoc != userLoction {
prevLoc = userLoction
MyVars.prevLoc = userLoction
if distance > 5 {
let latitude = userLoction.coordinate.latitude
let longitude = userLoction.coordinate.longitude
let latDelta: CLLocationDegrees = 0.05
let lonDelta: CLLocationDegrees = 0.05
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
MyVars.region = MKCoordinateRegionMake(location, span)
self.mapView.showsUserLocation = true
self.mapView.setRegion(MyVars.region, animated: true)
updateVisiblePins()
} else {
let latitude = userLoction.coordinate.latitude
let longitude = userLoction.coordinate.longitude
let span = mapView.region.span
let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
MyVars.region = MKCoordinateRegionMake(location, span)
self.mapView.showsUserLocation = true
updateVisiblePins()
}
}
}
func calculateDisatnceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{
var distanceMeters = source.distanceFromLocation(destination)
var distanceKM = distanceMeters / 1000
return distanceKM
}
func updateVisiblePins() {
for (index, landmark) in MyVars.landmarks.enumerate() {
let landmarkLat = landmark.coordinate.latitude
let landmarkLon = landmark.coordinate.longitude
let userLocation = locationManager.location
let landmarkLocation = CLLocation(latitude: landmarkLat, longitude: landmarkLon)
let distance = calculateDisatnceBetweenTwoLocations(userLocation!, destination: landmarkLocation)
if distance < 30 {
mapView.addAnnotation(landmark)
} else {
if rowIndex != nil {
if index == rowIndex{
self.mapView.addAnnotation(landmark)
} else {
mapView.removeAnnotation(landmark)
}
}
}
}
}
func loadInitialData() {
// 1
let fileName = NSBundle.mainBundle().pathForResource("PublicLandmark", ofType: "json")
var data: NSData = NSData()
do {
data = try NSData(contentsOfFile: fileName!, options: [])
} catch {
}
// 2
var jsonObject: AnyObject!
do {
jsonObject = try NSJSONSerialization.JSONObjectWithData(data,
options: [])
} catch {
}
// 3
if let jsonObject = jsonObject as? [String: AnyObject],
// 4
let jsonData = JSONValue.fromObject(jsonObject)?["data"]?.array {
for landmarkJSON in jsonData {
if let landmarkJSON = landmarkJSON.array,landmark = Landmark.fromJSON(landmarkJSON) {
MyVars.landmarks.append(landmark)
}
}
}
}
}
需要将 mapView.delegat = self
行添加到 viewDidLoad()
函数。
我很难将标注添加到我的注释中。我已经删除了从下面的代码中添加标注的所有尝试。
在updateVisiblePins函数底部添加注解
import UIKit
import MapKit
import CoreLocation
import Foundation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
var landmarkToPass: String!
var rowIndex: Int!
struct MyVars {
static var prevLoc = CLLocation()
static var region = MKCoordinateRegion()
static var landmarks = [Landmark]()
static var landmark: Landmark = Landmark(title: String(), locationName: String(), discipline: String(), coordinate: CLLocationCoordinate2D())
}
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
var landmarks: [Landmark] = [Landmark]()
loadInitialData()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var prevLoc = MyVars.prevLoc
let userLoction: CLLocation = locations[0]
let distance = calculateDisatnceBetweenTwoLocations(prevLoc, destination: userLoction)
if prevLoc != userLoction {
prevLoc = userLoction
MyVars.prevLoc = userLoction
if distance > 5 {
let latitude = userLoction.coordinate.latitude
let longitude = userLoction.coordinate.longitude
let latDelta: CLLocationDegrees = 0.05
let lonDelta: CLLocationDegrees = 0.05
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
MyVars.region = MKCoordinateRegionMake(location, span)
self.mapView.showsUserLocation = true
self.mapView.setRegion(MyVars.region, animated: true)
updateVisiblePins()
} else {
let latitude = userLoction.coordinate.latitude
let longitude = userLoction.coordinate.longitude
let span = mapView.region.span
let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
MyVars.region = MKCoordinateRegionMake(location, span)
self.mapView.showsUserLocation = true
updateVisiblePins()
}
}
}
func calculateDisatnceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{
var distanceMeters = source.distanceFromLocation(destination)
var distanceKM = distanceMeters / 1000
return distanceKM
}
func updateVisiblePins() {
for (index, landmark) in MyVars.landmarks.enumerate() {
let landmarkLat = landmark.coordinate.latitude
let landmarkLon = landmark.coordinate.longitude
let userLocation = locationManager.location
let landmarkLocation = CLLocation(latitude: landmarkLat, longitude: landmarkLon)
let distance = calculateDisatnceBetweenTwoLocations(userLocation!, destination: landmarkLocation)
if distance < 30 {
mapView.addAnnotation(landmark)
} else {
if rowIndex != nil {
if index == rowIndex{
self.mapView.addAnnotation(landmark)
} else {
mapView.removeAnnotation(landmark)
}
}
}
}
}
func loadInitialData() {
// 1
let fileName = NSBundle.mainBundle().pathForResource("PublicLandmark", ofType: "json")
var data: NSData = NSData()
do {
data = try NSData(contentsOfFile: fileName!, options: [])
} catch {
}
// 2
var jsonObject: AnyObject!
do {
jsonObject = try NSJSONSerialization.JSONObjectWithData(data,
options: [])
} catch {
}
// 3
if let jsonObject = jsonObject as? [String: AnyObject],
// 4
let jsonData = JSONValue.fromObject(jsonObject)?["data"]?.array {
for landmarkJSON in jsonData {
if let landmarkJSON = landmarkJSON.array,landmark = Landmark.fromJSON(landmarkJSON) {
MyVars.landmarks.append(landmark)
}
}
}
}
}
需要将 mapView.delegat = self
行添加到 viewDidLoad()
函数。