使用iphone mapkit获取tapped坐标

时间:2023-02-05 20:44:56

I'm making an app using apple's mapkit framework. What I want to do is to get longitude and latitude from a location that you press. I get the coordinates from the users current location using this code:

我正在使用apple的mapkit框架制作应用程序。我想要做的是从你按的位置获取经度和纬度。我使用以下代码从用户当前位置获取坐标:

- (IBAction)longpressToGetLocation:(id)sender {
    CLLocationCoordinate2D location = [[[self.mapView userLocation] location] coordinate];
    NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);
}

How do I get that code to show the pressed location instead of userlocation?

如何让代码显示按下的位置而不是用户位置?

3 个解决方案

#1


51  

First of all, use a UIGestureRecognizer instead an IBAction

首先,使用UIGestureRecognizer而不是IBAction

- (void)longpressToGetLocation:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
        return;

    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];   
    CLLocationCoordinate2D location = 
        [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

    NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

}

Swift 2:

斯威夫特2:

@IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
    if sender.state != UIGestureRecognizerState.Began { return }
    let touchLocation = sender.locationInView(protectedMapView)
    let locationCoordinate = protectedMapView.convertPoint(touchLocation, toCoordinateFromView: protectedMapView)
    print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
}

Swift 3:

斯威夫特3:

@IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
    if sender.state != UIGestureRecognizerState.began { return }
    let touchLocation = sender.location(in: protectedMapView)
    let locationCoordinate = protectedMapView.convert(touchLocation, toCoordinateFrom: protectedMapView)
    print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
}

#2


4  

class mapviewCtrl: UIViewController, UIGestureRecognizerDelegate
{
    @IBOutlet var mapViewVar: MKMapView!
    override func viewDidLoad() {
    super.viewDidLoad()
    let lpgr = UILongPressGestureRecognizer(target: self, action:"handleLongPress:")
    lpgr.minimumPressDuration = 0.5
    lpgr.delaysTouchesBegan = true
    lpgr.delegate = self
    self.mapViewVar.addGestureRecognizer(lpgr)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
    if gestureReconizer.state != UIGestureRecognizerState.Ended {
        let touchLocation = gestureReconizer.locationInView(mapViewVar)
        let locationCoordinate = mapViewVar.convertPoint(touchLocation,toCoordinateFromView: mapViewVar)
        println("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
        return
    }
    if gestureReconizer.state != UIGestureRecognizerState.Began {
        return
    }
}

Swift 4.

斯威夫特4。

class mapviewCtrl: UIViewController, UIGestureRecognizerDelegate
{
  @IBOutlet var mapViewVar: MKMapView!
  override func viewDidLoad() {
  super.viewDidLoad()
  self.setMapview()
}
func setMapview(){
  let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(mapviewCtrl.handleLongPress(gestureReconizer:)))
  lpgr.minimumPressDuration = 0.5
  lpgr.delaysTouchesBegan = true
  lpgr.delegate = self
  self.mapViewVar.addGestureRecognizer(lpgr)
} 

@objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
  if gestureReconizer.state != UIGestureRecognizerState.ended {
    let touchLocation = gestureReconizer.location(in: mapViewVar)
    let locationCoordinate = mapViewVar.convert(touchLocation,toCoordinateFrom: mapViewVar)
    print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
    return
  }
  if gestureReconizer.state != UIGestureRecognizerState.began {
    return
  }
}
}

#3


2  

Another way for swift 3 will be this one: This is a mashup of few snippets -- with the override you only update the code in one place which can be convinient, enhance modularity, and remove assumptions and potential crash points.

swift 3的另一种方式是这一个:这是一个包含少量片段的混搭 - 使用覆盖你只需在一个地方更新代码,这可以很方便,增强模块性,并删除假设和潜在的崩溃点。

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    // Let's put in a log statement to see the order of events
    print(#function)

    for touch in touches {
        let touchPoint = touch.location(in: self.mapView)
        let location = self.mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
        print ("\(location.latitude), \(location.longitude)")
    }
}

#1


51  

First of all, use a UIGestureRecognizer instead an IBAction

首先,使用UIGestureRecognizer而不是IBAction

- (void)longpressToGetLocation:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
        return;

    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];   
    CLLocationCoordinate2D location = 
        [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

    NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

}

Swift 2:

斯威夫特2:

@IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
    if sender.state != UIGestureRecognizerState.Began { return }
    let touchLocation = sender.locationInView(protectedMapView)
    let locationCoordinate = protectedMapView.convertPoint(touchLocation, toCoordinateFromView: protectedMapView)
    print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
}

Swift 3:

斯威夫特3:

@IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
    if sender.state != UIGestureRecognizerState.began { return }
    let touchLocation = sender.location(in: protectedMapView)
    let locationCoordinate = protectedMapView.convert(touchLocation, toCoordinateFrom: protectedMapView)
    print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
}

#2


4  

class mapviewCtrl: UIViewController, UIGestureRecognizerDelegate
{
    @IBOutlet var mapViewVar: MKMapView!
    override func viewDidLoad() {
    super.viewDidLoad()
    let lpgr = UILongPressGestureRecognizer(target: self, action:"handleLongPress:")
    lpgr.minimumPressDuration = 0.5
    lpgr.delaysTouchesBegan = true
    lpgr.delegate = self
    self.mapViewVar.addGestureRecognizer(lpgr)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
    if gestureReconizer.state != UIGestureRecognizerState.Ended {
        let touchLocation = gestureReconizer.locationInView(mapViewVar)
        let locationCoordinate = mapViewVar.convertPoint(touchLocation,toCoordinateFromView: mapViewVar)
        println("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
        return
    }
    if gestureReconizer.state != UIGestureRecognizerState.Began {
        return
    }
}

Swift 4.

斯威夫特4。

class mapviewCtrl: UIViewController, UIGestureRecognizerDelegate
{
  @IBOutlet var mapViewVar: MKMapView!
  override func viewDidLoad() {
  super.viewDidLoad()
  self.setMapview()
}
func setMapview(){
  let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(mapviewCtrl.handleLongPress(gestureReconizer:)))
  lpgr.minimumPressDuration = 0.5
  lpgr.delaysTouchesBegan = true
  lpgr.delegate = self
  self.mapViewVar.addGestureRecognizer(lpgr)
} 

@objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
  if gestureReconizer.state != UIGestureRecognizerState.ended {
    let touchLocation = gestureReconizer.location(in: mapViewVar)
    let locationCoordinate = mapViewVar.convert(touchLocation,toCoordinateFrom: mapViewVar)
    print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
    return
  }
  if gestureReconizer.state != UIGestureRecognizerState.began {
    return
  }
}
}

#3


2  

Another way for swift 3 will be this one: This is a mashup of few snippets -- with the override you only update the code in one place which can be convinient, enhance modularity, and remove assumptions and potential crash points.

swift 3的另一种方式是这一个:这是一个包含少量片段的混搭 - 使用覆盖你只需在一个地方更新代码,这可以很方便,增强模块性,并删除假设和潜在的崩溃点。

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    // Let's put in a log statement to see the order of events
    print(#function)

    for touch in touches {
        let touchPoint = touch.location(in: self.mapView)
        let location = self.mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
        print ("\(location.latitude), \(location.longitude)")
    }
}