我有以下函数来确定文本字段(searchAddress
)中包含的地址的纬度/经度,以创建以此位置为中心的地图区域,然后首先为此位置添加注释,其次对于存储在Parse后端中的商店位置。
首次打开map view
时,预设searchAddress
文本字段,其中包含用户默认地址以及viewDidLoad
对该功能的初始调用。它按照我的意图工作。但是,用户可以在此字段中键入不同的地址,然后单击“搜索”按钮,然后第二次调用相同的功能。它偶尔会起作用,但通常不会显示注释。
如果我再次单击“搜索”以使区域不会更改,则会正确显示注释。我认为这是一个计时问题,并且与我在更改区域时重绘地图所需的时间有关,因为有时注释会在第一次出现,但大多数时候却没有。如果我重复搜索而不更改地址,它可以正常工作。在此先感谢,我一直坚持这一点,无法找到解决方案。这是代码:
func addMapAnotations () {
// var searchLocation = preferredDeliveryLocation.text
var geocoder = CLGeocoder()
var address = CLLocationCoordinate2D()
geocoder.geocodeAddressString(searchAddress.text, completionHandler: {
(placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
var latDelta: CLLocationDegrees = 0.1
var lonDelta: CLLocationDegrees = 0.1
var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
address = placemark.location.coordinate
println(placemark.location.coordinate.latitude)
println("Inside geocoder lititude: \(address.latitude)")
println("Inside geocoder longitude: \(address.longitude)")
var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(address.latitude, address.longitude)
var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
self.map.setRegion(region, animated: true)
var tempLocationObject = self.currentUser
var searchAnnotation = NewCustomPointAnnotation(coordinate: address, title: "", subtitle: "", locationObject: tempLocationObject!, locationCategory: "")
self.annotationCategory = "homeLocation"
searchAnnotation.locationCategory = "homeLocation"
searchAnnotation.coordinate = address
searchAnnotation.title = "Search address"
searchAnnotation.subtitle = "Search for delivery locations near by..."
// self.map.addAnnotation(MKPlacemark(placemark: placemark))
self.map.addAnnotation(searchAnnotation)
}
})
self.queryCurrent.whereKey("country", equalTo: "Australia")
// self.queryCurrent.whereKey("State", equalTo: state)
self.queryCurrent.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
var latitude = object["latitude"] as! CLLocationDegrees
var longitude = object["longitude"] as! CLLocationDegrees
var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
var deliveryAnnotation = NewCustomPointAnnotation(coordinate: location, title: "", subtitle: "", locationObject: object, locationCategory: "")
self.annotationCategory = "deliveryLocation"
deliveryAnnotation.locationCategory = "deliveryLocation"
deliveryAnnotation.coordinate = location
deliveryAnnotation.title = object["name"] as! String
deliveryAnnotation.subtitle = "Click to select this location"
deliveryAnnotation.locationObject = object
println("Annotation 1: \(deliveryAnnotation.description)")
println("Object ID: \(deliveryAnnotation.locationObject.objectId)")
self.map.addAnnotation(deliveryAnnotation)
}
}
} else {
println("No locations found")
}
}
}