Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Saucepan

macrumors newbie
Original poster
Mar 4, 2015
3
0
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(){


}  
}
 
Last edited by a moderator:

kage207

macrumors 6502a
Jul 23, 2008
971
56
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)
 

Saucepan

macrumors newbie
Original poster
Mar 4, 2015
3
0
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)


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)
        
    }
 
Last edited by a moderator:

kage207

macrumors 6502a
Jul 23, 2008
971
56
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)
        
    }

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.
 

Saucepan

macrumors newbie
Original poster
Mar 4, 2015
3
0
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.

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.
 

kage207

macrumors 6502a
Jul 23, 2008
971
56
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.

No worries.
 

waterskier2007

macrumors 68000
Jun 19, 2007
1,871
228
Novi, MI
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)
        
    }

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.