当前位置标题? Mapkit

时间:2015-05-12 13:36:08

标签: ios xcode swift mapkit

我正在使用MapKit制作应用程序。这是一个看起来如何的图像: http://i.stack.imgur.com/Dt7Qg.png

我想从该引脚更改符号当前位置的(当前位置)标题。

LINK in comment because of reputation

这是代码:

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate,    MKMapViewDelegate {

@IBOutlet weak var theMap: MKMapView!
let locationManager = CLLocationManager()

override func viewDidLoad()
{
    super.viewDidLoad()


    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    let location = self.locationManager.location

    var latitude: Double = location.coordinate.latitude
    var longitude: Double = location.coordinate.longitude

    println("GPS Súradnice :: \(latitude), \(longitude)")

    theMap.delegate = self
    theMap.mapType = MKMapType.Standard
    theMap.showsUserLocation = true


}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()

}



//--- Find Address of Current Location ---//

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
    //--- CLGeocode to get address of current location ---//
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in



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

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

}


func displayLocationInfo(placemark: CLPlacemark?)
{
    if let Placemark = placemark
    {
        //Stop updating kvôli vydrži baterke
        locationManager.stopUpdatingLocation()
        let adresa = (Placemark.thoroughfare != nil) ? Placemark.thoroughfare : "Ulica: "
        let cislo = (Placemark.subThoroughfare != nil) ? Placemark.subThoroughfare : "Číslo ulice:"
        let mesto = (Placemark.locality != nil) ? Placemark.locality : "Mesto: "
        let stat = (Placemark.country != nil) ? Placemark.country : "Štát: "


        var coordinates:CLLocationCoordinate2D = placemark!.location.coordinate

        var pointAnnotation:MKPointAnnotation = MKPointAnnotation()
        pointAnnotation.coordinate = coordinates
        pointAnnotation.title = "\(adresa) \(cislo)"
        pointAnnotation.subtitle = "\(adresa) \(cislo), \(mesto), \(stat)"


        self.theMap.addAnnotation(pointAnnotation)
        self.theMap.centerCoordinate = coordinates
        self.theMap.selectAnnotation(pointAnnotation, animated: true)

        println(mesto)
        println(adresa)
        println(cislo)
        println(stat)

    }

}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
    println("Chyba pri aktualizovaní lokácie " + error.localizedDescription)
}




}

3 个答案:

答案 0 :(得分:5)

如果我做对了。你想改变蓝点。试试这个。

let theLocation: MKUserLocation = theMap.userLocation
theLocation.title = "I'm here!"

答案 1 :(得分:1)

// swift 3

let annotation = MKPointAnnotation()
annotation.coordinate = center
annotation.title = "title"
Annotation.subtitle = “Subtitle”
mapView.addAnnotation(annotation)

我认为,http://swift3devlopment.blogspot.in/在这里您可以获得有关MapKit的更多详细信息

答案 2 :(得分:-1)

对于swift 2.0中的Show blue circle mapkit:

override func viewDidLoad() {
    super.viewDidLoad()


    //this line show blue circle location user
    Mapa.showsUserLocation = true

    locationManager.requestWhenInUseAuthorization();
    if CLLocationManager.locationServicesEnabled(){
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
    else{
        print("Location service disabled");
    }

}



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate

    let region = MKCoordinateRegion(center: locValue, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
    print("locations = \(locValue.latitude) \(locValue.longitude)")
    Mapa.setRegion(region, animated: true)
}