Search code examples
iphoneobjective-cxcodemapkitcllocation

Selecting Location from MapView


I was thinking of an application where user could select a location on an iphone app. I have googled it and didn't find anything useful.

My question is, is it possible to let the user select a location from an iphone app using MapKit/CLLocation? If yes, please help me where should i start.

Thanks


Solution

  • You can add a long press gesture recognizer to the map:

    UILongPressGestureRecognizer* lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.5;
    lpgr.delegate = self;
    [self.map addGestureRecognizer:lpgr];
    [lpgr release];
    

    In the handle long press method get the CLLocationCordinate2D:

    - (void) handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        /*
         Only handle state as the touches began
         set the location of the annotation
         */
    
        CLLocationCoordinate2D coordinate = [self.map convertPoint:[gestureRecognizer locationInView:self.map] toCoordinateFromView:self.map];
    
        [self.map setCenterCoordinate:coordinate animated:YES];
    
      // Do anything else with the coordinate as you see fit in your application
    
       }
    }