swift - How can I mark a location on a map by clicking a button -
i want make when button clicked, current location of device saved. then, pin displayed on map @ current location. how do this? using swift.
import uikit import corelocation import mapkit class viewcontroller: uiviewcontroller, cllocationmanagerdelegate { var locationmanager = cllocationmanager() var myposition = cllocationcoordinate2d() @iboutlet var map: mkmapview! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. locationmanager.delegate = self locationmanager.requestwheninuseauthorization() locationmanager.startupdatinglocation() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func locationmanager(manager: cllocationmanager!, didupdatetolocation newlocation: cllocation!, fromlocation oldlocation: cllocation!) { println("updating location \(newlocation.coordinate.latitude) , \(newlocation.coordinate.longitude) ") myposition = newlocation.coordinate let span = mkcoordinatespanmake(0.0009, 0.0009) let region = mkcoordinateregion(center: newlocation.coordinate, span: span) map.setregion(region, animated: true) }
if handle authorization request correctly, locationmanager didupdatelocation should set myposition property.
add button viewcontroller , create ibaction handle touch inside event. in ibaction call map.addannotation. added annotation has several properties, namely coordinate, use myposition.
before start adding annotations, make sure locationmanager didupdatelocation working. there things need handled, checking authorization , added nslocationwheninusedescription...
Comments
Post a Comment