简体   繁体   中英

Dropping pin current location xCode6 Swift

I am fairly new to xCode6 and am having difficulties getting my app to drop a pin on my current location. All i would like to do is to allow the user to mark a spot with a pin, and then capture the long and lat of that coordinate - preferably in a println().

I have searched around for a bit, but not found anyone explaining this in swift or xcode6. As I have no idea how to translate from whatever old language was used to swift I figured I'd post the question here.

I have the following code, please help me out.


import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate   {

@IBOutlet var myMap : MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.locationManager.requestWhenInUseAuthorization()

    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func satelliteView(){
    myMap.mapType = MKMapType.Satellite
}

@IBAction func normalView(){

    myMap.mapType = MKMapType.Standard

} 
}

To place an annotation at same coordinates of you current position you can use:

let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: self.myMap.userLocation.coordinate.latitude, longitude: self.myMap.userLocation.coordinate.longitude)
self.myMap.addAnnotation(annotation)

This might be help you.

MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = loc.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";

[self.mapView addAnnotation:point];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM