ios - current - Obtener coordenadas marcadas con iPhone Mapkit
mapkit tutorial (3)
Estoy haciendo una aplicación usando el marco de mapkit de Apple. Lo que quiero hacer es obtener la longitud y la latitud de una ubicación que presione. Obtengo las coordenadas de la ubicación actual de los usuarios usando este código:
- (IBAction)longpressToGetLocation:(id)sender {
CLLocationCoordinate2D location = [[[self.mapView userLocation] location] coordinate];
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);
}
¿Cómo obtengo ese código para mostrar la ubicación presionada en lugar de la ubicación del usuario?
En primer lugar, use un UIGestureRecognizer
lugar de un IBAction
- (void)longpressToGetLocation:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D location =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);
}
Swift 2:
@IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
if sender.state != UIGestureRecognizerState.Began { return }
let touchLocation = sender.locationInView(protectedMapView)
let locationCoordinate = protectedMapView.convertPoint(touchLocation, toCoordinateFromView: protectedMapView)
print("Tapped at lat: /(locationCoordinate.latitude) long: /(locationCoordinate.longitude)")
}
Swift 3:
@IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
if sender.state != UIGestureRecognizerState.began { return }
let touchLocation = sender.location(in: protectedMapView)
let locationCoordinate = protectedMapView.convert(touchLocation, toCoordinateFrom: protectedMapView)
print("Tapped at lat: /(locationCoordinate.latitude) long: /(locationCoordinate.longitude)")
}
Otra forma para swift 3 será esta: esta es una combinación de algunos fragmentos: con la anulación solo actualiza el código en un lugar que puede ser conveniente, mejorar la modularidad y eliminar suposiciones y posibles puntos de bloqueo.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Let''s put in a log statement to see the order of events
print(#function)
for touch in touches {
let touchPoint = touch.location(in: self.mapView)
let location = self.mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
print ("/(location.latitude), /(location.longitude)")
}
}
class mapviewCtrl: UIViewController, UIGestureRecognizerDelegate
{
@IBOutlet var mapViewVar: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let lpgr = UILongPressGestureRecognizer(target: self, action:"handleLongPress:")
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.mapViewVar.addGestureRecognizer(lpgr)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.Ended {
let touchLocation = gestureReconizer.locationInView(mapViewVar)
let locationCoordinate = mapViewVar.convertPoint(touchLocation,toCoordinateFromView: mapViewVar)
println("Tapped at lat: /(locationCoordinate.latitude) long: /(locationCoordinate.longitude)")
return
}
if gestureReconizer.state != UIGestureRecognizerState.Began {
return
}
}
Swift 4.
class mapviewCtrl: UIViewController, UIGestureRecognizerDelegate
{
@IBOutlet var mapViewVar: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.setMapview()
}
func setMapview(){
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(mapviewCtrl.handleLongPress(gestureReconizer:)))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.mapViewVar.addGestureRecognizer(lpgr)
}
@objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.ended {
let touchLocation = gestureReconizer.location(in: mapViewVar)
let locationCoordinate = mapViewVar.convert(touchLocation,toCoordinateFrom: mapViewVar)
print("Tapped at lat: /(locationCoordinate.latitude) long: /(locationCoordinate.longitude)")
return
}
if gestureReconizer.state != UIGestureRecognizerState.began {
return
}
}
}