Releasing Memory from MapView - ios

In my project I am using a mapView to render locations from Lat-Lon received from an API. My project has a button which does the follow:
When clicked, it fires a timer that retrieves coordinates from the web and then plots on the mapview
When clicked again, it stops the timer and no data is retrieved.
However even when the timer is stopped it consumes a lot of memory around 100mbs if not more. So I want to release the memory when user is not using map and when they are map should come back again. I did the following to release memory:
self.mapView.delegate = nil;
self.mapView.removeFromSuperview()
self.mapView = nil;
This removes map and my memory comes back to 20mbs, normal. However is this the correct way to release memory? and how do I get it back once the button is pressed?.

To add a map you can do this:
import UIKit
import MapKit
class ViewController: UIViewController {
var mapView: MKMapView?
#IBOutlet weak var framer: UIView!//uiview to put map into
var coordinate = CLLocationCoordinate2D(){
willSet{
print("removing annotation...")
if let m = mapView{
m.removeAnnotation(anno)
}
}
didSet{
print("did set called, adding annotation...")
anno.coordinate = coordinate
if let m = mapView{
m.addAnnotation(anno)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func start(_ sender: Any) {
let mk = MKMapView()
mk.bounds = framer.bounds
mk.mapType = MKMapType.standard
mk.isZoomEnabled = true
mk.isScrollEnabled = true
// Or, if needed, we can position map in the center of the view
mk.center = framer.center
mapView = mk
if let mk2 = mapView{
framer.addSubview(mk2)
}
}
To remove
#IBAction func stop(_ sender: UIButton) {
if mapView != nil{
if let mk2 = mapView{
mk2.delegate = nil;
mk2.removeFromSuperview()
mapView = nil;
}
}
}
}

Related

Disappearing markers during clustering in MapKit [duplicate]

I have an array of latitudes and another array of longitudes that I add to an array of type CLLocationCoordinate2D. I then use the new array to annotate multiple points on the map. Some, or most, or maybe even all of the annotations are displaying on the map but as I zoom in (yes, zoom IN), some of the annotations disappear, then come back, or dont. Any ideas on how to keep them all visible? This is behavior I would expect while zooming out, not in.
Here is the code i'm using for what i've described above.
import UIKit
import MapKit
import CoreLocation
class MultiMapVC: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var multiEventMap: MKMapView!
var latDouble = Double()
var longDouble = Double()
let manager = CLLocationManager()
var receivedArrayOfLats = [Double]()
var receivedArrayOfLongs = [Double]()
var locations = [CLLocationCoordinate2D]()
func locationManager(_ manager: CLLocationManager, didUpdateLocations uLocation: [CLLocation]) {
let userLocation = uLocation[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.3, 0.3)
let usersLocation = userLocation.coordinate
let region:MKCoordinateRegion = MKCoordinateRegionMake(usersLocation, span)
multiEventMap.setRegion(region, animated: true)
manager.distanceFilter = 1000
self.multiEventMap.showsUserLocation = true
}
func multiPoint() {
var coordinateArray: [CLLocationCoordinate2D] = []
print ("Received Longitude Count = \(receivedArrayOfLongs.count)")
print ("Received Latitude Count = \(receivedArrayOfLats.count)")
if receivedArrayOfLats.count == receivedArrayOfLongs.count {
for i in 0 ..< receivedArrayOfLats.count {
let eventLocation = CLLocationCoordinate2DMake(receivedArrayOfLats[i], receivedArrayOfLongs[i])
coordinateArray.append(eventLocation)
print (coordinateArray.count)
}
}
for events in coordinateArray {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: events.latitude, longitude: events.longitude)
multiEventMap.addAnnotation(annotation)
}
}
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
multiPoint()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
multiEventMap.removeFromSuperview()
self.multiEventMap = nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
NiltiakSivad's solution works but it reverts to the old iOS 10 look. If you want to keep the new iOS 11 balloon markers for iOS 11 and use the old pin look only for older iOS versions then you can implement the delegate method as below:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let reuseIdentifier = "annotationView"
var view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
if #available(iOS 11.0, *) {
if view == nil {
view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
view?.displayPriority = .required
} else {
if view == nil {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
}
view?.annotation = annotation
view?.canShowCallout = true
return view
}
The accepted answer from Leszek Szary is correct.
But there is some fineprint. Sometimes MKMarkerAnnotationViews are not rendered, even if
view.displayPriority = .required
is set.
What you are seeing is a combination of different rules.
MKAnnotationViews are rendered from top to bottom of the map. (It doesn't matter where north is).
If MapKit decides to draw overlapping MKAnnotationViews, then the MKAnnotationView nearer to the bottom is drawn on top (because it's drawn later)
Not only MKAnnotationViews, also titles rendered below MKMArkerAnnotationViews need space. The rendering of those titles is influenced by markerView.titleVisibility. If markerView.titleVisibility is set to .visible (instead of the default .adaptive), then this title is stronger than a MarkerAnnotationView that is rendered later, even if the later MarkerAnnotationView has a displayPriority = .required. The MarkerAnnotationView nearer to the bottom is not rendered.
This even happens if the MarkerAnnotationView nearer to the top has a low displayPriority. So a MarkerAnnotationView with low displayPriority and .titleVisibility = .visible can make a MarkerAnnotationView nearer to the bottom with displayPriority = .required disappear.
I am not aware of a documentation of this behaviour. This is the result of my experiments with iOS 12. My description is sipmplified.
I was experiencing a similar issue. My best guess is that it has something to do with how iOS 11 detects pin collisions. Implementing a custom annotation view or reverting to use the iOS 10 pin fixed the problem for me.
For example, implementing the following should fix your code:
class MultiMapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annotation = annotation as? MKPointAnnotation else { return nil }
let identifier = "pin-marker"
var view: MKAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}
return view
}
}
If this doesn't work, there is a displayPriority property that is worth looking into as it is responsible for helping to determine when pins should be hidden/shown at different zoom levels. More info at https://developer.apple.com/documentation/mapkit/mkannotationview/2867298-displaypriority
Hope this helps.
I was setting annotationView.displayPriority = .required only when the MKMarkAnnotationView was first allocated. Normally thats all you should need to do, but setting it each time the cell was reused fixed the issue for me.
I was seeing a similar problem with Xcode 10, and iOS 12+ as my deployment target. A post from an Apple staffer (https://forums.developer.apple.com/thread/92839) recommends toggling the .isHidden property on the dequeued marker. That has improved things but has not completely solved the problem.
result?.isHidden = true
result?.isHidden = false
return result

Swift - Movement of GMSMarker along array of CLCoordinates from GMSPath (Google Maps SDK for iOS)

All attempts at animating marker movement in Google Maps between coordinates point to using the following snippet of code in Swift:
CATransaction.begin()
CATransaction.setAnimationDuration(duration)
marker.position = coordindates
CATransaction.commit()
As an example here is the SO post with most votes:
How to smoothly move GMSMarker along coordinates in Objective c
This works ok when animating between an origin and destination pair of coordinates. However, I'm looking to animate from the starting coordinate in a GMSPath to the ending coordinate. When iterating through the points in the path, the only displayed animation is between the last two coordinates. The marker only appears at the second to last point and animates to the final point.
Here is my code for a ViewController. It is receiving a segment of a route as an encoded path (for testing, first encoded path: "ika~Exi|vN|AaDzAyCTc#N[lBeEvB_ExBkExBmEjBwDXo#").
The code is iterating through all stored coordinates inside the GMSPath object and attempting to animate using the snippet posted above. As stated, it only shows animation between the last two points.
I have tried placing all sets of code in ViewDidLoad, ViewDidAppear, and ViewWillAppear. ViewDidLoad kept the zoom level at intercontinental. ViewDidAppear and ViewWillAppear brought the zoom in appropriately, and resulted in the issues around animation mentioned in this post. The code is currently split between ViewDidAppear and ViewWillAppear, but will act the same if placed solely in either method.
import UIKit
import GoogleMaps
import CoreLocation
class MapVC:UIViewController {
var mapView:GMSMapView?
var polyline:GMSPolyline?
var path:GMSPath?
var encodedPath:String? = nil
var marker:GMSMarker?
override func viewDidLoad() {
super.viewDidLoad()
setupMap()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if encodedPath != nil {
self.path = GMSPath(fromEncodedPath: encodedPath!)
self.polyline = GMSPolyline(path: path)
self.polyline!.map = self.mapView!
let bounds:GMSCoordinateBounds = GMSCoordinateBounds(path: path!)
let update = GMSCameraUpdate.fit(bounds, withPadding: 10.0)
self.mapView!.animate(with: update)
} else {
print("nil path")
}
let a=2
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
var index:UInt = 0
let count:UInt = self.path!.count()
if count > 0 {
marker = GMSMarker(position: self.path!.coordinate(at:index))
marker!.map = self.mapView
index += 1
while index < count {
CATransaction.begin()
CATransaction.setAnimationDuration(30)
self.marker!.position = self.path!.coordinate(at:index)
CATransaction.commit()
index += 1
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupMap() {
let camera = GMSCameraPosition.camera(withLatitude: 36.5, longitude: -82.5, zoom: 16)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = mapView
}
}
Found a solution using a timer: https://github.com/antonyraphel/ARCarMovement/blob/master/ARCarMovementSwift/ARCarMovementSwift/ViewController.swift
Updated code from the view controller shown above:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
marker = GMSMarker(position: self.path!.coordinate(at:self.index))
marker!.map = self.mapView
timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector:
#selector(MapVC.timerTriggered), userInfo: nil, repeats: true)
}
#objc func timerTriggered() {
if self.index < self.path!.count() {
CATransaction.begin()
CATransaction.setAnimationDuration(1.9)
self.marker!.position = self.path!.coordinate(at:index)
CATransaction.commit()
self.index += 1
} else {
timer.invalidate()
timer = nil
}
}

Could not inset legal attribution from corner 4 swift

I am new to Xcode and mobile app. I am doing an app to find the current location. I tested it on the simulator and got this message in the console.
"Could not inset legal attribution from corner 4". What does it mean and how can I fix it?
import UIKit
import Alamofire
import AlamofireImage
import MapKit
import CoreLocation
class MapVC: UIViewController
#IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
let authorizationStatus = CLLocationManager.authorizationStatus()
let regionRadius: Double = 1000
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager.delegate = self
configureLocationServices()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func centerMapPressed(_ sender: Any) {
if authorizationStatus == .authorizedAlways || authorizationStatus == .authorizedWhenInUse{
centerMapOnUserLocation()
}
}
MKMapViewDelegate:
func centerMapOnUserLocation(){
guard let coordinate = locationManager.location?.coordinate else{return}
let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinate, regionRadius*2.0, regionRadius * 2.0 )
mapView.setRegion(coordinateRegion, animated: true)
}
CLLocationManagerDelegate:
func configureLocationServices(){
if authorizationStatus == .notDetermined{
locationManager.requestAlwaysAuthorization()
}else{
return}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
centerMapOnUserLocation()
}
this issue occurs when the required information in the right form is not found. if your device i trying to get some location or some number a variable, and that a variable is required for afun.. the value is not set into a, but afun is called that this error occurs..
call your cell's coordinates in viewdidload without any other function.
Here is the simplest way to get your cell's position.
1. you should have updated privacies of the devices under infor.plist
Add following lines into your plist before closing tag
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Privacy - Location Always and When In Use Usage Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Privacy - Location Always Usage Description</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Privacy - Location When In Use Usage Description</string>
after that.. this is the simplest working code .. i'm happily using it with no issue..
import CoreLocation
import MapKit
class AddressVc: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var map: MKMapView!
var locationManager = CLLocationManager()
here is code to move map in center for the required location
let latDelta:Double = 0.5
let lngDelta:Double = 0.5
let latitude:Double = 37.57554038
let longitude:Double = -122.40068475
let locationcoordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let zoomSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lngDelta)
let region = MKCoordinateRegion(center: locationcoordinates, span: zoomSpan)
self.map.setRegion(region, animated: true)
in the above code.. if you put device's lat and lng postions in latitude and lognitude that will move the map to your's devices's location.
now how to get device's location.
here is the code you can put into your viewDidLoad() function.
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
// device latitude = (locationManager.location?.coordinate.latitude)!
// device longitude = (locationManager.location?.coordinate.longitude)!
}
this is detect the device's latitude and longitude values. you can put them in above mentioned code..
Here is the full code.
import CoreLocation
import MapKit
class AddressVc: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var map: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
devicelatitude = (locationManager.location?.coordinate.latitude)!
devicelongitude = (locationManager.location?.coordinate.longitude)!
let latDelta:Double = 0.5
let lngDelta:Double = 0.5
let latitude:Double = devicelatitude
let longitude:Double = devicelongitude
let locationcoordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let zoomSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lngDelta)
let region = MKCoordinateRegion(center: locationcoordinates, span: zoomSpan)
self.map.setRegion(region, animated: true)
}
I hope this will help you.. if not.. there would be many other who are facing this issue.
==========================
With the simulator open, go to the DEBUG menu on the mac top bar, the last option is location, mine was showing this error when set to NONE. Set it Custom Location, first with you prompt you to a custom location on a new window, fill that, close the simulator, and relaunch the app. It should get the custom location from your code now. (andrecasarini credit)
This error occurs when the "Legal" link on the bottom left of the MapView is not within bounds or is obscured. There are similar errors for the map's scale and for the compass that appears when rotating.
Consider using size constraints or a safeAreaInset.
I believe the legal inset bug is in Apple's mapkit initialization code and unrelated to what we do when we use the code. Here's my reasoning.
If you use storyboards and the UIMapKit drag and drop module, the legal inset error pops up somewhere between the call to the initial call to application in the app delegate and the first viewDidLoad call, i.e., it's an OS error.
I got curious if I could work around it and wrote a version that doesn't use the storyboard editor. I was thinking that perhaps the editor was inserting some broken code into the app. My test app has two views, the start view and a second view with the map on it. The code is shown below. Don't be harsh, it's my first attempt at understanding what's going on with controllers views and subviews using programmatic views and subviews. It isn't pretty but it works to isolate the legal inset bug.
I started by building a new xcode project and deleting the storyboard. I then replaced the application function stub in appdelegate with the following code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
let viewController = ViewController()
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
let mapView = mapViewController()
mapView.otherView = viewController
viewController.mapView = mapView
return true
}
Even though the mapView is created here, the legal inset error doesn't show up. What does show up are a couple of other messages about scale and compass having some sort of issue. Since I was focusing on the legal inset bug, I ignored them and moved on.
I then replaced the default view controller with code that created two subviews, one a subview that serves as a button to switch to the second view controller and the second subview that serves as "you're on the first view" marker view. That required figuring out how to handle a tap. The color changing code in the tap handler was initial "hello world. I see a tap" code and serves no other purpose.
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
let DynamicView = UIView()
let switchView = UIView(frame: CGRect(x: UIScreen.main.bounds.minX+10, y: UIScreen.main.bounds.minY+20, width: 40, height: 40))
public var mapView:mapViewController? = nil
public var otherView:UIViewController? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = .white
DynamicView.backgroundColor = .yellow
DynamicView.layer.cornerRadius = 25
DynamicView.layer.borderWidth = 2
switchView.backgroundColor = .orange
switchView.layer.borderWidth = 2
var gR = UITapGestureRecognizer(target: self, action:#selector(self.handlebigTap(_:)))
DynamicView.addGestureRecognizer(gR)
gR = UITapGestureRecognizer(target: self, action:#selector(self.handlesmallTap(_:)))
switchView.addGestureRecognizer(gR)
self.view.addSubview(switchView)
self.view.addSubview(DynamicView)
DynamicView.translatesAutoresizingMaskIntoConstraints = false
let margins = view.layoutMarginsGuide
DynamicView.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 10).isActive = true
DynamicView.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 10).isActive = true
DynamicView.topAnchor.constraint(equalTo: margins.topAnchor, constant: 40).isActive = true
DynamicView.bottomAnchor.constraint(equalTo: margins.topAnchor, constant: 110).isActive = true
}
#objc func handlebigTap(_ sender: UITapGestureRecognizer) {
if ( self.DynamicView.backgroundColor == .green){
self.DynamicView.backgroundColor = .blue
} else {
self.DynamicView.backgroundColor = .green
}
}
#objc func handlesmallTap(_ sender: UITapGestureRecognizer) {
if ( self.switchView.backgroundColor == .orange){
self.switchView.backgroundColor = .blue
present(mapView!, animated: true, completion: nil)
} else {
self.switchView.backgroundColor = .orange
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
At this point, I checked to see if the first call to viewDidLoad triggered the legal inset error and saw that it did not. That meant the error was being triggered somewhere in the mapKit initialization code which was yet to be built. I simply copied and pasted the first viewController into a new file and called that mapViewController. I commented out the DynamicView code left over from the first controller and added the mapKit initialization code as shown here:
import UIKit
import MapKit
import CoreLocation
class mapViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {
let DynamicView = UIView(frame: CGRect(x: UIScreen.main.bounds.maxX-110, y: UIScreen.main.bounds.maxY-110, width: 100, height: 100))
let switchView = UIView(frame: CGRect(x: UIScreen.main.bounds.minX+10, y: UIScreen.main.bounds.minY+20, width: 40, height: 40))
var mapView = MKMapView()
public var otherView:UIViewController? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = .white
// DynamicView.backgroundColor = .yellow
// DynamicView.layer.cornerRadius = 25
// DynamicView.layer.borderWidth = 2
switchView.backgroundColor = .orange
switchView.layer.borderWidth = 2
var gR = UITapGestureRecognizer(target: self, action:#selector(self.handlebigTap(_:)))
DynamicView.addGestureRecognizer(gR)
gR = UITapGestureRecognizer(target: self, action:#selector(self.handlesmallTap(_:)))
switchView.addGestureRecognizer(gR)
self.view.addSubview(switchView)
// self.view.addSubview(DynamicView)
// mapView.frame = CGRect(x: 10, y: 60, width: view.frame.size.width-20, height: 300)
mapView.mapType = MKMapType.standard
mapView.isZoomEnabled = true
mapView.isScrollEnabled = true
view.addSubview(mapView)
mapView.translatesAutoresizingMaskIntoConstraints = false
let margins = view.layoutMarginsGuide
mapView.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 10).isActive = true
mapView.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 0).isActive = true
mapView.topAnchor.constraint(equalTo: margins.topAnchor, constant: 45).isActive = true
mapView.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: -20).isActive = true
}
#objc func handlebigTap(_ sender: UITapGestureRecognizer) {
if ( self.DynamicView.backgroundColor == .green){
self.DynamicView.backgroundColor = .blue
} else {
self.DynamicView.backgroundColor = .green
}
}
#objc func handlesmallTap(_ sender: UITapGestureRecognizer) {
if ( self.switchView.backgroundColor == .orange){
self.switchView.backgroundColor = .blue
dismiss(animated: true, completion: nil)
} else {
self.switchView.backgroundColor = .orange
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
}
}
I ran the above code and stepped through looking for the legal inset message to show up. This is the offending line:
mapView.frame = CGRect(x: 10, y: 60, width: view.frame.size.width-20, height: 300)
As soon as the view is framed, the error message comes out. Doesn't matter what dimensions you give the view, the message appears. OK.... Maybe the message expects a constraint based framing instead of hard coded per the offending line.
I commented out the line and added constraints to the view and still the error popped up.
At this point I gave up. I couldn't figure out how to configure a map view so the error message doesn't show up. Hopefully Apple will pipe up and say something at this point or someone else will pick up the baton to see if there's a way to configure mapkit to stop spewing error messages.
As for myself at least I learned how to dynamically add views, subviews, gesture recognizers and constraints so my time was profitably spent chasing the bug. Thanks to all who posted sample dynamic view code, gesture recognition code, subview code and constraint code. You may recognize a bit of your code here.
You have not typed in an opening curly bracket for your MapVC Class:
your code:
class MapVC: UIViewController
to fix:
class MapVC: UIViewController {

how to create a custom callout button from each annotation view?

Im trying to create a button within each annotation view to bring the user to a new page custom to the selected annotation. I've successfully implemented the mapview to show the annotations with titles and subtitles loaded from my parse database but am struggling to find a way of 1) adding a button to the annotation view to bring the user to a new view 2) finding a way of creating custom views for each of the annotations when selected. Any help would be greatly appreciated ?
Code
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet weak var mapView: MKMapView!
var MapViewLocationManager:CLLocationManager! = CLLocationManager()
let btn = UIButton(type: .DetailDisclosure)
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = true
mapView.delegate = self
MapViewLocationManager.delegate = self
MapViewLocationManager.startUpdatingLocation()
mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}
override func viewDidAppear(animated: Bool) {
let annotationQuery = PFQuery(className: "Clubs")
annotationQuery.findObjectsInBackgroundWithBlock {
(clubs, error) -> Void in
if error == nil {
// The find succeeded.
print("Successful query for annotations")
// Do something with the found objects
let myClubs = clubs! as [PFObject]
for club in myClubs {
//data for annotation
let annotation = MKPointAnnotation()
let place = club["location"] as? PFGeoPoint
let clubName = club["clubName"] as? String
let stadiumName = club["stadium"] as? String
annotation.title = clubName
annotation.subtitle = stadiumName
annotation.coordinate = CLLocationCoordinate2DMake(place!.latitude,place!.longitude)
//add annotations
self.mapView.addAnnotation(annotation)
}
} else {
// Log details of the failure
print("Error: \(error)")
}
}
}
For 1st - You can use leftCalloutAccessoryView & rightCalloutAccessoryView on annoationView. You will have to implement MapViewDelegate
mapView(_:viewForAnnotation:).
For 2nd - Similar to calloutAccessoryView, you have access to detailCalloutAccessoryView on annoationView. You can use that to customize your callout.
This is Objective-C solution, hope it may help you.
To set button to annotationView,
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = detailButton;
When click performed on this button, following method called like delegate, so override following method too,
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
You can do whatever customisation you want in above method because you know that user tapped on accessory view by now.

Open last scroll position in UIWebView

I'm have a WebView:
func loadPage() {
let request = NSURLRequest (URL: url!)
myBrowser.loadRequest(request)
}
Which load RTF document from this URL:
var url: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Chapter1", ofType: "rtf")!)!
How can I save last scroll position to NSUserDefaults, to get it then (if user pause reading and resume it).
Or maybe other type or method to make bookmark in this case.
Note: Ive been trying this:
var myBookmark: CGPoint = CGPointMake(0, 0)
override func viewWillDisappear(animated: Bool) {
myBookmark = myBrowser.scrollView.contentOffset
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
myBrowser.scrollView.delegate = self
myBrowser.scrollView.setContentOffset(myBookmark, animated: false)
}
But it's not take any effect.
UPD1: translate my VAR name.
To save and retrieve your scrollview offset from NSUserDefaults, set your UIWebView's delegate and try this:
var viewLaidoutSubviews = false // <-- variable to prevent the viewDidLayoutSubviews code from happening more than once
// Save your content offset when the view disappears
override func viewWillDisappear(animated: Bool) {
var userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(NSStringFromCGPoint(myBrowser.scrollView.contentOffset), forKey: "scroll_offset")
userDefaults.synchronize()
viewLaidoutSubviews = false
}
// Retrieve and set your content offset when the view re-appears
// and its subviews are first laid out
override func viewDidLayoutSubviews() {
if (!viewLaidoutSubviews) {
// If a scroll_offset is store in NSUserDefaults, use it
var userDefaults = NSUserDefaults.standardUserDefaults()
var scrollOffset:CGPoint? = CGPointFromString(userDefaults.valueForKey("scroll_offset") as? NSString)
if let position:CGPoint = scrollOffset {
myBrowser.scrollView.delegate = self
myBrowser.scrollView.setContentOffset(position, animated: false)
}
viewLaidoutSubviews = true
}
}
And utilize you UIWebView's webViewDidFinishLoad delegate method to update the scroll offset in the case that the web view didn't finish rendering before the view laid out its subviews:
// Retrieve and set your content offset once the web view has
// finished rendering (in case it hasn't finished rendering
// by the time the view's subviews were laid out)
func webViewDidFinishLoad(webView: UIWebView) {
// If a scroll_offset is store in NSUserDefaults, use it
var userDefaults = NSUserDefaults.standardUserDefaults()
var scrollOffset:CGPoint? = CGPointFromString(userDefaults.valueForKey("scroll_offset") as? NSString)
if let position:CGPoint = scrollOffset {
myBrowser.scrollView.delegate = self
myBrowser.scrollView.setContentOffset(position, animated: true)
}
}
But note, your NSUserDefaults value will also persist between app launches unless you delete it.

Resources