Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

joaogfmoreira

macrumors newbie
Original poster
Feb 24, 2015
24
0
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.
    }
 

Attachments

  • Screen Shot 2015-03-13 at 17.52.23.png
    Screen Shot 2015-03-13 at 17.52.23.png
    107.8 KB · Views: 191
  • Screen Shot 2015-03-13 at 17.52.01.png
    Screen Shot 2015-03-13 at 17.52.01.png
    142.7 KB · Views: 150
Last edited:

joaogfmoreira

macrumors newbie
Original poster
Feb 24, 2015
24
0
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?

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.
 

Ubuntu

macrumors 68020
Jul 3, 2005
2,140
474
UK/US
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.

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?
 

joaogfmoreira

macrumors newbie
Original poster
Feb 24, 2015
24
0
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?

Yes i clicked more than 2 seconds! Also tried your suggestion on commenting the minimumPressDuration line but still it does nothing.
 

Ubuntu

macrumors 68020
Jul 3, 2005
2,140
474
UK/US
Yes i clicked more than 2 seconds! Also tried your suggestion on commenting the minimumPressDuration line but still it does nothing.

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?
 

joaogfmoreira

macrumors newbie
Original poster
Feb 24, 2015
24
0
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?

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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.