location manager function is not getting called - ios

import UIKit
import MapKit
import CoreLocation
class StartViewController: UIViewController, CLLocationManagerDelegate
{
var latitude,longitude: Double?
var obLocatinManager: CLLocationManager?
#IBOutlet var objMapView: MKMapView?
#IBOutlet var standard: UIButton?
#IBOutlet var satellite: UIButton?
#IBOutlet var hybrid: UIButton?
#IBOutlet var mapView: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
edgesForExtendedLayout = UIRectEdge.None
let swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeRight)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadUserLocation() -> Void
{
obLocatinManager?.delegate = self;
obLocatinManager?.distanceFilter = kCLDistanceFilterNone
obLocatinManager?.desiredAccuracy = kCLLocationAccuracyHundredMeters
obLocatinManager?.requestWhenInUseAuthorization()
obLocatinManager?.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let newlocaton = locations[1] as! CLLocation
self.latitude = newlocaton.coordinate.latitude
self.longitude = newlocaton.coordinate.longitude
obLocatinManager?.stopUpdatingLocation()
self.loadMapView()
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
obLocatinManager?.stopUpdatingLocation()
}
func loadMapView() -> Void
{
var latDelta:CLLocationDegrees = 0.2
var longDelta:CLLocationDegrees = 0.2
var objCoor2D: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: self.latitude!, longitude: self.longitude!)
var objCoorSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var region: MKCoordinateRegion = MKCoordinateRegionMake(objCoor2D, objCoorSpan)
self.objMapView!.setRegion(region, animated: false)
}
#IBAction func didtapStandard(sender: UIButton)
{
self.standard?.backgroundColor = UIColor.greenColor()
self.satellite?.backgroundColor = UIColor.clearColor()
self.hybrid?.backgroundColor = UIColor.clearColor()
self.objMapView?.mapType = MKMapType.Standard
self.loadUserLocation()
println(self.longitude)
println(self.latitude)
}
#IBAction func didtapSatellite(sender: UIButton)
{
self.satellite?.backgroundColor = UIColor.greenColor()
self.standard?.backgroundColor = UIColor.clearColor()
self.hybrid?.backgroundColor = UIColor.clearColor()
self.objMapView?.mapType = MKMapType.Satellite
self.loadUserLocation()
println(self.longitude)
println(self.latitude)
}
#IBAction func didtapHybrid(sender: UIButton)
{
self.hybrid?.backgroundColor = UIColor.greenColor()
self.satellite?.backgroundColor = UIColor.clearColor()
self.standard?.backgroundColor = UIColor.clearColor()
self.objMapView?.mapType = MKMapType.Hybrid
self.loadUserLocation()
println(self.longitude)
println(self.latitude)
}
I am trying to fetch location by using map kit in IOS but (location manager)these function is not getting called

You never init the CLLocationManager object anywhere. Put the following code in the View Did Appear
obLocatinManager = CLLocationManager()
When you use simulator you can invoke location change by this menu. I gotta need to make some change first I think to try to catch the location change delegate in the simulators.

put obLocatinManager = CLLocationManager() in view did Appear and run it on to real device.

Related

Update MapView Pin Location

