- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在复制我的问题之前,请阅读整篇文章。我在我的应用程序中使用 MkMapKit,现在我必须在缩小 map 时显示集群中的人,到目前为止,我已经使用 from this answer 实现了对他们进行编号。使用 Apple 的默认集群类。现在我不知道如何添加并显示它们所有一个圆圈,我知道它应该与半径相关但我不知道我该怎么做,在下面分享我的代码,我希望有任何帮助赞赏。谢谢还展示了我所做的事情的图片:
这是我的UserAnnotationClass
class UserAnnotation: NSObject, MKAnnotation {
let title: String?
let locationName: String
let discipline: String
let coordinate: CLLocationCoordinate2D
let userProfile: UserProfile!
let index: Int!
let memberAnnotations: [UserProfile]!
init(userProfile: UserProfile, at index: Int) {
self.title = userProfile.fullName
self.locationName = (userProfile.locationAddress != nil) ? userProfile.locationAddress : ""
let userProfilePicture: String = (userProfile.profilePicture == nil || userProfile.profilePicture == "") ? "" : userProfile.profilePicture
self.discipline = userProfilePicture
// print("\(userProfile.fullName) \(userProfile.location.dist)")
if (userProfile.isMapVisibility == true) {
self.coordinate = CLLocationCoordinate2D(latitude: userProfile.location.lat, longitude: userProfile.location.lon)
} else {
self.coordinate = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
}
memberAnnotations = [UserProfile]()
memberAnnotations.append(userProfile)
self.userProfile = userProfile
self.index = index
super.init()
}
var subtitle: String? {
return locationName
}
// pinTintColor for disciplines: Sculpture, Plaque, Mural, Monument, other
var markerTintColor: UIColor {
switch discipline {
case "Monument":
return .red
case "Mural":
return .cyan
case "Plaque":
return .blue
case "Sculpture":
return .purple
default:
return .clear
}
}
// Annotation right callout accessory opens this mapItem in Maps app
func mapItem() -> MKMapItem {
let addressDict = [CNPostalAddressStreetKey: subtitle!]
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDict)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = title
return mapItem
}
}
这是我用来使它们聚类的 CLusterViewClass。
class ClusterView: MKAnnotationView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, with: event)
if (hitView != nil)
{
if (hitView?.isKind(of: UIButton.self))! {
let sender: UIButton = hitView as! UIButton
sender.sendActions(for: .touchUpInside)
}
else {
self.superview?.bringSubviewToFront(self)
}
}
return hitView
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let rect = self.bounds
var isInside: Bool = rect.contains(point)
if(!isInside)
{
for view in self.subviews
{
isInside = view.frame.contains(point)
if isInside
{
break
}
}
}
return isInside
}
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
displayPriority = .defaultHigh
collisionMode = .circle
centerOffset = CGPoint(x: 0, y: -10) // Offset center point to animate better with marker annotations
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? {
willSet {
canShowCallout = false
if let cluster = newValue as? UserAnnotation {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 40, height: 40))
let count = cluster.memberAnnotations.count
let uniCount = cluster.memberAnnotations.filter { member -> Bool in
//Log("Bool \(member) , \(member.isMapVisibility == false) 💚")
return member.isMapVisibility == true
}.count
//Log("COUNTS \(count) , \(uniCount) ❤️")
image = renderer.image { _ in
// Fill full circle with tricycle color
if uniCount > 0 {
AppTheme.blueColor.setFill()
UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 40, height: 40)).fill()
// Fill inner circle with white color
UIColor.white.setFill()
UIBezierPath(ovalIn: CGRect(x: 8, y: 8, width: 24, height: 24)).fill()
// Finally draw count text vertically and horizontally centered
let attributes = [ NSAttributedString.Key.foregroundColor: UIColor.black,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)]
//let text = "\(count)"
let text = "4"
let size = text.size(withAttributes: attributes)
let rect = CGRect(x: 20 - size.width / 2, y: 20 - size.height / 2, width: size.width, height: size.height)
text.draw(in: rect, withAttributes: attributes)
}
}
}
}
}
}
这些是我的一些 MapKit 函数
extension FeedsViewController: MKMapViewDelegate {
// 1
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annotation = annotation as? UserAnnotation else { return nil }
// 2
let identifier = "marker"
if #available(iOS 11.0, *) {
var view: ClusterView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? ClusterView { // 3
dequeuedView.annotation = annotation
view = dequeuedView
} else {
// 4
view = ClusterView(annotation: annotation, reuseIdentifier: identifier)
}
return view
} else {
// Fallback on earlier versions
return nil
}
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// let zoomWidth = mapView.visibleMapRect.size.width
// let zoomFactor = Int(log2(zoomWidth))
// print("...REGION DID CHANGE: ZOOM FACTOR \(zoomFactor)")
let centralLocation = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)
Log("💚 Radius - \(self.getRadius(centralLocation: centralLocation))")
}
func getRadius(centralLocation: CLLocation) -> Double{
let topCentralLat:Double = centralLocation.coordinate.latitude - mapView.region.span.latitudeDelta/2
let topCentralLocation = CLLocation(latitude: topCentralLat, longitude: centralLocation.coordinate.longitude)
let radius = centralLocation.distance(from: topCentralLocation)
return radius / 1000.0 // to convert radius to meters
}
func mapView(_ mapView: MKMapView,
didSelect view: MKAnnotationView)
{
// 1
if view.annotation is MKUserLocation
{
// Don't proceed with custom callout
return
}
// 2
let annotation = view.annotation as! UserAnnotation
let detailAnnotationView: UserDetailAnnotationView = UserDetailAnnotationView(frame: CGRect(x: 0, y: 0, width: 320, height: 74))
let url = (annotation.discipline == "") ? nil : URL(string: annotation.discipline)!
let range = 0.0..<0.9
if annotation.userProfile.location.dist != nil {
if range.contains(annotation.userProfile.location.dist) {
let kMeters = Measurement(value: annotation.userProfile.location.dist, unit: UnitLength.kilometers)
let meters = kMeters.converted(to: UnitLength.meters)
detailAnnotationView.distancelbl.text = "\(String(describing: round(Double(meters.value)))) m Away"
} else {
detailAnnotationView.distancelbl.text = "\(String(describing: round(annotation.userProfile.location.dist))) Km Away"
}
}
detailAnnotationView.set(Title: annotation.title!, imageUrl: url) { [weak self] (sender) in
guard let self = self else { return }
if self.isOpenChat {
self.isOpenChat = false
if annotation.userProfile.channel != "" {
self.appDelegate.pubNubAddPushNotifications([annotation.userProfile.channel]) { (status) in
print(status.description)
}
let chatViewController: ChatViewController = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
chatViewController.userProfile = annotation.userProfile
chatViewController.loginUserProfile = self.loginUserProfile
self.navigationController?.pushViewController(chatViewController, animated: true)
}
}
}
detailAnnotationView.center = CGPoint(x: view.bounds.size.width / 2, y: -detailAnnotationView.bounds.size.height*0.52)
view.addSubview(detailAnnotationView)
mapView.setCenter((view.annotation?.coordinate)!, animated: true)
// let calloutView = views?[0] as! CustomCalloutView
// calloutView.starbucksName.text = starbucksAnnotation.name
// calloutView.starbucksAddress.text = starbucksAnnotation.address
// calloutView.starbucksPhone.text = starbucksAnnotation.phone
// calloutView.starbucksImage.image = starbucksAnnotation.image
// let button = UIButton(frame: calloutView.starbucksPhone.frame)
// button.addTarget(self, action: #selector(ViewController.callPhoneNumber(sender:)), for: .touchUpInside)
// calloutView.addSubview(button)
// // 3
// calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
// view.addSubview(calloutView)
// mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
if #available(iOS 11.0, *) {
if view.isKind(of: ClusterView.self)
{
for subview in view.subviews
{
subview.removeFromSuperview()
}
}
} else {
// Fallback on earlier versions
}
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
// let location = view.annotation as! UserAnnotation
// let launchOptions = [MKLaunchOptionsDirectionsModeKey:
// MKLaunchOptionsDirectionsModeDriving]
// location.mapItem().openInMaps(launchOptions: launchOptions)
}
}
这就是我设置 mapView 的方式....
fileprivate func setupMapsLayout() {
if self.userAnnotationList.count > 0 {
// HereMap
self.mapView.removeAnnotations(self.userAnnotationList)
}
self.mapView.delegate = self
// mapView.register(ArtworkMarkerView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
if #available(iOS 11.0, *) {
// HereMap
//self.mapView.register(UserAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(ClusterView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
} else {
// Fallback on earlier versions
}
self.loadInitialData()
self.mapView.addAnnotations(self.userAnnotationList)
//self.mapView.topCenterCoordinate()
}
最佳答案
好的,iOS 11 及更高版本的解决方案相当简单。您有两个注释 View ,一个用于您自己的注释,一个用于注释集群。您的主注释 View 只需在初始化时以及 annotation
属性更改时指定 clusteringIdentifier
:
class UserAnnotationView: MKMarkerAnnotationView {
static let preferredClusteringIdentifier = Bundle.main.bundleIdentifier! + ".UserAnnotationView"
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
clusteringIdentifier = UserAnnotationView.preferredClusteringIdentifier
collisionMode = .circle
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? {
willSet {
clusteringIdentifier = UserAnnotationView.preferredClusteringIdentifier
}
}
}
并且您的集群注释 View 应该在其 annotation
属性更新时更新其图像:
class UserClusterAnnotationView: MKAnnotationView {
static let preferredClusteringIdentifier = Bundle.main.bundleIdentifier! + ".UserClusterAnnotationView"
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
collisionMode = .circle
updateImage()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? { didSet { updateImage() } }
private func updateImage() {
if let clusterAnnotation = annotation as? MKClusterAnnotation {
self.image = image(count: clusterAnnotation.memberAnnotations.count)
} else {
self.image = image(count: 1)
}
}
func image(count: Int) -> UIImage {
let bounds = CGRect(origin: .zero, size: CGSize(width: 40, height: 40))
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { _ in
// Fill full circle with tricycle color
AppTheme.blueColor.setFill()
UIBezierPath(ovalIn: bounds).fill()
// Fill inner circle with white color
UIColor.white.setFill()
UIBezierPath(ovalIn: bounds.insetBy(dx: 8, dy: 8)).fill()
// Finally draw count text vertically and horizontally centered
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.black,
.font: UIFont.boldSystemFont(ofSize: 20)
]
let text = "\(count)"
let size = text.size(withAttributes: attributes)
let origin = CGPoint(x: bounds.midX - size.width / 2, y: bounds.midY - size.height / 2)
let rect = CGRect(origin: origin, size: size)
text.draw(in: rect, withAttributes: attributes)
}
}
}
然后,您所要做的就是注册您的类(class):
mapView.register(UserAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(UserClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
不需要(也不需要)mapView(_:viewFor:)
实现。但是上面的结果(在缩小和返回时显示默认动画):
现在,显然,您可以随意修改 UserAnnotationView
。 (您的问题没有说明标准的单用户注释 View 是什么样的)。但是通过设置它的 clusteringIdentifier
并注册一个 MKMapViewDefaultClusterAnnotationViewReuseIdentifier
,您可以在 iOS 11 及更高版本中相当轻松地进行集群。
如果你真的想让集群注释 View 看起来像标准注释 View ,你可以为两者注册相同的注释 View 类:
mapView.register(UserClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(UserClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
但是您必须为集群注释 View 提供我们之前为标准注释 View 提供的相同 clusteringIdentifier:
class UserClusterAnnotationView: MKAnnotationView {
static let preferredClusteringIdentifier = Bundle.main.bundleIdentifier! + ".UserClusterAnnotationView"
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
clusteringIdentifier = UserClusterAnnotationView.preferredClusteringIdentifier
collisionMode = .circle
updateImage()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? {
didSet {
clusteringIdentifier = UserClusterAnnotationView.preferredClusteringIdentifier
updateImage()
}
}
private func updateImage() {
if let clusterAnnotation = annotation as? MKClusterAnnotation {
self.image = image(count: clusterAnnotation.memberAnnotations.count)
} else {
self.image = image(count: 1)
}
}
func image(count: Int) -> UIImage {
let bounds = CGRect(origin: .zero, size: CGSize(width: 40, height: 40))
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { _ in
// Fill full circle with tricycle color
AppTheme.blueColor.setFill()
UIBezierPath(ovalIn: bounds).fill()
// Fill inner circle with white color
UIColor.white.setFill()
UIBezierPath(ovalIn: bounds.insetBy(dx: 8, dy: 8)).fill()
// Finally draw count text vertically and horizontally centered
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.black,
.font: UIFont.boldSystemFont(ofSize: 20)
]
let text = "\(count)"
let size = text.size(withAttributes: attributes)
let origin = CGPoint(x: bounds.midX - size.width / 2, y: bounds.midY - size.height / 2)
let rect = CGRect(origin: origin, size: size)
text.draw(in: rect, withAttributes: attributes)
}
}
}
产生:
就我个人而言,我认为这有点令人困惑,但如果这是您的目标,那么这是实现它的一种方式。
现在,如果您确实需要支持 11 之前的 iOS 版本并且想要集群,那么您必须自己完成所有这些集群逻辑(或寻找第三方库来完成)。 Apple 在 WWDC 2011 中展示了如何做到这一点 Visualizing Information Geographically with MapKit .他们采用的概念是将可见 map 划分为网格,如果在特定网格内有多个注释,他们会删除它们并添加一个“集群”注释。它们还说明了您甚至可以如何以视觉方式动画化注解移入和移出群集,以便用户可以了解在放大和缩小时发生了什么。当您深入研究时,这是一个很好的起点。
这很重要,所以我会仔细考虑是否要自己实现它。我要么放弃 iOS 11 之前的版本,要么寻找第三方实现(question you reference 有很多示例)。
关于swift - MKMapView 不是在 Swift 中缩小 map 时的聚类注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56311852/
有没有办法在 .swift 文件(编译成 .swift 模块)中声明函数,如下所示: 你好.swift func hello_world() { println("hello world")
我正在尝试使用 xmpp_messenger_ios 和 XMPPFramework 在 iOS 上执行 MUC 这是加入房间的代码。 func createOrJoinRoomOnXMPP()
我想在我的应用程序上创建一个 3D Touch 快捷方式,我已经完成了有关快捷方式本身的所有操作,它显示正确,带有文本和图标。 当我运行这个快捷方式时,我的应用程序崩溃了,因为 AppDelegate
我的代码如下: let assetTag = Expression("asset_tag") let query2 = mdm.select(mdm[assetTag],os, mac, lastRe
我的 swift 代码如下所示 Family.arrayTuple:[(String,String)]? = [] Family.arrayTupleStorage:String? Family.ar
这是我的 JSON,当我读取 ord 和 uniq 数据时出现错误 let response2 : [String: Any] = ["Response":["status":"SUCCESS","
我想将 swift 扩展文件移动到 swift 包中。但是,将文件移动到 swift 包后,我遇到了这种错误: "Type 'NSAttributedString' has no member 'ma
使用CocoaPods,我们可以设置以下配置: pod 'SourceModel', :configurations => ['Debug'] 有什么方法可以用 Swift Package Manag
我正在 Xcode 中开发一个 swift 项目。我将其称为主要项目。我大部分都在工作。我在日期选择器、日期范围和日期数学方面遇到了麻烦,因此我开始了另一个名为 StarEndDate 的项目,其中只
这是 ObjectiveC 代码: CCSprite *progress = [CCSprite spriteWithImageNamed:@"progress.png"]; mProgressBar
我正在创建一个命令行工具,在 Xcode 中使用 Swift。我想使用一个类似于 grunt 的配置文件确实如此,但我希望它是像 Swift 包管理器的 package.swift 文件那样的快速代码
我假设这意味着使用系统上安装的任何 swift 运行脚本:#!/usr/bin/swift 如何指定脚本适用的解释器版本? 最佳答案 Cato可用于此: #!/usr/bin/env cato 1.2
代码说完全没问题,没有错误,但是当我去运行模拟器的时候,会出现这样的字样: (Swift.LazyMapCollection (_base:[ ] 我正在尝试创建一个显示报价的报价应用。 这是导入
是否可以在运行 Swift(例如 Perfect、Vapor、Kitura 等)的服务器上使用 RealmSwift 并使用它来存储数据? (我正在考虑尝试将其作为另一种解决方案的替代方案,例如 no
我刚开始学习编程,正在尝试完成 Swift 编程书中的实验。 它要求““编写一个函数,通过比较两个 Rank 值的原始值来比较它们。” enum Rank: Int { case Ace = 1 ca
在您将此问题标记为重复之前,我检查了 this question 它对我不起作用。 如何修复这个错误: error: SWIFT_VERSION '5.0' is unsupported, suppo
从 Xcode 9.3 开始,我在我的模型中使用“Swift.ImplicitlyUnwrappedOptional.some”包裹了我的字符串变量 我不知道这是怎么发生的,但它毁了我的应用程序! 我
这个问题在这里已经有了答案: How to include .swift file from other .swift file in an immediate mode? (2 个答案) 关闭 6
我正在使用 Swift Package Manager 创建一个应用程序,我需要知道构建项目的配置,即 Debug 或 Release。我试图避免使用 .xcodeproj 文件。请有人让我知道这是否
有一个带有函数定义的文件bar.swift: func bar() { println("bar") } 以及一个以立即模式运行的脚本foo.swift: #!/usr/bin/xcrun s
我是一名优秀的程序员,十分优秀!