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

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.

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.

Google maps into a small rectangle

I'm just starting to look into learning swift and iOS app making. I'm trying to make an app that will use google maps. So far looking at examples I created a map and set
let camera = GMSCameraPosition.cameraWithLatitude(45.489434, longitude: -73.775858, zoom: 15)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
view = mapView
This works fine but covers up the entire screen, I would like to be able to set the map to the lower portion of the screen only for example so I can add other things to the top.
To do so I tried adding a view to my view controller that covers a smaller portion of the screen. This new view is a child of the main view that was already there. I then changed the class of this new smaller view to GMSMapView and linked it to viewController.swift which created this.
#IBOutlet weak var smallMap: GMSMapView!
I then assign my map to this smallMap view and my code now looks like this.
#IBOutlet weak var smallMapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
GMSServices.provideAPIKey("AIzaSyCNG-Qg6vtIL7pQ26go1hPy9mmhfEre5IU")
let camera = GMSCameraPosition.cameraWithLatitude(45.489434, longitude: -73.775858, zoom: 15)
smallMapView.camera = camera}
Is that the right thing to do to get a smaller map view? When I run this I get libc++abi.dylib: terminating with uncaught exception of type NSException error.
Anybody can help with what I'm missing?
EDIT: Here's what my Xcode view looks like
Thanks!

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

How to get Google Street View working inside a view in iPhone?

I have just recently started learning Swift and iOS development and I am trying to get Google Street View working on a Single View Application. So basically I was thinking to have a ViewController that has a view and inside the view I will have the StreetView view and a Button.
Following Google's tutorial: https://developers.google.com/maps/documentation/ios/streetview I can get everything working. It works when I do this:
self.view = panoView
Now, I am trying to achieve what I mentioned above with the following code:
class ViewController: UIViewController, GMSPanoramaViewDelegate{
#IBOutlet weak var panoramaView: GMSPanoramaView!
override func viewDidLoad() {
super.viewDidLoad()
var panoView = GMSPanoramaView(frame: CGRectZero)
self.panoramaView.addSubview(panoView)
self.panoramaView = panoView
self.panoramaView.delegate = self
panoView.moveNearCoordinate(CLLocationCoordinate2DMake(position.latitude, position.longitude))
}
}
And what I have in the storyboard is the following:
The only thing I can see is the refresh button.
I have also tried not setting the view as a GMSPanoramaView but only as a UIView, but that also didnt work
Can anyone point me to the right direction, please? Ultimately what I want is the StreetView covering all my screen and I want some buttons and some views on top of it.
I am using Xcode 6.1
Thanks
EDIT:
I added the function and can see the ID being printed out
func panoramaView(view: GMSPanoramaView!, didMoveToPanorama panorama: GMSPanorama!) {
println(panorama.panoramaID)
}
StreetView still empty

Resources