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!
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
Last edited: