Location Exhibit Inwards Ios Eight Alongside Swift

What nosotros volition travel doing is a unproblematic app the tells your location base of operations on the longitude as well as latitude.


   For making things simpler, I already set-up the UI inwards this link. Once y'all accept downloaded the files become to storyboard as well as modify the size shape from wAny|hANy to wCompact|hRegular. Here is what the even out board should await like:





First, add together "CoreLocation" to the framework.

Once added, within the "ViewController.swift" import CoreLocation as well as add CLLocationManagerDelegate.

Then declare the next variables:

    var locationManager : CLLocationManager = CLLocationManager()          var geocoder : CLGeocoder = CLGeocoder()     var placemark : CLPlacemark = CLPlacemark() 

Then insert the next lines of code:

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {         println("didFailWithError %@", error)         var errorAlert : UIAlertView = UIAlertView(title: "Error", message: "Failed to instruct your location", delegate: nil, cancelButtonTitle: "OK")              }          func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {         //println("didUpdateToLocation %@",locations)                  var currentLocation : CLLocation = locations[0] equally CLLocation                  if currentLocation != nil{             var stringLongitude : NSString = NSString(format: "%0.8f", currentLocation.coordinate.longitude)             var stringLatitude : NSString = NSString(format: "%0.8f", currentLocation.coordinate.latitude)             lbl_longitude.text = stringLongitude             lbl_latitude.text = stringLatitude         }                  locationManager.stopUpdatingLocation()                       CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void inwards                          if mistake {                 //println(“Reverse geocoder failed amongst error” + error.localizedDescription)                 println("Reverse geocode failed amongst error")                 supply             }                          if placemarks.count > 0 {                 permit pm = placemarks[0] equally CLPlacemark                 self.displayLocationInfo(pm)             } else {                // println(“Problem amongst the information received from geocoder”)                 println("Problem amongst the engagement recieved from geocoder")             }          })                   }          func displayLocationInfo(placemark: CLPlacemark) {         if placemark != zilch {                          var tempString : String = ""                          if(placemark.locality != nil){                 tempString = tempString +  placemark.locality + "\n"             }             if(placemark.postalCode != nil){               tempString = tempString +  placemark.postalCode + "\n"             }             if(placemark.administrativeArea != nil){                 tempString = tempString +  placemark.administrativeArea + "\n"             }             if(placemark.country != nil){                tempString = tempString +  placemark.country + "\n"             }                          lbl_address.text = tempString                      }     } 

After inserting the lines of code, insert this lines of code within the push method:


        locationManager.delegate = self         locationManager.desiredAccuracy = kCLLocationAccuracyBest         locationManager.requestWhenInUseAuthorization()         locationManager.startUpdatingLocation() 

Last, within the Info.plist, add NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription amongst both boolean value laid to YES.

And that's it. Build as well as Run the projection as well as y'all should directly travel able encounter your address.
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.