使用iphone mapkit获取tapped坐标 [英] get tapped coordinates with iphone mapkit

查看:93
本文介绍了使用iphone mapkit获取tapped坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

- (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?

推荐答案

首先,使用 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:

@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:

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

这篇关于使用iphone mapkit获取tapped坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