So I have a MapView which adds a pin to the current user location. Also I have two textfields which displays the coordinates of the pin. I want to add two features:
When I drag and drop the pin, I want to update the coordinates inside the textfields.
When I edit the coordinates in the textfield, it should move the pin to the updated coorinates from the textfields.
Here my code where i handle everything for MapView. Im still pretty new to coding, so the code could be confusing, sorry for that.
class LocateVC: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var finishedLocateButton: UIButton!
#IBOutlet weak var relocateButton: UIButton!
var coordinates: [[Double]]!
var latTitleLabel:[String]!
var latValueLabel:[String]!
var lngTitleLabel:[String]!
var lngValueLabel: [String]!
var isEdited = false
var customCallout: UIView?
//Important to track location
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
view.layer.backgroundColor = Colors.grey.cgColor
//button titles
relocateButton.isHidden = true
relocateButton.setTitle("Relocate", for: .normal)
finishedLocateButton.setTitle("Continue", for: .normal)
navigationItem.title = "Location"
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
self.mapView.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
if isEdited == false {
if CLLocationManager.locationServicesEnabled() {
addPin()
}
}
// Latitude,Longitude
coordinates = [
[ProductData.shared.latitude!, ProductData.shared.longitude!],
]
} //end of viewDidLoad
override func viewWillAppear(_ animated: Bool) {
super.dismiss(animated: animated)
}
public func removePin() {
}
func dropPinFor(placemark: MKPlacemark) {
for annotation in mapView.annotations {
if annotation.isKind(of: MKPointAnnotation.self) {
// mapView.removeAnnotation(annotation) // removing the pins from the map
}
}
let annotation = MKPointAnnotation()
annotation.coordinate = placemark.coordinate
mapView.addAnnotation(annotation)
}
//1
public func addPin() {
if isEdited == false {
ProductData.shared.latitude = locationManager.location?.coordinate.latitude
ProductData.shared.longitude = locationManager.location?.coordinate.longitude
}
self.mapView.delegate = self
// adds an annotation to coordinates from productData
let point = ProductAnnotation(coordinate: CLLocationCoordinate2D(latitude: ProductData.shared.latitude! , longitude: ProductData.shared.longitude!))
self.mapView.addAnnotation(point)
// 3
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: ProductData.shared.latitude!, longitude: ProductData.shared.longitude!), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
self.mapView.setRegion(region, animated: true)
}
public func editCoord() {
performSegue(withIdentifier: "editCoord", sender: CustomCalloutView.self)
}
#IBAction func relocateButtonClicked(_ sender: Any) {
addPin()
}
#IBAction func finishedLocateButtonClicked(_ sender: Any) {
performSegue(withIdentifier: "finishedLocateSegue2", sender: self)
}
//4
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
if view.isKind(of: CustomCalloutView.self ) || view.isKind(of: AnnotationView.self) || view.isKind(of: ProductAnnotation.self) {
return
} else {
customCallout?.removeFromSuperview()
}
}
//3
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if view.annotation is MKUserLocation {
return
}
//this creates the callout
let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
let calloutView = views?[0] as! CustomCalloutView
calloutView.delegate = self
calloutView.lngTitleLabel.text = "Lng"
calloutView.latTitleLabel.text = "Lat"
calloutView.lngTextField.text = String(format:"%f", ProductData.shared.longitude!)
calloutView.latTextField.text = String(format:"%f", ProductData.shared.latitude!)
calloutView.latTextField.layer.borderWidth = 0.0
calloutView.lngTextField.layer.borderWidth = 0.0
calloutView.latTextField.isEnabled = false
calloutView.lngTextField.isEnabled = false
calloutView.latTextField.keyboardType = .numberPad
calloutView.lngTextField.keyboardType = .numberPad
calloutView.alpha = 1.0
calloutView.layer.cornerRadius = 8
calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
customCallout = calloutView
view.addSubview(calloutView)
mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}
//2
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
if annotationView == nil{
annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "Pin")
annotationView?.isDraggable = true
annotationView?.canShowCallout = false
} else {
annotationView?.annotation = annotation
}
annotationView?.image = UIImage(named: "dot")
return annotationView
}
func saveButtonTapped() {
customCallout?.removeFromSuperview()
}
}
extension LocateVC: CustomCalloutViewDelegate {
func didClickEditButton() {
editCoord()
}
func didClickSaveButton() {
saveButtonTapped()
}
}
and here my custom callout:
class CustomCalloutView: UIView {
#IBOutlet weak var latTitleLabel: UILabel!
#IBOutlet weak var lngTitleLabel: UILabel!
#IBOutlet weak var editButton: UIButton!
#IBOutlet weak var saveButton: UIButton!
#IBOutlet weak var latTextField: UITextField!
#IBOutlet weak var lngTextField: UITextField!
var delegate: CustomCalloutViewDelegate?
var isEditing: Bool = false
#IBAction func didClickEditButton(_ sender: Any) {
// delegate?.didClickEditButton()
isEditing = true
latTextField.layer.borderWidth = 1.0
lngTextField.layer.borderWidth = 1.0
latTextField.isEnabled = true
lngTextField.isEnabled = true
saveButton.isHidden = false
}
#IBAction func saveButtonTapped(_ sender: Any) {
if isEditing == true {
if let lat = latTextField.text {
ProductData.shared.latitude = Double(lat)
}
if let lng = lngTextField.text {
ProductData.shared.longitude = Double(lng)
}
latTextField.layer.borderWidth = 0.0
lngTextField.layer.borderWidth = 0.0
latTextField.isEnabled = false
lngTextField.isEnabled = false
self.saveButton.setTitle("Save", for: .normal)
isEditing = false
} else {
self.saveButton.setTitle("Cancel", for: .normal)
delegate?.didClickSaveButton()
}
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
if self.bounds.contains(point) {
return true
} else {
return self.subviews.contains { $0.frame.contains(point) }
}
}
}
protocol CustomCalloutViewDelegate {
func didClickEditButton()
func didClickSaveButton()
}
any idea how i can achive that? Maybe in a general way.
Thank you in advance
Here's a link to DSCenterPinMapView. A CocoaPod I recommend you to use.
It is a custom MapView with an animated and customizable center pin useful for selecting locations in map.
Here is also a guide on installing CocoaPods if it is new to you.
You should install the Pod an then, as a solution to your questions:
Implement the delegate so that you can get the location where pin drops.
pinMapView.delegate = self
extension MyViewController: DSCenterPinMapViewDelegate {
func didStartDragging() {
// My custom actions
}
func didEndDragging() {
// My custom actions
selectedLocation = pinMapView.mapview.centerCoordinate
// Update Text field
}
}
After you finish adding the coordinates on your TextFields you should call
pinMapView.center(on coordinate: myTextFieldCoordinate)
to center the map on the desired location.
After changing the marker position get coordinates like
let latitude = marker.coordinate.latitude
let longitude = marker.coordinate.longitude
And to set coordinate
let coordinate = CLLocationCoordinate2D(latitude: "your lat", longitude: "your long")
marker.coordinate = coordinate
mapView.setCenter(coordinate, animated: true)
if you not keeping the marker reference then
guard let marker = self.appleMap.annotations.first else {return}
let latitude = marker.coordinate.latitude
let longitude = marker.coordinate.longitude
And to set coordinate
guard let marker = self.appleMap.annotations.first else {return}
let coordinate = CLLocationCoordinate2D(latitude: "your lat", longitude: "your long")
marker.coordinate = coordinate
mapView.setCenter(coordinate, animated: true)
Hope that will help.

