Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get tapped coordinates with iphone mapkit

I'm making an app using apple's mapkit framework. What I want to do is to get longitude and latitude from a location that you press. I get the coordinates from the users current location using this code:

- (IBAction)longpressToGetLocation:(id)sender {
    CLLocationCoordinate2D location = [[[self.mapView userLocation] location] coordinate];
    NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);
}

How do I get that code to show the pressed location instead of userlocation?

like image 274
Tobbbe Avatar asked Jan 29 '13 10:01

Tobbbe


People also ask

Can you get longitude and latitude with iPhone?

Get GPS Coordinates in Maps on iPhone and iPadTap the current location button on the top right. When the blue circle for your spot appears on the map, tap it. Swipe up from the bottom to view full details for your location and you'll see the Latitude and Longitude.

What is MapKit in IOS?

Overview. Use MapKit to give your app a sense of place with maps and location information. You can use the MapKit framework to: Embed maps directly into your app's windows and views. Add annotations and overlays to a map to call out points of interest.


2 Answers

First of all, use a UIGestureRecognizer instead an 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)")
}
like image 74
jcesarmobile Avatar answered Oct 11 '22 11:10

jcesarmobile


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
  }
}
}
like image 28
Thomas Paul Avatar answered Oct 11 '22 11:10

Thomas Paul