iOS:使用MapBox为从用户输入中出现的注释创建动态语音提示框 [英] iOS: Creating dynamic speech balloons for annotations that appear from user input using MapBox

查看:73
本文介绍了iOS:使用MapBox为从用户输入中出现的注释创建动态语音提示框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布的代码中,当您单击注释时,语音气球会弹出并说出

Hello World!
Welcome to my marker

我想知道如何在使用应用程序时使气泡出现,并使气泡显示用户输入的一些文本,并在大约一个小时后消失.即使该用户注销或关闭了该应用程序,该气泡也将被其他用户看到,并且除非用户经过该气泡的时间窗口,否则当用户再次进入该应用程序时,该气泡仍将处于打开状态. /p>

谢谢

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并向其中添加文本字段.但是,这可能有些棘手,因为看起来MGLPointAnnotationMGLShape的子类,而它似乎不是视图/视图控制器类的常用UIKit层次结构的子类.您最好将Mapbox框架换成基本的MapKit解决方案(...我不知道您还依赖Mapbox做什么).

Apple的MapKit确实具有MKAnnotationView.对于如何将UITextField添加到MKAnnotationView,有一个明确的答案.请参阅如何在MKAnnotationView标题中添加UITextField .您可能需要根据答案的表现方式修改答案.

另一方面,如果您考虑用户通过iOS应用程序中的另一个屏幕在文本字段中输入文本,则有许多简便的方法可以在UIViewControllerUITextField >,UITableViewControllerUICollectionView

或者,如果您正在考虑用户通过网站输入文本,那么使用HTML表单就很容易了.

要在气泡消失之前显示气泡大约1或3个小时的时间范围,您需要向MKAnnotationView子类添加createdTimestamp属性.只需定期将当前时间与注释上的createdTimestamp进行比较,如果为currentTime >= annotation.createTimestamp + oneHour,则从地图上删除注释.您可以在此处查看Swift中的日期: https://developer.apple.com/documentation/foundation /日期

就其他用户"看到的气泡而言,这将需要某种网络解决方案(例如与这些气泡的数据同步然后将其广播给其他用户的中央服务器).如果您正在考虑使用网站来收集/显示地图数据,则无论如何都需要网络设置.

In the code posted, when you click on the annotation, the speech balloon pops up to say

Hello World!
Welcome to my marker

I would like to know how to make the speech bubble appear while using the app, and have the speech bubble display some text that the user would enter in, and disappear after about an hour or so. The bubble would be able to be seen by other users even if the user logged out or closed the app, and the bubble would still be open when the user goes back into the app, unless the window of time for the bubble has passed.

Thank-you

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
    }
}

解决方案

Are you wanting the user to enter text into a text field inside the annotation bubble? If so, consider subclassing MGLPointAnnotation and adding a text field to it. That might be a bit tricky though since it would appear that MGLPointAnnotation is a subclass of MGLShape, which appears to not be a subclass of the usual UIKit hierarchy of view/view-controller classes. You may be better off swapping out the Mapbox framework for a basic MapKit solution (...I don't know for what else you are relying on Mapbox though).

Apple's MapKit does have MKAnnotationView. There is a definite answer for how to add a UITextField to MKAnnotationView. See how to add UITextField in MKAnnotationView Title. You may need to modify the answer depending on how you want your annotation to behave.

If, on the other hand, you were thinking of the user entering text into a text field through another screen in the iOS app, there are many easy ways to properly implement a UITextField in a UIViewController, UIView, UITableViewController, UICollectionView, etc.

Alternatively, if you were thinking about the user entering text through a website, that is trivially easy with HTML forms.

For the approximate 1 or 3 hour(s) timeframe for displaying the bubble before it goes away, you would need to add a createdTimestamp property to the MKAnnotationView subclass. Just compare the current time periodically to the createdTimestamp on the annotation and if currentTime >= annotation.createTimestamp + oneHour, remove the annotation from the map. You can see about dates in Swift here: https://developer.apple.com/documentation/foundation/date

As far as "other users" seeing the bubble goes, that would require some sort of networked solution (such as a central server that synchronizes with these bubbles' data and then broadcasts them to other users). You would need a networked setup anyway if you were thinking of using website(s) to gather/display map data.

这篇关于iOS:使用MapBox为从用户输入中出现的注释创建动态语音提示框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