2015-07-13 33 views
1

所以我创建了一个应用程序,当点击一个按钮时,用户当前的位置被用来在Apples mapkit上创建一个注解。除了将按钮链接到将创建注释并在地图上显示注释的代码之外,我所做的一切工作。贝娄是我到目前为止的代码:单击按钮时,如何使用用户当前位置显示注释?

import UIKit 
import CoreLocation 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate { 

var locationManager = CLLocationManager() 

@IBOutlet var Map: MKMapView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    locationManager.delegate = self 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.startUpdatingLocation() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) { 

    println("Updating Location \(newLocation.coordinate.latitude) , \(newLocation.coordinate.longitude) ") 

    let span = MKCoordinateSpanMake(0.0009, 0.0009) 
    let region = MKCoordinateRegion(center: newLocation.coordinate, span: span) 
    Map.setRegion(region, animated: false) 
    } 
} 

正如你所看到的,我有它,所以它不断地显示了他们的用户。我想要这个,但我也希望用户能够创建注释。

我的问题是我需要什么代码来创建注释,它到底在哪里?我对编程相当陌生,所以我不完全全面地使用所有的代码术语,所以如果你愿意的话(请为我省下一些麻烦)。另外,当用户单击添加注释的按钮时,最后一个注释需要被删除。我将如何做到这一点?我在Xcode中使用Swift。

回答

1

你快到了!

  • 首先,在码(只是类内)的顶部创建注释:

    var annotation = MKPointAnnotation()

  • 然后,使用newLocation.coordinate.latitude.longitude创建CLLocationCoordinate2D并将其设置为annotation.coordinate

    • 然后使用map.title = "something"map.addAnnotation(annotation)

    • 您需要让某些变量更具全局性才能达到此目的,但在放置注释之前,只需使用map.removeAnnotation(annotation) - 因此您一次只能在屏幕上显示一个注释。

+0

好了,这里是我的: –

+0

进口的UIKit 进口CoreLocation 进口MapKit 类的ViewController:UIViewController中,CLLocationManagerDelegate { VAR的LocationManager添加= CLLocationManager(CLLocationManager为) //新代码 var注解= MKPointAnnotation() // @IBOutlet var Map:MKMapView! 重写func viewDidLoad(){ super.viewDidLoad() //在加载视图后,通常从一个笔尖执行任何其他设置。 locationManager.delegate =自 locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } –

+0

倍率FUNC didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() //处置,可以被重新创建任何资源。 (更新位置\(newLocation.coordinate.latitude),\(newLocation.coordinate。)(位置更新位置\(newLocation.coordinate.latitude),\(newLocation.coordinate.laordinate。)(012LX} 函数locationManager(manager:CLLocationManager !, didUpdateToLocation newLocation:CLLocation !, fromLocation oldLocation:CLLocation!){ println经度)“) //新的代码添加 newLocation.coordinate.latitude newLocation.coordinate.longitude 让跨度= MKCoordinateSpanMake(0.0009,0.0009) –

相关问题