2016-11-30 17 views
8

Tôi đang tìm cách có thể yêu cầu người dùng ứng dụng cho vị trí hiện tại của anh ấy và một ghim được tự động thả vào vị trí đó. Đây là mã của tôi để lấy vị trí hiện tại, nhưng tôi gặp khó khăn khi hiểu cách tôi có thể thả ghim cho vị trí hiện tại.Swift MKMapView Thả Chú thích Pin đến Vị trí Hiện tại

import UIKit 
import MapKit 
import CoreLocation 


class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 



@IBOutlet weak var map: MKMapView! 

let locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // User's location 

    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    if #available(iOS 8.0, *) { 
     locationManager.requestAlwaysAuthorization() 
    } else { 
     // Fallback on earlier versions 
    } 
    locationManager.startUpdatingLocation() 

    // add gesture recognizer 
    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(MapVC.mapLongPress(_:))) // colon needs to pass through info 
    longPress.minimumPressDuration = 1.5 // in seconds 
    //add gesture recognition 
    map.addGestureRecognizer(longPress) 
} 

// func called when gesture recognizer detects a long press 

func mapLongPress(_ recognizer: UIGestureRecognizer) { 

    print("A long press has been detected.") 

    let touchedAt = recognizer.location(in: self.map) // adds the location on the view it was pressed 
    let touchedAtCoordinate : CLLocationCoordinate2D = map.convert(touchedAt, toCoordinateFrom: self.map) // will get coordinates 

    let newPin = MKPointAnnotation() 
    newPin.coordinate = touchedAtCoordinate 
    map.addAnnotation(newPin) 


} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

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




    //set region on the map 
    self.map.setRegion(region, animated: true) 



} 

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


} 

Trả lời

13

Nếu bạn muốn thêm pin để sử dụng vị trí mà bạn có thể làm điều đó trong didUpdateLocations phương pháp đại biểu như thế này

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    mapView.removeAnnotation(newPin) 

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

    //set region on the map 
    map.setRegion(region, animated: true) 

    newPin.coordinate = location.coordinate 
    map.addAnnotation(newPin) 

} 

Tạo một biến toàn cầu cho pin của bạn

let newPin = MKPointAnnotation() 

Vì vậy, bất cứ khi nào người dùng sẽ chuyển sang vị trí mới mà ghim trước đó sẽ bị xóa và ghim mới sẽ được thêm vào vị trí được cập nhật.

+0

Khi tôi triển khai điều này, nó cho tôi lỗi "Sử dụng số nhận dạng chưa được giải quyết 'mapView'" – Kevin

+0

'mapView' là tên biến' MapView' thay thế bằng biến 'MapView' là' map ', cũng là i đã cập nhật nó trong câu trả lời của tôi – Rajat

+0

Tôi hiểu rồi. Điều này đã sửa lỗi. Làm thế nào tôi có thể kiểm tra điều này? Khi tôi chạy nó với bản đồ nó không nhận ra vị trí hiện tại của ứng dụng. – Kevin

3

Trước tiên, bạn cần phải thêm chú thích trong didUpdateLocations phương pháp và sau đó bất cứ khi nào chú thích được thêm vào sau đó viewForAnnotation được gọi là như vậy đây là mã và tương ứng phương pháp để thêm pin trong sử dụng vị trí hiện tại:

// Thêm chú thích về vị trí bây giờ

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     //Get Current Location 
     let location = locations.last! as CLLocation 
     let userLocation:CLLocation = locations[0] as CLLocation 
     let myAnnotation: MKPointAnnotation = MKPointAnnotation() 
     myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude) 
     myAnnotation.title = "Current location" 
     map.addAnnotation(myAnnotation) 
} 

// Thêm hình ảnh để người dùng hiện hành pin vị trí

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

    guard !(annotation is MKUserLocation) else { 
      let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation") 
      annotationView.image = UIImage(named:"anyimage.png") 
      return annotationView 
     } 
return nil 
} 

Vui lòng hỏi xem có bất kỳ sự cố nào khác không.

Các vấn đề liên quan