Long Press Annotation on a Map

Discussion in 'iOS Programming' started by joaogfmoreira, Mar 13, 2015.

  1. joaogfmoreira, Mar 13, 2015
    Last edited: Mar 13, 2015

    joaogfmoreira macrumors newbie

    Joined:
    Feb 24, 2015
    #1
    Hello guys!

    I have been trying to add an annotation to a Map View by long pressing on it for two seconds but I can't get it to work. I have attached two files containing my code. They include a set annotation in New York and the part of the long press, which i can't finish.
    Please ignore the comments,
    Thanks!

    Code:
    //
    //  ViewController.swift
    //  Learning Maps
    //
    //  Created by João Moreira on 10/03/15.
    //  Copyright (c) 2015 JoaoMoreira. All rights reserved.
    //
    
    import UIKit
    
    import MapKit
    
    class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    
        @IBOutlet var myMap: MKMapView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            var latitude:CLLocationDegrees = 40.748712         
            
            var longitude:CLLocationDegrees = -73.985728        
    
            var latDelta:CLLocationDegrees = 0.01        
            
            var lonDelta:CLLocationDegrees = 0.01    
    
            var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)  
            
            var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
            
            var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)  
            
            myMap.setRegion(region, animated: true)
    
            var annotation = MKPointAnnotation()
            
            annotation.coordinate = location
            
            annotation.title = "Statue of liberty"
            
            annotation.subtitle = "One day I'll go here..."
            
            myMap.addAnnotation(annotation)
    
            var uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")  
                                                                                        
            uilpgr.minimumPressDuration = 2.0
            
            myMap.addGestureRecognizer(uilpgr)
    
        }
    
    func action(gestureRecognizer:UIGestureRecognizer) {        
            
            println("test")
            
            var touchPoint = gestureRecognizer.locationInView(self.myMap)  
            
            var newCoordinate:CLLocationCoordinate2D = myMap.convertPoint(touchPoint, toCoordinateFromView: self.myMap)
            
            var newAnnotation = MKPointAnnotation()
            
            newAnnotation.coordinate = newCoordinate
            
            newAnnotation.title = "I will come here"
            
            newAnnotation.subtitle = "for sure"
    
            myMap.addAnnotation(newAnnotation)
            
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
     

    Attached Files:

  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Can you please copy-and-paste the code snippets (put them inside
    Code:
     tags) into a post rather than attaching them as images? That's easier for us to deal with.
     
  3. joaogfmoreira thread starter macrumors newbie

    Joined:
    Feb 24, 2015
    #3
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Alright, thanks!

    Now, with that out of the way, let's get to the issue: When you say "it doesn't work", can you be more specific? What do you expect to happen and what happens instead? What debugging, if any, have you done?
     
  5. joaogfmoreira thread starter macrumors newbie

    Joined:
    Feb 24, 2015
    #5
    Well, I want it to add a Pin in the place in the map where I do the long pressing. When i run the simulator, the map opens at New York and if I try to long press it, it will just do nothing. The long press is not working at all. Even the "test" that I printed to the log does not work.
     
  6. Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #6
    This is going to be obvious but can you confirm you're pressing for at least two seconds? Does it work if you comment out the minimumPressDuration line?
     
  7. joaogfmoreira thread starter macrumors newbie

    Joined:
    Feb 24, 2015
    #7
    Yes i clicked more than 2 seconds! Also tried your suggestion on commenting the minimumPressDuration line but still it does nothing.
     
  8. Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #8
    I've made a new project with your sample code and it works fine. Try placing a breakpoint in the action method and then doing the long press gesture. Does a long press trigger the breakpoint?
     
  9. joaogfmoreira thread starter macrumors newbie

    Joined:
    Feb 24, 2015
    #9
    That fixed it! I don't know what was wrong in the first place. But i placed the breakpoint and the long press was working and than i restarted the app and it was working! Thank you! Sorry for the trouble!
     
  10. Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #10

    Not a problem.
     

Share This Page