iOS: Creating dynamic speech balloons for annotations that appear from user input using MapBox - ios

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.

Presumably from your other question, I assume that you are using the map fullscreen. There are several approaches to this.
You could use the default I accessory button to add a target action to it. Which calls a custom UIView which has a textView and a submit button. Which then modifies your annotation.
Or you could modify your mapview to show a small textbox at the bottom of the screen which is then shifted upwards whilst editing and added to your annotation upon submitting.
When it comes to your timeout question I did not find anything in the MapBox's documentation to get you the results you want. I believe it needs some sort of backend server side timer function which will handle this accordingly.

Related

Perform action when tapping on My Location in MapKit

Is there a way to trigger code to execute when tapping on the "My Location" blue dot in MapKit?
I tried implementing func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) { but this doesn't get called when tapping the blue dot. Instead, a pretty useless generic avatar annotation shows up as shown in the simulator screenshot above. Can this behavior be customized?
You need to implement the delegate function mapView(viewForAnnotation) and substitute your own view in the case where the annotation is the MKUserLocation. Now you are in charge of how the caption behaves.

swift google maps open modal on marker tap

Within my swift app I used to open up a infowindow when a map marker was tapped. I am in the process of changing this instead to open up a viewcontroller as a modal and present the information in it instead. The problem I'm encountering is that the modal doesn't present from when a map marker is tapped. The map marker gets called, the data I need is passed to the controller but the controller itself won't appear. It looks to exist as when I tap the marker a second time, it'll error out about the activity already presenting.
Here is the code on tapping the marker
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
mapView.animate(toLocation: CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude))
detailsViewController.marker = marker
present(detailsViewController, animated: true)
return true
}
The viewcontroller does present correctly if not done with a map marker. If I tap a button and call the present, it show as it should.
Anyone do something like this, have an idea of what needs to be changed, or have some code they can share?
Update: it mysteriously started working so the code is correct. No change. Just rebuilt a few times. Even clean didn't resolve it but building it numerous times did.

Check Which Annotation Callout is Opening

I'm working on a project where I need to find the estimated travel time from the user location to a certain MKAnnotation. I would like to be able to see which annotation the user has clicked on so I can generate the ETA and display it on the annotationCalloutView using the "viewFor annotation" function.
This ETA cannot be calculated using a custom MKAnnotation class due throttling from Apple if done in this way: HERE So it must be done while the annotation callout is opening.
You have the mapView delegate method:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
}
You have the parameter view which is the clicked MKAnnotationView and from that parameter you can access your annotation.

is there a way of scrolling the mkmapview in swift automatically?

In my Storyboard in Swift app I have a UIViewController with MKMapView stretched to each edge of the screen. It was enough for me to import a MapKit, then I set up the delegate:
class MyClass: MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad(){
super.viewDidLoad()
mapView.delegate = self
}
}
and then, when I ran the app, I saw the map nicely centered above my country.
I want to implement a feature, that works like this:
when user opens this view, he sees the map, but centered on a place next to my home country, then the map could scroll automatically to center to my home country. Is that even achievable?
For example, if I lived in the US and opened the map, I would see the Pacific Ocean and then the map would scroll to show me the US territory.
Of course it's possible. MKMapView has the method setRegion(_:animated:).
You could set the map region to a spot in the pacific, wait a few seconds (using a timer or dispatch_after) and then call setRegion(_:animated:) to move the map to be centered over your current location.

How do I allow users to click on iOS maps to show a callout in swift?

I am trying to build an application where I need to show a callout with details regarding business (name, address etc) when user clicks on the points of interests on the map.
I am able to show callout when there is an annotation. But I want to have a functionality similar to apple maps application, where even without an annotation, users are able to directly tap on the point of interest to show the details about that point of interest.
I have already set the following properties on my mapView:
mapView.userInteractionEnabled = true
mapView.showsPointsOfInterest = true
Any help is appreciated.
you can use MKMapViewDelegate and override Mouse Event
override func rightMouseDown(theEvent: NSEvent) {
let eventLocation: NSPoint = theEvent.locationInWindow
// do something
NSNotificationCenter.defaultCenter().postNotification(notification)
}

Resources