2015-07-11 30 views
-1

我是新開發的應用程序,我一直在嘗試創建一個應用程序,它將使用核心位置管理器來查找用戶位置,然後使用mapkit進行顯示。以下是我提出的代碼。我一直無法解決它,有人知道我在做什麼錯了嗎?謝謝。如何使用Mapkit訪問用戶位置(Swift)

// 
// ViewController.swift 
// MapKit Test 
// 
// Created by TMT on 7/11/15. 
// Copyright (c) 2015 TMT Inc. All rights reserved. 
// 

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 

@IBOutlet weak var mapView: MKMapView! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    mapView.showsUserLocation = true 
    mapView.showsPointsOfInterest = true 
    mapView.delegate = self 

    let locationManager = CLLocationManager() 

    // Ask for Authorisation from the User. 
    locationManager.requestAlwaysAuthorization() 

    // For use in foreground 
    locationManager.requestWhenInUseAuthorization() 

    if CLLocationManager.locationServicesEnabled() { 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
    locationManager.startUpdatingLocation() 
    } 

    @NSCopying var location: CLLocation! { get } 


    var span = MKCoordinateSpanMake(0.2 
    , 0.2) 

    var region = MKCoordinateRegion(center: location, span: span) 

    mapView.setRegion(region, animated: true) 


    var request = MKLocalSearchRequest() 
    request.naturalLanguageQuery = "library" 


      // Do any additional setup after loading the view, typically from a nib. 
} 

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


} 
+0

看看這個樣本的https:// github上。 com/talhaqamar/Map-demo-swift –

+0

謝謝Johnny,這對我幫助很大。 – TMT

回答

0

您沒有處理location/map的委託方法。您的代碼正在設置地圖和位置框架以及委託方法。你已經初始化了它們,並告訴編譯器你想用mapkit/corelocation做些什麼。所以,現在你需要處理你已經包含了委託方法MKMapViewDelegate, CLLocationManagerDelegate,並委託除處理設置爲self

的地方,你的代碼某處

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

    //Get map location 
    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.mapView.setRegion(region, animated: true) 

    //Get co ordinates 
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in 

     if (error != nil) { 
      println("Error: " + error.localizedDescription) 
      return 
     } 

     if placemarks.count > 0 { 
      let pm = placemarks[0] as! CLPlacemark 
      self.displayLocationInfo(pm) 
     } else { 
      println("Error with the data.") 
     } 
    }) 
} 
func displayLocationInfo(placemark: CLPlacemark) { 

//Remove observer 
self.locationManager.stopUpdatingLocation() 

println(placemark.locality) 
println(placemark.postalCode) 
println(placemark.administrativeArea) 
println(placemark.country) 

} 

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) { 
    println("Error: " + error.localizedDescription) 
} 
+0

我需要把這個放在哪裏?我嘗試將其粘貼到我的代碼中,並且只是遇到了一堆錯誤。 – TMT

相關問題