---
title: "长按地图获取经纬度 添加图钉"
author: "修博龙泉"
date: "2012-12-07T17:09:40Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2012-12-07T17:38:32Z"
id: 1743872
url: "https://longquan.iteye.com/blog/1743872"
---

长按地图获取经纬度

> 
> 
>     UILongPressGestureRecognizer *lpress = [[uilongpressgesturerecognizer alloc] initWithTarget:self action:@selector(longPress:)];
>     lpress.minimumPressDuration = 0.3;//按0.5秒响应longPress方法
>     lpress.allowableMovement = 10.0;
>     //给MKMapView加上长按事件
>     [mapview addgesturerecognizer:lpress];//mapView是MKMapView的实例
>     [lpress release];
> 
> - (void)longPress:(UIGestureRecognizer*)gestureRecognizer {
>     if (gestureRecognizer.state == UIGestureRecognizerStateBegan){  //这个状态判断很重要
>         //坐标转换
>         CGPoint touchPoint = [gesturerecognizer locationinview:self.mapview];
>         CLLocationCoordinate2D touchMapCoordinate =
>         [self.mapview convertpoint:touchpoint tocoordinatefromview:self.mapview];
>         //这里的touchMapCoordinate.latitude和touchMapCoordinate.longitude就是你要的经纬度，
>         NSLog(@"%f",touchMapCoordinate.latitude);
>         NSLog(@"%f",touchMapCoordinate.longitude);
>     }
> }
> 


长按添加图钉

```java

- (void)longPress:(UIGestureRecognizer*)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan){  //这个状态判断很重要
        //坐标转换
        CGPoint touchPoint = [gesturerecognizer locationinview:self.mapview];
        CLLocationCoordinate2D touchMapCoordinate =
        [self.mapview convertpoint:touchpoint tocoordinatefromview:self.mapview];
        //这里的touchMapCoordinate.latitude和touchMapCoordinate.longitude就是你要的经纬度，
        NSLog(@"%f",touchMapCoordinate.latitude);
        NSLog(@"%f",touchMapCoordinate.longitude);
        //30.264998 120.122538   30.285012 120.117989
        LocationObject *aLocationObject = [[locationobject alloc]initWithTitle:@"hello" latitue:touchMapCoordinate.latitude longitude:touchMapCoordinate.longitude];
        aLocationObject._subTitleString = @"world";
        NSMutableArray *_mapAnnotations = [[nsmutablearray alloc]initWithCapacity:1];
        [_mapannotations addobject:alocationobject];
        [self.mapview addannotations:_mapannotations ];
        [_mapannotations release];
        [alocationobject release];

    }
}


```