How do I allow the user to resize an mkcircle object in a map view using a gesture recognizer on the mkcircle?

In Appleā€™s iOS Reminders app, you can set a location area at which you would be reminded of your reminder. When you set the location area, you are allowed to draw a circle that covers the area of the location you want to be reminded at. How would I duplicate this feature. I particularly would like to know how to allow the user to resize the circle on a map view by dragging his finger on the edge of the circle. I have already figured out how to create the circle.
Here is my code so far:
import UIKit
import MapKit
class ViewController: UIViewController {
// MARK: - Outlets
#IBOutlet weak var mapView: MKMapView!
// MARK: - Variables and Constants
let regionRadius: CLLocationDistance = 1000
let circularRegionRadius: CLLocationDistance = 500
let locationManager = CLLocationManager()
// MARK: - View
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
centerMapOnLocation(location: locationManager.location!)
}
// MARK: - Actions
#IBAction func addRegion(_ sender: UILongPressGestureRecognizer) {
print("addRegion(_:)")
let longPress = sender
let touchLocation = longPress.location(in: mapView)
let coordinates = mapView.convert(touchLocation, toCoordinateFrom: mapView)
let region = CLCircularRegion(center: coordinates, radius: circularRegionRadius, identifier: "geofence")
mapView.removeOverlays(mapView.overlays)
locationManager.startMonitoring(for: region)
let circle = MKCircle(center: coordinates, radius: region.radius)
mapView.add(circle)
}
// MARK: - Helper Functions
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius, regionRadius)
mapView.setRegion(coordinateRegion, animated: true)
}
// MARK: - Memory Warning
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
print("mapView(_:rendererFor:_:)")
guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}
let circleRenderer = MKCircleRenderer(circle: circelOverLay)
circleRenderer.strokeColor = .blue
circleRenderer.fillColor = .blue
circleRenderer.alpha = 0.2
return circleRenderer
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("locationManager(_:didUpdateLocations:)")
locationManager.stopUpdatingLocation()
mapView.showsUserLocation = true
}
}

How do I add a marker when I press the done button?

