1

当用户在视图中长按时,我想从地图视图中获取 GPS 坐标。

目前我正在使用:

- (void)viewDidLoad
{
    [super viewDidLoad];
    mapView.showsUserLocation =YES;
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(mapLongPress:)];
    longPressGesture.minimumPressDuration = 1.5;
    [mapView addGestureRecognizer:longPressGesture];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)mapLongPress:(UILongPressGestureRecognizer *)gestureRecognizer{
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
        CGPoint touchLocation = [gestureRecognizer locationInView:mapView];

        CLLocationCoordinate2D coordinate;
        coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];// how to convert this to a String or something else?
        NSLog(@"Longpress");
    }
}

但是我如何获得坐标?从坐标 = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];?

4

1 回答 1

1

用代码回答你的问题:

“如何将其转换为字符串或其他内容?”

NSLog(@"LongPress coordinate: latitude = %f, longitude  = %f", coordinate.latitude, coordinate.longitude);

地理坐标由纬度和经度表示。
使用您使用CLLocationCoordinate2D的 ios,其中包含坐标组件纬度和经度。

纬度范围 [-90.0, 90.0]
经度范围 [-180.0, 180.0]

您可以使用CLLocationCoordinate2D或您自己的结构来存储它。

于 2013-02-01T16:28:10.497 回答