gpt4 book ai didi

ios - 如何使用 swift 2 和 xcode 7 在 MapKit 中使注释点可拖动?

转载 作者:可可西里 更新时间:2023-11-01 01:03:31 27 4
gpt4 key购买 nike

找到这个问题的解决方案非常令人沮丧。我发布了整个代码以了解问题。请帮忙

import UIKit
import MapKit
import CoreLocation
class LocationViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate {


@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()
var annotationPoint: MKPointAnnotation!
var getMovedMapCenter: CLLocation!
var myPinView:MKPinAnnotationView!
override func viewDidLoad() {
super.viewDidLoad()

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

// Do any additional setup after loading the view.
}

// MARK: - Location Delegate Methods

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last

let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

self.mapView.setRegion(region, animated: true)

if(self.annotationPoint == nil)
{
self.annotationPoint = MKPointAnnotation()
getMovedMapCenter = CLLocation(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

self.annotationPoint.coordinate = getMovedMapCenter.coordinate

// self.annotationPoint.title = location_value
//self.annotationPoint.subtitle = ""

// println_debug(self.annotationPoint)

self.mapView.addAnnotation(self.annotationPoint)

}

self.locationManager.stopUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}

//MARK:- View for Annotation
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
if annotation is MKPointAnnotation {
let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")

pinAnnotationView.pinColor = .Purple
pinAnnotationView.draggable = true
pinAnnotationView.canShowCallout = true
pinAnnotationView.animatesDrop = true

return pinAnnotationView
}

return nil
}

//MARK:- Annotation Changed State

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {

print("hiii")


// println_debug("End State coordinates: \(self.annotationPoint.coordinate.latitude),\(self.annotationPoint.coordinate.longitude)")


if(newState == .Ending)
{


let center = CLLocationCoordinate2D(latitude: self.annotationPoint.coordinate.latitude, longitude: self.annotationPoint.coordinate.longitude)
// self.currentLocationNameA(center)

// dispatch_async(GlobalBackgroundQueue)
// {
//
//
// }
}

}

//MARK:- update Not Get

func mapView(mapView: MKMapView,
didFailToLocateUserWithError error: NSError)
{
// AppHelper.showALertWithTag(121, title: APP_NAME, message: "Failed to get location. Please check Location setting", delegate: nil, cancelButtonTitle: "Ok", otherButtonTitle: nil)

}




}

我被这段代码卡住了。因为 didChangeDragState Delegate 从未调用过。请帮助我为此找到更好的解决方案。我是 swift 的新手;所以很难在swift中转换objective c代码

最佳答案

The drag state typically changes in response to user interactions with the annotation view. However, the annotation view itself is responsible for changing that state as well.

替换此方法并尝试一次。可能工作正常

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
switch (newState) {
case .Starting:
view.dragState = .Dragging
case .Ending, .Canceling:
view.dragState = .None
default: break
}
}

用这个替换你的点注释方法。

let pa = MKPointAnnotation()
pa.coordinate = placemark.location.coordinate
pa.title = placemark.name //or whatever title you like
self.mapView.addAnnotation(pa)

更多可以看CodeAnother Sample Code

关于ios - 如何使用 swift 2 和 xcode 7 在 MapKit 中使注释点可拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33210733/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com