Okay, so I want to add a marker when I click the done button. I want to add my current locations marker to the MapVC class when I click the done button in AddPinPointVC. What would I have to do to do this?
Here's the relevant code:
MapsVC
class MapsVC: UIViewController {
weak var googleMaps: GMSMapView!
var locationManager = CLLocationManager()
var currentLocation: CLLocation?
var placesClient: GMSPlacesClient!
var zoomLevel: Float = 15.0
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 39.9533, longitude: -75.1593, zoom: 15.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
self.view = mapView
locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = 50
locationManager.startUpdatingLocation()
locationManager.delegate = self as? CLLocationManagerDelegate
placesClient = GMSPlacesClient.shared()
if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){
currentLocation = locationManager.location
}
}
static var latitude: CLLocationDegrees?
}
AddPinPointVC
class AddPinPointVC: UIViewController, UICollectionViewDelegateFlowLayout,UIPickerViewDelegate, UIPickerViewDataSource {
var pickerdata: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(handleCancel))
//function for cancel button
func handleCancel() { dismiss(animated: true, completion: nil) }
func handleDone(){
dismiss(animated: true, completion: nil)
let userMarker = GMSMarker()
userMarker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees, longitude: CLLocationDegrees)
// userMarker.title = typeOfPlaces[row]
userMarker.snippet = ""
}
}
If there are any further questions, please let me know! Thank you.
You should make a delegate for this with a function that fires when the Done button is pressed.
Protocol
protocol AddPinPointDelegate: class {
func addPinPointWillDismiss(_ marker: GMSMarker)
}
MapsVC
class MapsVC: UIViewController, AddPinPointDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 39.9533, longitude: -75.1593, zoom: 15.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
[...]
}
[...]
var mapView: GMSMapView!
//assuming you open AddPinPointVC via a segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "presentAddPinPoint" { //the identifier you gave for the segue
(segue.destination as! AddPinPointVC).delegate = self
}
}
func addPinPointWillDismiss(_ marker: GMSMarker) {
marker.map = mapView
}
}
AddPinPointVC
class AddPinPointVC: UIViewController, UICollectionViewDelegateFlowLayout, UIPickerViewDelegate, UIPickerViewDataSource {
[...]
weak var delegate: AddPinPointDelegate?
func handleDone() {
let userMarker = GMSMarker()
userMarker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees, longitude: CLLocationDegrees)
//userMarker.title = typeOfPlaces[row]
userMarker.snippet = ""
delegate?.addPinPointWillDismiss(userMarker)
dismiss(animated: true)
}
}

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) error occurs when getting the latitude and longitude

Please help me with this problem
import Foundation
import UIKit
import MapKit
class DetailViewController : UIViewController {
#IBOutlet weak var mapView: MKMapView!
var selectedLocation : LocationModel?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
// Create coordinates from location lat/long
var poiCoodinates: CLLocationCoordinate2D = CLLocationCoordinate2D()
poiCoodinates.latitude = CDouble(self.selectedLocation!.latitude!)! //Problem is in this line
poiCoodinates.longitude = CDouble(self.selectedLocation!.longitude!)!
// Zoom to region
let viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(poiCoodinates, 750, 750)
self.mapView.setRegion(viewRegion, animated: true)
// Plot pin
let pin: MKPointAnnotation = MKPointAnnotation()
pin.coordinate = poiCoodinates
self.mapView.addAnnotation(pin)
//add title to the pin
pin.title = selectedLocation!.name
}
}
You have not initialised var selectedLocation : LocationModel? so when you ask for self.selectedLocation! it crash.
Add that needed initialisation and try to refactor your code in this
way:
override func viewDidAppear(animated: Bool) {
guard let location = self.selectedLocation, let latitude = location.latitude, let longitude = location.longitude else {
return //Here was an error, so you can not continue, report it or do something about it before returning
}
// Create coordinates from location lat/long
var poiCoodinates: CLLocationCoordinate2D = CLLocationCoordinate2D()
poiCoodinates.latitude = CDouble(latitude)!
poiCoodinates.longitude = CDouble(longitude)!
// Zoom to region
let viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(poiCoodinates, 750, 750)
self.mapView.setRegion(viewRegion, animated: true)
// Plot pin
let pin: MKPointAnnotation = MKPointAnnotation()
pin.coordinate = poiCoodinates
self.mapView.addAnnotation(pin)
//add title to the pin
pin.title = location.name
}
I finally found the solution and here it goes:
import MapKit
class DetailViewController : UIViewController, MKMapViewDelegate {
//var mapType:UISegmentedControl!
//var showPointsOfInterest:UISwitch!
#IBOutlet weak var mapView: MKMapView!
#IBAction func showDirection(sender: AnyObject) {
}
var selectedLocation : LocationModel?
let locationManager = CLLocationManager()
var currentPlacemark:CLPlacemark?
//var segmentedControlAciton:UISegmentedControl!
#IBAction func myLocation(sender: AnyObject) {
// Request for a user's authorization for location services
locationManager.requestWhenInUseAuthorization()
let status = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
mapView.showsUserLocation = true
}
}
//
override func viewDidLoad() {
super.viewDidLoad()
// mapView.showsUserLocation = true
title = selectedLocation?.name
mapView.delegate = self
}
override func viewDidAppear(animated: Bool) {
// Create coordinates from location lat/long
var poiCoordinates: CLLocationCoordinate2D = CLLocationCoordinate2D()
poiCoordinates.latitude = CDouble(self.selectedLocation!.latitude!)!
poiCoordinates.longitude = CDouble(self.selectedLocation!.longitude!)!
// Zoom to region
let viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(poiCoordinates, 750, 750)
self.mapView.setRegion(viewRegion, animated: true)
// Plot pin
let pin: MKPointAnnotation = MKPointAnnotation()
pin.coordinate = poiCoordinates
self.mapView.addAnnotation(pin)
//add title to the pin
pin.title = selectedLocation!.name
pin.subtitle=selectedLocation!.address
mapView.showsScale = true
}

