如何添加导致地图上的图钉的GPS?

时间:2015-08-27 13:32:17

标签: ios swift gps

我正在创建一个应用程序,在地图上显示引脚(用户添加)并将其保存在桌面视图中,我希望当用户点击打开GPS的按钮时,GPS会引导用户,这样他们就可以到达那个地方,我怎么能这样做,这是我在表格视图中的代码:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

        cell.textLabel?.text = places[indexPath.row]["name"]

        return cell
    }

    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

        activePlace = indexPath.row

        return indexPath

    }

这是我在地图视图中的代码:

func action(gestureRecognizer:UIGestureRecognizer) {

        if gestureRecognizer.state == UIGestureRecognizerState.Began {

            var touchPoint = gestureRecognizer.locationInView(self.Map)

            var newCoordinate = self.Map.convertPoint(touchPoint, toCoordinateFromView: self.Map)

            var location = CLLocation(latitude: newCoordinate.latitude , longitude: newCoordinate.longitude)



            CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

                var title = ""

                if (error == nil) {



                    if let p = CLPlacemark(placemark: placemarks?[0] as! CLPlacemark) {

                        var subThoroughfare: String = ""
                        var thoroughfare: String = ""

                        if p.subThoroughfare != nil {

                            subThoroughfare = p.subThoroughfare

                        }

                        if p.thoroughfare != nil {

                            thoroughfare = p.thoroughfare
                        }

                        title = "\(subThoroughfare) \(thoroughfare)"

                    }

                }

                if title == "" {

                    title = "added \(NSDate())"
                }

                places.append(["name":title,"lat":"\(newCoordinate.latitude)","lon":"\(newCoordinate.longitude)"])

我一直在尝试下面的代码来添加GPS,但我有一个预定义的目的地,所以我想我必须将坐标更改为其他东西,但我不知道究竟是什么,谢谢你的帮助!

UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/maps?daddr=34.539250,-117.222025")!)

我也在使用这段代码作为用户位置

 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

        var userLocation:CLLocation = locations[0] as! CLLocation

        var latitude = userLocation.coordinate.latitude
        var longitude = userLocation.coordinate.longitude

        var coordinate = CLLocationCoordinate2DMake(latitude, longitude)

        var latDelta:CLLocationDegrees = 0.01
        var lonDelta:CLLocationDegrees = 0.01

        var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

        var region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)

        self.Map.setRegion(region, animated: true)

2 个答案:

答案 0 :(得分:0)

我们假设您已启用位置,因此您只需要"用户位置"您已保存在表格中然后尝试:

只要通过你的lang,lat就应该这样做^^

  func openMapForPlace() {

    var lat1 : NSString = self.venueLat
    var lng1 : NSString = self.venueLng

    var latitute:CLLocationDegrees =  lat1.doubleValue
    var longitute:CLLocationDegrees =  lng1.doubleValue

    let regionDistance:CLLocationDistance = 10000
    var coordinates = CLLocationCoordinate2DMake(latitute, longitute)
    let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
    var options = [
        MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
        MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
    ]
    var placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
    var mapItem = MKMapItem(placemark: placemark)
    mapItem.name = "\(self.venueName)"
    mapItem.openInMapsWithLaunchOptions(options)

}

如果有任何问题可以随意提问

答案 1 :(得分:0)

为了回应@Mingebag的回答,回应你的评论: 您需要在他提供的openMapForPlace()方法中设置变量,这些变量对应于您想要在apple maps应用程序中获取路线的gps位置。

无论您何时调用此方法,都可以将所需的变量传递给它。为了实现这一点,你真的需要以某种方式给它Lat / Lon。 你可以做到这一点:

func openMapsForPlace(lat: Double, lon: Double) {}

或您拥有的任何格式。

如果您有更好的执行地点,您也可以将此代码放在任何地方:

let regionDistance:CLLocationDistance = 10000

//set the coordinates with your variables
var coordinates = CLLocationCoordinate2DMake(newCoordinates.latitude, newCoordinates.longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
var options = [
    MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
    MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
]

//now your placemark will have the lat long you put in above
var placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil) 

var mapItem = MKMapItem(placemark: placemark)
mapItem.name = "\(self.venueName)"

//this line then launches the app for you
mapItem.openInMapsWithLaunchOptions(options)