问题
I'm using iOS 6, and I've read dozens of ways to accomplish this (including many from stack overflow) without success.
Here's what I've tried, separated into "stages":
Create a
UILongTouchGestureRecognizer
to receive long touches for theMKMapView
.- I've tried adding a
UILongTouchGestureRecognizer
via my Storyboard, and hooking up the outlets, delegates, etc. through the Connections Inspector. - I've tried creating the
UILongTouchGestureRecognizer
programmatically, initializing it usingmapView
as target,self
as target, andself.view
as target.
- I've tried adding a
Receive the touch gesture using the selector method from the
UILongTouchGestureRecognizer
, get theCGPoint
, and convert it to aCLLocationCoordinate2D
object.- I've tried:
- Using [mapView convertPoint:(CGPoint) toCoordinateFromView:self.mapView];
- Using
MKMapPoint aPoint = MKMapPointMake(aCGPoint.x, aCGPoint.y);
first, and then usingMKCoordinateForMapPoint(aMapPoint)
to get theCLLocationCoordinate2D
. - Both Directly accessing the UILongPressGestureRecognizer and using the
(UILongPressGestureRecognizer *)sender
from the method call to get the x,y for the CGRect.
- I've tried:
Results
- In a nutshell here is what I've been getting when checking the values using
NSLog
, irregardless of the methods used. - For the X,Y given from theUILongPressGestureRecognizer
, x seems to fluctuate to somewhere in the range of 200, and 420. Y ranges from ~400 - 700. - The latitudes and longitudes triggered by a long touch & printed to the log, strangely, only vary in the 3rd - 6th decimal values after the decimal point of 85.051XX (latitude) and -179.9997XX (longitude).
- In a nutshell here is what I've been getting when checking the values using
Here's examples of some of the code I've tried
- (void)viewDidLoad
{
NSLog(@"View did load");
[super viewDidLoad];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:mapView action:@selector(handleLongPress:)];
mapView = [[MKMapView alloc] init];
}
- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender
{
NSLog(@"CGPoint point: x - %f y - %f", point.x, point.y);
CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
NSLog(@"Coords from \"locCoord\": lat - %f lng - %f", locCoord.latitude, locCoord.lon);
MKPointAnnotation *addAnnotation = [[MKPointAnnotation alloc] init];
[addAnnotation setCoordinate:
[self.mapView addAnnotation:addAnnotation];
}
Hope someone can help me, because I'm completely stumped on this one.
Just to reiterate, the end goal is to get a coordinate of where the user "long pressed a mapView", and then (something else I can't get to work) drop a pin at that location.
回答1:
Looking back at an old project of mine that works in iOS 6 we seem to have almost identical code (same source tutorial I guess). The difference being that I am checking the state
and that my mapView variable is not being reset in the viewDidLoad. You seem to be allocating a new one after the gesture recogniser and you haven't shown any code for adding it to your viewController. I'd guess that you've managed to separate the one on screen from the one in code. If your MKMapview is meant to be on screen through the life of the viewcontroller, let IB manage the allocation and assignment.
- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
CGPoint point = [recognizer locationInView:mapView];
CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
[self addPinAtLocation:locCoord];
}
}
来源:https://stackoverflow.com/questions/13336650/cant-get-coordinates-from-mkmapview-using-uilongtouchgesturerecognizer