简体   繁体   中英

Current location title? Mapkit

I'm making an application with MapKit. Here's an image of how it looks: http://i.stack.imgur.com/Dt7Qg.png

And I want to change (Current location) title of the symbol current location from that pin.

由于声誉而在评论中链接

Here's code:

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)
}




}

If I get it right. You want to change blue dot. Try this.

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

//swift 3

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

I think, http://swift3devlopment.blogspot.in/ here you get more details of MapKit

For Show blue circle mapkit in swift 2.0:

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM