Created
August 6, 2016 09:52
-
-
Save tkc/7dc9674ce3c66ec307d6f828095a41ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MapKit | |
import UIKit | |
import CoreLocation | |
class MapGetAddresSandbox: UIViewController,CLLocationManagerDelegate{ | |
var mapInit:Bool=false; | |
var button: UIButton! | |
var currentLatitude:CLLocationDegrees=35; | |
var currentLongitude:CLLocationDegrees=137; | |
private var locationManager:CLLocationManager! | |
private var mapView: MKMapView = MKMapView() | |
private var lat:CLLocationDegrees = 35 | |
private var long:CLLocationDegrees = 137.1 | |
private var latitudeDelta:CLLocationDegrees = 0 | |
private var longitudeDelta:CLLocationDegrees = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
mapView.frame = self.view.frame | |
mapView.mapType=MKMapType.Standard | |
locationManager = CLLocationManager() | |
locationManager.delegate = self | |
self.view.addSubview(mapView) | |
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long) | |
mapView.setCenterCoordinate(center, animated: true) | |
let mySpan: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta) | |
let myRegion: MKCoordinateRegion = MKCoordinateRegionMake(center, mySpan) | |
mapView.region = myRegion | |
mapView.setCenterCoordinate(center, animated: true) | |
button = UIButton(frame: CGRectMake(0, 0,100, 100)) | |
button.backgroundColor = UIColor.redColor() | |
button.layer.cornerRadius = 50.0 | |
button.layer.position = CGPointMake(self.view.frame.width/2, self.view.frame.height-150) | |
button.setTitle("+", forState: .Normal) | |
button.setTitleColor(UIColor.whiteColor(), forState: .Normal) | |
button.addTarget(self, action: #selector(self.setLocation(_:)), forControlEvents: .TouchUpInside) | |
self.view.addSubview(button) | |
} | |
func setLocation(sender: UIButton){ | |
print(currentLatitude) | |
print(currentLongitude) | |
self.updateMapPoint(currentLatitude,long:currentLongitude) | |
let data = self.getPin(currentLatitude,long:currentLongitude,text: "Here!!"); | |
mapView.addAnnotation(data) | |
self.getGeoLocation(locationManager) | |
} | |
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { | |
var statusStr = ""; | |
switch (status) { | |
case .NotDetermined: | |
statusStr = "NotDetermined" | |
manager.requestWhenInUseAuthorization() | |
case .Restricted: | |
statusStr = "Restricted" | |
case .Denied: | |
statusStr = "Denied" | |
case .AuthorizedAlways: | |
statusStr = "AuthorizedAlways" | |
case .AuthorizedWhenInUse: | |
statusStr = "AuthorizedWhenInUse" | |
} | |
print(" CLAuthorizationStatus: \(statusStr)") | |
locationManager.startUpdatingLocation() | |
} | |
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
self.currentLatitude = manager.location!.coordinate.latitude; | |
self.currentLongitude = manager.location!.coordinate.longitude; | |
if(mapInit==false){ | |
self.updateMapPoint(currentLatitude,long:currentLongitude) | |
mapInit=true | |
} | |
} | |
func getGeoLocation(manager: CLLocationManager){ | |
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in | |
if error != nil { | |
print(error!.localizedDescription) | |
return | |
} | |
if placemarks!.count > 0 { | |
let placemark = placemarks![0] as CLPlacemark | |
let address = self.displayLocationInfo(placemark) | |
let alert = UIAlertView() | |
alert.title = "Address" | |
alert.message = address | |
alert.addButtonWithTitle("OK") | |
alert.show() | |
} else { | |
print("error") | |
} | |
}) | |
} | |
func displayLocationInfo(placemark: CLPlacemark)->String { | |
var addressStinrg: String = "" | |
addressStinrg = placemark.locality != nil ? placemark.locality! : "" | |
addressStinrg += " " | |
addressStinrg += placemark.postalCode != nil ? placemark.postalCode! : "" | |
addressStinrg += " " | |
addressStinrg += placemark.subLocality != nil ? placemark.subLocality! : "" | |
addressStinrg += " " | |
addressStinrg += placemark.administrativeArea != nil ? placemark.administrativeArea! : "" | |
addressStinrg += " " | |
addressStinrg += placemark.country != nil ? placemark.country! : "" | |
return addressStinrg | |
} | |
func updateMapPoint(lat: CLLocationDegrees,long: CLLocationDegrees) { | |
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long) | |
mapView.setCenterCoordinate(center, animated: true) | |
} | |
func locationManager(manager: CLLocationManager,didFailWithError error: NSError){ | |
print("error") | |
} | |
func getPin(lat: CLLocationDegrees,long: CLLocationDegrees,text:String) -> MKPointAnnotation { | |
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long) | |
let myPin: MKPointAnnotation = MKPointAnnotation() | |
myPin.coordinate = center | |
myPin.title = text | |
return myPin | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment