在贴出的代码中,当你点击注释时,会弹出对话气球说
Hello World!
Welcome to my marker
我想知道如何在使用该应用程序时让对话泡泡出现,并让对话泡泡显示用户输入的一些文本,并在大约一个小时左右后消失。即使用户注销或关闭应用程序,其他用户也可以看到气泡,并且当用户返回应用程序时气泡仍会打开,除非气泡的时间窗口已经过去。
谢谢
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Set the map’s center coordinate and zoom level.
mapView.setCenter(CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407), zoomLevel: 12, animated: false)
view.addSubview(mapView)
// Set the delegate property of our map view to `self` after instantiating it.
mapView.delegate = self
// Declare the marker `hello` and set its coordinates, title, and subtitle.
let hello = MGLPointAnnotation()
hello.coordinate = CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407)
hello.title = "Hello world!"
hello.subtitle = "Welcome to my marker"
// Add marker `hello` to the map.
mapView.addAnnotation(hello)
}
// Use the default marker. See also: our view annotation or custom marker examples.
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
return nil
}
// Allow callout view to appear when an annotation is tapped.
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}
}
最佳答案
您是否希望用户在注释气泡内的文本字段中输入文本?如果是这样,请考虑子类化 MGLPointAnnotation
并向其添加文本字段。这可能有点棘手,因为看起来 MGLPointAnnotation
是 MGLShape
的子类,它似乎不是通常的 UIKit View / View 层次结构的子类- Controller 类。您最好将 Mapbox 框架换成基本的 MapKit 解决方案(...不过我不知道您还依赖 Mapbox 的什么)。
Apple 的 MapKit 确实有 MKAnnotationView
。对于如何将 UITextField
添加到 MKAnnotationView
有一个明确的答案。参见 how to add UITextField in MKAnnotationView Title .您可能需要根据您希望注释的行为方式修改答案。
另一方面,如果您正在考虑用户通过 iOS 应用程序中的另一个屏幕将文本输入文本字段,则有许多简单的方法可以在 UITextField
中正确实现 UIViewController
, UIView
, UITableViewController
, UICollectionView
等
或者,如果您考虑用户通过网站输入文本,那么使用 HTML 表单就很容易了。
对于在气泡消失之前显示气泡的大约 1 或 3 小时时间范围,您需要将 createdTimestamp
属性添加到 MKAnnotationView
子类。只需定期将当前时间与注释上的 createdTimestamp
进行比较,如果 currentTime >= annotation.createTimestamp + oneHour
,则从 map 中删除注释。您可以在此处查看 Swift 中的日期:https://developer.apple.com/documentation/foundation/date
就“其他用户”看到气泡消失而言,这将需要某种网络解决方案(例如与这些气泡数据同步然后将其广播给其他用户的中央服务器)。如果您正在考虑使用网站来收集/显示 map 数据,您无论如何都需要联网设置。
关于iOS:使用 MapBox 为出现在用户输入中的注释创建动态语音气球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52574121/