How to get marker data of current camera position in Google Maps iOS SDK? - ios

I'm building a tableview/scrollview to the lower part of a window containing a GMSMapView.map, which is intended to display the marker data of the current camera position.
The GMSMapView.map has already markers set in.
I'm missing the way to get the marker data of the current camera position, checked through Google's documentation but couldn't find it there, any help largely appreciated.

Some digging and here's the answer, simplified, using the SDK's mapView didChange position func, single already defined marker and print confirmations:
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
let camView = mapView.projection.visibleRegion()
let cameraBox = GMSCoordinateBounds(region: camView)
if cameraBox.contains(marker.position) {
print("YES")
}
else {print("NO")
}

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.

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.

How to get the long pressed point in MapView. Google Maps SDK. Swift

For the mapView, I create it by self.view.addSubview(mapView!), so I guess that's why I can't override the touchesBegan function to detect the touch point. There is a function called didLongPressAtCoordinate in mapView delegate, but it only contains coordinates and mapView as argument.
func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
}
Any suggestions?
Thanks.
Please try using UITapGestureRecognizer.
For the implementation, you can check the solution given in this SO post - UIScrollView touchesBegan.
Aside from that, explanation and sample code given in this blog - How to perform action when user taps on map using MapKit, Swift might also help.

Google Maps SDK didChangeCameraPosition() gets called repeatedly

I'm trying to refresh my Google map by fetching new, different sets of data from a different geohash region whenever a user moves/scrolls the camera to a another location in the map.
func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
if let curLocation = curLocation{
loadDataFromFireBase(curLocation.coordinate)
}
}
However, for some reasons this function gets called repeatedly every single second even if I haven't moved the camera.
I have found a solution for this. Instead of using didChangeCameraPosition you can use idleAtCameraPosition.

Google map GMSMarker warning

I have integrated google map SDK in my iOS app. It gives following warning in debug area.
Marker set as selectedMarker while not belonging to this map. Ignoring.
If any one have idea then please share it.
Whenever you create a GMSMarker, you'll want to set its map property. Check out Google's documentation here. Swift example below, but the .map call is the same in Objective-C.
let position = CLLocationCoordinate2D(latitude: 10, longitude: 10)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = mapView
Line 4 is the key here. Failing to do that will throw the error you're seeing.
The main reason is that you forget to set marker.map = mapView,
the second and the most common reason for whom interacting with markers is that you return 'false' in
mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
, the documentation said:
return YES if this delegate handled the tap event, which prevents the
map from performing its default selection behavior, and NO if the
map should continue with its default selection behavior.

Resources