Dropping pin current location xCode6 Swift

Discussion in 'iOS Programming' started by Saucepan, Mar 4, 2015.

  1. Saucepan, Mar 4, 2015
    Last edited by a moderator: Mar 4, 2015

    Saucepan macrumors newbie

    Joined:
    Mar 4, 2015
    #1
    I am fairly new to xCode6 and am having difficulties getting my app to drop a pin on my current location. What I would like to do is to allow the user to mark his/her current location with a pin by pressing the button "drop 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.

    Code:
    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
    
    }
    @IBAction func dropPin(){
    
    
    }  
    }
     
  2. kage207 macrumors 6502a

    Joined:
    Jul 23, 2008
    #2
    Here's what you need for the pin:

    Code:
    var mapAnnotation = MKPointAnnotation()
    mapAnnotation.coordinate = locationManager
    mapAnnotation.title = "If you want a title"
    mapAnnotation.subtitle = "or subtitle"
    myMap.addAnnotation(mapAnnotation)
     
  3. Saucepan, Mar 4, 2015
    Last edited by a moderator: Mar 5, 2015

    Saucepan thread starter macrumors newbie

    Joined:
    Mar 4, 2015
    #3

    I put that into my dropPin() function but it is returning an error, complaining that the "locationManager" on row 2 is not convertible to CLLocationCoordinate2D. Here is my code now:

    Code:
    @IBAction func dropPin(){
            var mapAnnotation = MKPointAnnotation()
            mapAnnotation.coordinate = locationManager
            mapAnnotation.title = "If you want a title"
            mapAnnotation.subtitle = "or subtitle"
            myMap.addAnnotation(mapAnnotation)
            
        }
     
  4. kage207 macrumors 6502a

    Joined:
    Jul 23, 2008
    #4
    You need to look into CLLocationManager and look that the location property. Then you need to convert that from CLLocation to CLLocationCoordinate2D. You really should look at the Apple documentation.

    https://developer.apple.com/library...pple_ref/occ/instp/CLLocationManager/delegate

    On the last note, I didn't think that code would work exactly cause I copied and pasted a code snippet of mine and changed things slighty. This forum is more to help with errors, point you to resources to help you and to help understand things. Not to code for you.
     
  5. Saucepan thread starter macrumors newbie

    Joined:
    Mar 4, 2015
    #5
    Thank you for the info on the developers documents. I will look into it.

    I am also sorry if I offended you by asking for code info. I am very new to coding and was just looking for help with a problem I was having. I never expected to get free coding here, bur was rather viewing this forum as a point to get info on how to cross a smaller hurdle. I do not think I was out of line for asking, but apparently I was. I can only hope you can accept my deepest apologies.
     
  6. kage207 macrumors 6502a

    Joined:
    Jul 23, 2008
    #6
    No worries.
     
  7. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    Novi, MI
    #7
    You really just need to change this line

    Code:
    mapAnnotation.coordinate = locationManager
    to

    Code:
    mapAnnotation.coordinate = locationManager.location.coordinate
    You don't want to assign the locationManager to the coordinate but get the location of the location manager, and assign that to the coordinate.

    I think this should work, but I'm just going based on everything else that was posted being correct
     

Share This Page