Swift delegate protocol not updating with locationManager

I have 2 views which the childView communicates with the parentView. I am using Google maps SDK to track a users location however if I startUpdatingLocation() using a button, the protocol does not work however if I initiate startUpdatingLocation() as soon as the app loads it works fine. Below are my Two controller files.
I am simply trying to display the location in a label but not sure why the protocol is not working after button click.
Child:
import Foundation
import UIKit
protocol LocationUpdateDelegate{
func delegateUserLocation(mapChildController:MapChildController, userLocation: String)
}
class MapChildController: UIViewController, CLLocationManagerDelegate
{
#IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
var didFindMyLocation = false
var myLocations:[String] = []
var alreadyUpdatedLocation = false
var locationDelegate:LocationUpdateDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 15, bearing: 0, viewingAngle: 45)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 8, minorVersion: 0, patchVersion: 0)) {
println("iOS >= 8.0.0")
locationManager.requestWhenInUseAuthorization()
}
mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as! CLLocation
var mapView = self.view as! GMSMapView
mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 15.0)
mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
var locValue:CLLocationCoordinate2D = manager.location.coordinate
myLocations.append("\(locValue.latitude)/\(locValue.longitude)")
self.locationDelegate?.delegateUserLocation(self, userLocation: "From child")
println("Child location: \(locValue.latitude)/\(locValue.longitude)")
}
func trackingToggle()
{
if !alreadyUpdatedLocation
{
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
println("Tracking started")
alreadyUpdatedLocation = true
}
else
{
locationManager.stopUpdatingLocation()
println("Tracking stopped")
alreadyUpdatedLocation = false
}
}
}
Parent:
import UIKit
struct Constants {
static let embedSegue = "embedSegue"
}
class MapMainController: UIViewController, LocationUpdateDelegate
{
let mapChild = MapChildController()
var alreadyUpdatedLocation = false;
#IBOutlet weak var startTrackingButton: UIButton!
#IBOutlet weak var MapStatsView: UIView!
#IBOutlet weak var txtUserLocation: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == Constants.embedSegue {
let mapChildController = segue.destinationViewController as! MapChildController
mapChildController.locationDelegate = self
}
}
func delegateUserLocation(mapChildController: MapChildController, userLocation: String) {
println("Parent location: \(userLocation)");
txtUserLocation.text = userLocation
}
#IBAction func startTrackingTapped(sender: UIButton) {
mapChild.trackingToggle();
if !alreadyUpdatedLocation
{
alreadyUpdatedLocation = true
startTrackingButton.setTitle("Stop Tracking", forState: UIControlState.Normal)
}
else
{
alreadyUpdatedLocation = false
startTrackingButton.setTitle("Start Tracking", forState: UIControlState.Normal)
}
}
}
You are setting the mapChildController to self just before you segue to another view:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == Constants.embedSegue {
let mapChildController = segue.destinationViewController as! MapChildController
mapChildController.locationDelegate = self
After you just set the delegate to self your viewController went out of scope, now your delegate is nil again and will never be called.
You need to set the delegate in viewDidLoad

Resources