gpt4 book ai didi

iOS MapView无法配合GPS服务

转载 作者:行者123 更新时间:2023-11-30 14:15:38 25 4
gpt4 key购买 nike

我遇到了一个问题,我想通过 GPS 获取用户的位置。不过,我无法配合 map View ,只有 GPS 是好的。我想知道该位置并将其作为 map View 上的注释。以下是 Swift 中的代码。

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{

@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}




@IBOutlet weak var LocateBtn: UIButton!

@IBAction func Locate(sender: UIButton) {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()


}


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in

println(2)
if (error != nil) {
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}

if placemarks.count > 0 {
println(3)
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
} else {
println("Problem with the data received from geocoder")
}
})
}


func displayLocationInfo(placemark: CLPlacemark?) {
if let containsPlacemark = placemark {
//stop updating location to save battery life
locationManager.stopUpdatingLocation()
let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""
let locations = Locations()
var annotations = [MKPointAnnotation]()


for dictionary in locations {


let coordinate = containsPlacemark.location.coordinate
let first = dictionary["firstName"] as! String
let last = dictionary["lastName"] as! String
let mediaURL = dictionary["mediaURL"] as! String

var annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "\(first) \(last)"
annotation.subtitle = mediaURL

annotations.append(annotation)
}
println(annotations)
self.mapView.addAnnotations(annotations)
}

}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let reusedId = "pin"

var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reusedId) as? MKPinAnnotationView

if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reusedId)
pinView!.canShowCallout = true
pinView!.pinColor = .Red
pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIButton

} else {
pinView!.annotation = annotation
}
return pinView
}


func mapView(mapView: MKMapView!, annotationView: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {

if control == annotationView.rightCalloutAccessoryView {
let app = UIApplication.sharedApplication()
app.openURL(NSURL(string: annotationView.annotation.subtitle!)!)
}
}

func Locations() -> [[String : AnyObject]] {
return [
[
"createdAt" : "2015-7-3T21:50:00",
"firstName" : "Joe",
"lastName" : "Li",
"latitude" : 28.1461248,
"longitude" : -82.75676799999999,
"mapString" : "Tarpon Springs, FL",
"mediaURL" : "www.linkedin.com/in/jessicauelmen/en",
"objectId" : "kj18GEaWD8",
"uniqueKey" : 872458750,
"updatedAt" : "2015-7-4T21:00:00"
]
]
}

}

最佳答案

查看本教程 Here :

试试这个:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as CLLocation

let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

self.map.setRegion(region, animated: true)
}

关于iOS MapView无法配合GPS服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31226861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com