Swift delegate protocol not updating with locationManager - ios

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

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.

location manager is not on main thread after segue?

I have a serious problem about use location manage and mapview.
I create a app simple show where the location I am now, it runs ok when only one viewcontroller, but after I added a viewController and segue, and after segue first viewController to map viewController, it has some error message:
location manager (0x101f3d870) was created on a dispatch queue executing on a thread other than the main thread
after that, the App crash with this error message :
MKMapView must be initialized on the main thread
But I really move the code into the main thread, and it does ok before I added first viewcontroller and segue!!! The code at below:
import UIKit
import MapKit
import CoreLocation
class Map_ViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate,UISearchBarDelegate {
#IBOutlet weak var mapView: MKMapView!
var cloaction = CLLocationManager()
var span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
var currentLat : Double = 0
var currentLon : Double = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
// Start get the location on viewWillAppear
DispatchQueue.main.async {
self.mapView.delegate = self
self.mapView.showsScale = true
self.mapView.showsPointsOfInterest = true
self.mapView.showsUserLocation = true
self.cloaction.requestAlwaysAuthorization()
self.cloaction.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
self.cloaction.delegate = self
self.cloaction.desiredAccuracy = kCLLocationAccuracyBest
self.cloaction.startUpdatingLocation()
}
setCenter()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
mapView.region.center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
mapView.setRegion(mapView.region, animated: false)
}
}
func setCenter() {
let center = CLLocationCoordinate2D(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
mapView.region.center = center
mapView.region.span = span
mapView.setRegion(mapView.region, animated: true)
}
}
The segue in the main viewController :
override func viewDidLoad() {
super.viewDidLoad()
self.performSegue(withIdentifier: "goToMap", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("goToMapViewController")
}

Swift gesture delegates

I'm trying to implement HERE maps SDK gestures. I added delegate and didRecivePan function. The pan gesture is recognized by the application.
Problem is that my mapView gets frozen for the time my pan gesture is being used and it creates lagging like map scrolling which makes UX really annoying. Is there anyway this can be resolved.
var follow : Bool = true;
#IBOutlet weak var mapView: NMAMapView!
let positionManager = NMAPositioningManager.self
let gestureMarker = NMAMapMarker.self
override func viewDidLoad() {
super.viewDidLoad()
mapView.gestureDelegate = self
}
#objc func didLosePosition(){ print("Position lost")}
#objc func positionDidUpdate(){
/* Possible solution but an ugly one!
if(follow == true){
mapView.gestureDelegate = self
}else if(follow == false){
mapView.gestureDelegate = nil
}*/
print("Follow \(follow)")
print("position updated")
let position = positionManager.sharedInstance().currentPosition
print(position!.coordinates ?? "xx")
if(follow){
mapView.set(geoCenter:(position?.coordinates)!,animation: .linear)
}
}
override func viewWillAppear(_ animated: Bool) {
if(positionManager.sharedInstance().startPositioning()){
// Register to positioning manager notifications
NotificationCenter.default.addObserver(self, selector: #selector(self.positionDidUpdate), name: NSNotification.Name.NMAPositioningManagerDidUpdatePosition, object: positionManager.sharedInstance())
NotificationCenter.default.addObserver(self, selector: #selector(self.didLosePosition), name: NSNotification.Name.NMAPositioningManagerDidLosePosition, object: positionManager.sharedInstance())
}
super.viewWillAppear(animated)
mapView.zoomLevel = 17
mapView.set(geoCenter: NMAGeoCoordinates(latitude: 45.921189263505788, longitude: 14.234863696633125),
animation: .linear)
let mapMarker = NMAMapMarker(geoCoordinates: NMAGeoCoordinates(latitude: 45.921189263505788, longitude: 14.234863696633125), icon: NMAImage(uiImage: UIImage(named: "driver") ?? UIImage())!)
mapView.positionIndicator.isVisible = true;
mapView.positionIndicator.isAccuracyIndicatorVisible = true
mapView.positionIndicator.set(displayObject: mapMarker, toLayer: NMAMapLayerType.foreground)
mapView.copyrightLogoPosition = NMALayoutPosition.centerRight
}
func mapView(_ mapView: NMAMapView, didReceivePan translation: CGPoint, at location: CGPoint) {
follow = false
}
#IBAction func followButton(_ sender: UIButton) {
follow = true
mapView.set(geoCenter: (positionManager.sharedInstance().currentPosition?.coordinates)!,
animation: .linear)
mapView.zoomLevel = 17
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

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

location manager function is not getting called

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.

Resources