【问题标题】:How to show my location and pin in map correctly on Swift?如何在 Swift 上正确显示我的位置并在地图上固定?
【发布时间】:2014-07-16 12:03:19
【问题描述】:

我正在尝试在应用程序 Swift 中显示我的位置,但这没有显示任何内容,地图全是蓝色的..

我的代码是这个形式的互联网教程:

 var latitude:CLLocationDegrees = location.location.coordinate.latitude
    var longitude:CLLocationDegrees = location.location.coordinate.longitude
    var homeLati: CLLocationDegrees = 40.01540192
    var homeLong: CLLocationDegrees = 20.87901079
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01
    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)

    var myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    self.mapKit.setRegion(region, animated: true)

    self.mapKit.showsUserLocation = true
    ///Red Pin
    var myHomePin = MKPointAnnotation()
    myHomePin.coordinate = myHome
    myHomePin.title = "Home"
    myHomePin.subtitle = "Bogdan's home"
    self.mapKit.addAnnotation(myHomePin)

我已经在.split.中导入了相应的配置。这段代码有什么不好的地方?

谢谢!

【问题讨论】:

  • 我正在使用该问题中的代码。该帖子中的问题是添加引脚注释,我的问题是在地图中显示位置,我无法在地图中显示任何内容。地图清晰
  • 我忘记了 plist 中的 NSLocationAlwaysUsageDescription,因此我无法在 mapView 中显示。谢谢!这在另一个中说,你是对的!

标签: swift mapkit cllocation


【解决方案1】:

使用纬度和经度在地图上显示位置。

 var latDelta:CLLocationDegrees = 0.01

 var longDelta:CLLocationDegrees = 0.01

 var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
 var pointLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(your latitude, your longitude)

 var region:MKCoordinateRegion = MKCoordinateRegionMake(pointLocation, theSpan)
  mapView.setRegion(region, animated: true)

  var pinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(your latitude, your longitude)
  var objectAnnotation = MKPointAnnotation()
  objectAnnotation.coordinate = pinLocation
  objectAnnotation.title = your title
  self.mapView.addAnnotation(objectAnnotation)

【讨论】:

    【解决方案2】:

    你可以这样做:

    import UIKit
    
    import MapKit
    
    class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    
    let locationManager = CLLocationManager()  // Here it is an object to get the user's location.
    
    
    
    @IBOutlet weak var mapView: MKMapView!    // It is my map.
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestLocation()
        // To initialize locationManager (). Now it can give you the user's location.        
    
    
        map.delegate = self
        map.showsUserLocation = true
        // To initialize the map.
    
    }// Here you should initialize locationManager and your map. I send you the code later.
    
    func dropPinZoomIn(placemark: MKPlacemark){   // This function will "poste" the dialogue bubble of the pin.
        var selectedPin: MKPlacemark?
    
        // cache the pin
        selectedPin = placemark    // MKPlacemark() give the details like location to the dialogue bubble. Place mark is initialize in the function getLocationAddress (location: ) who call this function.
    
        // clear existing pins to work with only one dialogue bubble.
        mapView.removeAnnotations(mapView.annotations)
        let annotation = MKPointAnnotation()    // The dialogue bubble object.
        annotation.coordinate = placemark.coordinate   
        annotation.title = placemark.name// Here you should test to understand where the location appear in the dialogue bubble.
    
        if let city = placemark.locality,
            let state = placemark.administrativeArea {
            annotation.subtitle = String((city))+String((state));
        } // To "post" the user's location in the bubble.
    
        mapView.addAnnotation(annotation)     // To initialize the bubble.
        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegionMake(placemark.coordinate, span)
        mapView.setRegion(region, animated: true)   // To update the map with a center and a size.
    }
    
    func getLocationAddress(location:CLLocation) {    // This function give you the user's address from a location like locationManager.coordinate (it is usually the user's location).
        let geocoder = CLGeocoder()
    
        print("-> Finding user address...")
    
        geocoder.reverseGeocodeLocation(location, completionHandler: {(placemarks, error)->Void in
            var placemark:CLPlacemark!
    
            if error == nil && placemarks!.count > 0 {
                placemark = placemarks![0] as CLPlacemark
    
    
                var addressString : String = ""
                if placemark.ISOcountryCode == "TW" /*Address Format in Chinese*/ {
                    if placemark.country != nil {  // To have the country
                        addressString = placemark.country!
                    }
                    if placemark.subAdministrativeArea != nil {  // To have the subAdministrativeArea.
                        addressString = addressString + placemark.subAdministrativeArea! + ", "
                    }
                    if placemark.postalCode != nil {   // To ...
                        addressString = addressString + placemark.postalCode! + " "
                    }
                    if placemark.locality != nil {
                        addressString = addressString + placemark.locality!
                    }
                    if placemark.thoroughfare != nil {
                        addressString = addressString + placemark.thoroughfare!
                    }
                    if placemark.subThoroughfare != nil {
                        addressString = addressString + placemark.subThoroughfare!
                    }
                } else {
                    if placemark.subThoroughfare != nil {
                        addressString = placemark.subThoroughfare! + " "
                    }
                    if placemark.thoroughfare != nil {
                        addressString = addressString + placemark.thoroughfare! + ", "
                    }
                    if placemark.postalCode != nil {
                        addressString = addressString + placemark.postalCode! + " "
                    }
                    if placemark.locality != nil {
                        addressString = addressString + placemark.locality! + ", "
                    }
                    if placemark.administrativeArea != nil {
                        addressString = addressString + placemark.administrativeArea! + " "
                    }
                    if placemark.country != nil {
                        addressString = addressString + placemark.country!
                    }
    
                    let new_placemark: MKPlacemark = MKPlacemark (placemark: placemark)
    
                    // new_placemark initialize a variable of type MKPlacemark () from geocoder to use the function dropPinZoomIn (placemark:).
    
    
                    self.dropPinZoomIn (new_placemark)
    
                    print (placemark.description)   // You can see the place mark's details like the country.
    
                }
    
    
            }
        })
    
    }
    

    我为您提供了一个简单的定位系统,您可以在其中获得用户的地址、用户的位置,并将其显示在带有显示用户位置的对话气泡的地图中。 您可以在Thorn web site with dialogueapple developer web site. 中获得更多文档

    【讨论】:

    • 您能否解释一下您做了什么以及为什么!?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多