It is very frustrating to find the solution for this problem. I posted the whole code to understand the problem. Please help
找到解决这个问题的办法是很令人沮丧的。我发布了整个代码来理解这个问题。请帮助
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)
}
}
I got stuck in this code. Becasuse didChangeDragState Delegate never called. Please help me to find a better solution for this. I am new to swift; So its hard to convert the objective c code in swift
我被这个密码困住了。Becasuse didChangeDragState委托从未调用。请帮助我找到更好的解决办法。我是斯威夫特新来的;因此,快速地转换objective c代码是很困难的。
3 个解决方案
#1
2
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.
拖动状态通常在响应用户与annotation视图的交互时发生变化。然而,annotation视图本身也负责更改该状态。
replace this method and try once. may be working fine
替换此方法并尝试一次。可能是工作正常
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
}
}
Replace you point annotation method with this one.
用这个替换你的点注释法。
let pa = MKPointAnnotation()
pa.coordinate = placemark.location.coordinate
pa.title = placemark.name //or whatever title you like
self.mapView.addAnnotation(pa)
for more , you can see Code and Another Sample Code
#2
0
if (annotation is MKUserLocation) {
return nil
}
let reuseId12 = "pin12"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId12)
pinView.image = UIImage(named:"pin-1250.png")
pinView.canShowCallout = false
pinView.draggable = true
}
else {
pinView.annotation = annotation
}
return pinView
Try this code.
试试这个代码。
#3
0
pinAnnotationView.canShowCallout = false
This line solves my problem. Popup of showing "current location" was the main cause of this problem.
这条线解决了我的问题。显示“当前位置”的弹出是导致这个问题的主要原因。
and these changes in code make it smooth dragging. Mehul and Vijay Answer help me in finding this solution.
这些代码的变化使它平滑地拖动。Mehul和Vijay的回答帮助我找到这个解决方案。
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
switch (newState) {
case .Ending, .Canceling:
view.dragState = .None
default: break
}
}
#1
2
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.
拖动状态通常在响应用户与annotation视图的交互时发生变化。然而,annotation视图本身也负责更改该状态。
replace this method and try once. may be working fine
替换此方法并尝试一次。可能是工作正常
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
}
}
Replace you point annotation method with this one.
用这个替换你的点注释法。
let pa = MKPointAnnotation()
pa.coordinate = placemark.location.coordinate
pa.title = placemark.name //or whatever title you like
self.mapView.addAnnotation(pa)
for more , you can see Code and Another Sample Code
#2
0
if (annotation is MKUserLocation) {
return nil
}
let reuseId12 = "pin12"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId12)
pinView.image = UIImage(named:"pin-1250.png")
pinView.canShowCallout = false
pinView.draggable = true
}
else {
pinView.annotation = annotation
}
return pinView
Try this code.
试试这个代码。
#3
0
pinAnnotationView.canShowCallout = false
This line solves my problem. Popup of showing "current location" was the main cause of this problem.
这条线解决了我的问题。显示“当前位置”的弹出是导致这个问题的主要原因。
and these changes in code make it smooth dragging. Mehul and Vijay Answer help me in finding this solution.
这些代码的变化使它平滑地拖动。Mehul和Vijay的回答帮助我找到这个解决方案。
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
switch (newState) {
case .Ending, .Canceling:
view.dragState = .None
default: break
}
}