1)我需要设置一个自定义注释,具体取决于地图上的缩放比例,它们将被放置在地图上的整个轨迹中。 (箭头显示方向)。
我的代码
class customPin: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
self.title = pinTitle
self.subtitle = pinSubTitle
self.coordinate = location
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
annotationView.image = UIImage(named:"mov_on_2")
annotationView.canShowCallout = true
return annotationView
}
答案 0 :(得分:0)
我发现最好的方法是确定地图显示的屏幕宽度的距离。您可以像使用地图regionDidChangeAnimated
一样使用mapView的跨度来做到这一点:
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// Get the span and the center coordinate of the mapview
let span = mapView.region.span
let center = mapView.centerCoordinate
// Create two points on the left and right side of the region
let rightSide = CLLocationCoordinate2D(latitude: center.latitude + (span.latitudeDelta / 2), longitude: center.longitude)
let leftSide = CLLocationCoordinate2D(latitude: center.latitude - (span.latitudeDelta / 2), longitude: center.longitude)
// Calculate the distance between these two points (don't forget to convert to meters!)
let distance = calculateDistanceBetween(firstLoc: leftSide, secondGeoPoint: rightSide) * 1609.34
// Switch case the distance to handle zooming logic
switch distance {
case _ where distance < 1000:
// Handle logic for if the on screen width distance is < 1000 meters
case _ where distance < 5000:
// Handle logic for if the on screen width distance is < 5000 meters
default:
// Handle logic for if the on screen width distance is > 5000 meters
}
}
可以使用以下方法找到任意两个GPS坐标之间的距离
// Calculates the distance between two locations, returned in miles
func calculateDistanceBetween(firstLoc: CLLocationCoordinate2D, secondLoc: CLLocationCoordinate2D) -> Double {
// Convert lat's and long's into radians
let firstLatR = firstLoc.latitude * Double.pi / 180
let firstLongR = firstLoc.longitude * Double.pi / 180
let secondLatR = secondLoc.latitude * Double.pi / 180
let secondLongR = secondLoc.longitude * Double.pi / 180
// Calculate and return the distance
return 2 * 3963.1 * asin((pow(sin((secondLatR - firstLatR) / 2), 2) + cos(firstLatR) * cos(secondLatR) * pow(sin((secondLongR - firstLongR) / 2), 2)).squareRoot())
}
值得注意的是,以上等式假设地球是一个完美的球体。对于您的应用程序,这应该足够准确。 为地图找到真正的“缩放”级别的问题是屏幕宽度(度经距离)changes depending on your latitude上的地图距离。