我目前正在尝试构建一种方法来计算我当前位置和其他硬编码位置之间的英尺距离。我最终会将这些距离显示为表格视图中的字符串。这些是我正在采取的步骤:
1)我有一个硬编码的字典库,每个字典都有一个“纬度”和“经度”键值对。
2)从结构中提取此信息,如下所示:
struct BarDetials {
// other properties
....
var lat: CLLocationDegrees?
var long CLLocationDegrees?
init(index: Int) {
// initialize other properties
// ...
lat = specificBarDetail["latitude"] as! CLLocationDegrees!
long = specificBarDetail["longitude"] as! CLLocationDegrees!
}
}
3)我使用另一个结构从这些坐标创建一个 CLLocation 实例数组,如下所示:
struct ConstructLocationsToCompare {
var latitude : [CLLocationDegrees?] = []
var longitude : [CLLocationDegrees?] = []
var barLocations : [CLLocation?] = []
init(){
for index in 0...21 {
var data = BarDetails(index: index)
if data.lat != nil {
latitude.append(data.lat!)
}
if data.long != nil {
longitude.append(data.long!)
}
barLocations[index] = CLLocation(latitude: latitude[index]!, longitude: longitude[index]!)
}
}
}
4) 然后我在我的 MasterViewController() 中计算距离。
var latestLocation : AnyObject? , var currentLocation : CLLocation! 和 var distanceStringArray : [String?] = [] 都是我的 MasterViewController() 类的属性。
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
// Get latest location and store it as a property of MasterViewController
if self.latestLocation != nil {
self.latestLocation = locations[locations.count - 1]
// Print out the latest locaiton for debuging
println("Latest Location")
println("---------------")
println(self.latestLocation)
// Create an instance of ContructLocationsToCompare to get all locaiton data
var getBarLocations = ConstructLocationsToCompare()
// Get loop to calculate the distance you are away from each Bar
var distanceDoubleArray : [Double?] = []
for index in 0...21 {
var distanceBetween : CLLocationDistance = self.latestLocation!.distanceFromLocation(getBarLocations.barLocations[index])
var distanceInFeet = distanceBetween * 3.28084
distanceDoubleArray[index] = distanceInFeet
self.distancesStringArray.append( String(format: "%6.0 ft", distanceDoubleArray[index]!))
}
}
//println(distanceBetween) or get error if it exists
if error != nil {
println("Error: " + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
}
})
}
5)最后,显示我与每个位置的距离(在我的 -cellForRowAtIndexPath 中):
println("")
println("")
println("List of Locations")
println("=================")
println(self.distancesStringArray)
println("")
println("")
println("")
println("")
if self.distancesStringArray[indexPath.row]!.isEmpty == true{
futureCell.distanceAway?.text = "-" // I crash at this line ^^^^^^
} else {
futureCell.distanceAway?.text = self.distancesStringArray[indexPath.row]
}
*** 我的 distanceStringArray 始终为空,因此出现致命错误并且应用程序崩溃。我该如何解决?是我声明 var latestLocation : AnyObject 的事实吗?, var currentLocation : CLLocation! 和 var distanceStringArray : [String?] = [] 我的 MasterViewController() 类的属性不好的做法?如果不是,这些属性是否需要转换为不同的类型/以不同的方式声明?