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

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

Related

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

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.

Reloading Google Map View when adding and removing markers

We are coding in Swift to create an application with UI buttons. These UI buttons will add or remove markers depending on its status. Because we want the buttons to be layered on top of google maps we have two view controllers. The top view controller contains a button. When the button is pressed, we want to remove the markers that have a "bad" status.
This is our code to remove the marker:
func showOnlyGood(){
mapView.clear() //this is the google map (GMSMapView.map)
for x in arrayOfGood { //Array of good markers
x.map = mapView //Set good markers to show
}
for y in arrayOfBad { //Array of bad markers
y.map = nil //removes markers from map
}
}
The google maps gets updated if the function call is in viewDidLoad(), but when we call the function in the top view controller with the buttons it does not update the map accordingly.
We think this is an issue with refreshing the google map view and have tried many different solutions, but the google map view only shows what is initially in viewDidLoad().
First, if you just want the buttons to be layered on top of the map, just add the buttons to the view after you've added the map to the view; do not create a second view controller. Your problem is most likely caused by this awkward setup. You also don't have an #objc prefix in your action method, which would definitely prevent the button from executing its action.
#objc func updateButtons() {
mapView.clear() // clear the map
for i in someArray {
let marker = GMSMarker()
// configure parameters
marker.map = mapView
}
}
That method will update your map's markers. There is [probably] never a need to refresh the map, even if you want to change styles.

List of neearby searched types IOS xcode

So this piece of code gets nearest searchedTypes(atms and banks) for google and puts markers around the map.
private func fetchNearbyPlaces(coordinate: CLLocationCoordinate2D) {
mapView.clear()
dataProvider.fetchPlacesNearCoordinate(coordinate, radius:searchRadius, types: searchedTypes) { places in
places.forEach {
let marker = PlaceMarker(place: $0)
marker.map = self.mapView
}
}
}
How do I get a table view with the list of these nearest searedTypes and get information on them to show on another viewcontroller where I can navigate from user location to that point. How the normal google maps works.
With places acquired, you can pass data to a new controller that has table view and display data there.

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.

Google Maps SDK iOS change floor level programmatically

I dont find any way in the documentation how to change the floor level programmatically. For example:
I load a specific polygon for 1 room, and i want to change the floor map to the specific floor level (For example Zoom to Level 2, and then add my Polygon).
Polygon adding with Infoboxes are not the problem, ill just want to know if its possible to programmatically select the right floor level.
Like here:
I still know that there is the GMSIndoorDisplayDelegate - and i am able to set the active building. There should be an "activeLevel" method, but i am unable to assign any value.
I found the solution on how to force the floor change for a particular building.
Add the delegate GMSIndoorDisplayDelegate to the class,
Set the delegates:
mapView.delegate = self
mapView.indoorDisplay.delegate = self
Add the delegate methods:
func didChangeActiveBuilding(building: GMSIndoorBuilding!) {
if let currentBuilding = building {
var levels = currentBuilding.levels as! [GMSIndoorLevel]
mapView.indoorDisplay.activeLevel = levels[2] // set the level (key)
}
}
func didChangeActiveLevel(level: GMSIndoorLevel!) {
println("will be called after activeBuilding")
}
P.S. Just a friendly reminder, the upmost level is the first item in the array. It's backwards.

Resources