简体   繁体   English

单击按钮后,如何使用“用户当前位置”显示注释?

[英]How do I display an annotation using the Users current location when a button is clicked?

So I am creating an app that when a button is clicked, the users current location is used to create an annotation on Apples mapkit. 因此,我正在创建一个应用程序,当单击按钮时,将使用用户当前位置在Apples mapkit上创建注释。 I have everything working except for linking the button up to code that will create the annotation and display the annotation on the map. 除了将按钮链接到将创建注释并在地图上显示注释的代码之外,我已经完成了所有工作。 Bellow is the code I have so far: 贝娄是我到目前为止的代码:

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

As you will see, I have it so it constantly shows the user where they are. 如您所见,我拥有它,因此它不断向用户显示他们的位置。 I want this but I also want the user to be able to create the annotation. 我想要这个,但是我也希望用户能够创建注释。

My question is what code do I need to create the annotations and where does it go? 我的问题是创建注释需要什么代码,它在哪里? I am fairly new to programing so I'm not completely comprehensive of all of the code lingo so if you would (dumb things down for me please). 我是编程的新手,所以我对所有的代码术语并不完全了解,所以请(对我来说愚蠢的东西)。 Also, when the user clicks the button that adds the annotation, the last annotation needs to be deleted. 同样,当用户单击添加注释的按钮时,需要删除最后一个注释。 How would I do this? 我该怎么做? I am using Swift in Xcode. 我在Xcode中使用Swift。

You're almost there! 你快到了!

  • First, create an annotation at the top of the code (just within the class): 首先,在代码顶部(在类内)创建一个注释:

    var annotation = MKPointAnnotation()

  • Then, use newLocation.coordinate.latitude and .longitude to create a CLLocationCoordinate2D and set that to annotation.coordinate . 然后,使用newLocation.coordinate.latitude.longitude创建一个CLLocationCoordinate2D并将其设置为annotation.coordinate .longitude

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

    • You'll need to have some of the variables more global to achieve this, but before you place the annotation, simply use map.removeAnnotation(annotation) - so you only ever have one annotation on the screen at a time. 您需要使某些变量具有全局性才能实现此目的,但是在放置注释之前,只需使用map.removeAnnotation(annotation) -这样您一次只能在屏幕上拥有一个注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在“ prepareForSegue”中时,如何访问当前注释的NSString? - How do I access the current annotation's NSString when in “prepareForSegue”? 单击当前ViewController中的按钮时,如何显示当前ViewController的精确副本? - How to display exact copy of current ViewController when a button in current ViewController is clicked? 单击UITextField时如何显示联系人? - How do I display contacts when UITextField is clicked? 单击按钮时如何更改按钮的文本? -迅捷 - How do I change the text of a button when it is clicked? - Swift 如何使用CoreLocation在当前位置添加注释 - How to Add an annotation At your current location using CoreLocation 单击时如何删除按钮的选定状态? - How do I remove button selected state when clicked? 单击按钮时如何打开电话设置? - How do I open phone settings when a button is clicked? 如何使用 React Native 在 Mapview 中显示当前位置? - How do I show the current location in a Mapview using React Native? 如何在MKMapView上显示自定义注释? - How do I display a custom annotation on MKMapView? 如何使用Parse查找当前用户位置附近的用户? - How can I find users near the current user's location using Parse?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM