地理编码地址不返回正确的坐标

时间:2016-05-13 13:05:08

标签: ios swift mkmapview clgeocoder

我正在尝试从地址字符串中获取坐标并在地图视图上放置标记,但我没有从地理编码器获取正确的坐标。我看到了一些问题。

其一,地图上的注释不是它们应该的位置,因此没有从我的地理编码器获得正确的坐标。它们应该都相当接近,但事实并非如此。

二,应该有三个注释,但只有两个注释出现。我做错了吗?

我的完整方法。我的数据库目前有纬度和经度,但我试图避免在那里有这个,如果我可以使这个工作。

func checkDistanceAndAddPins() {
        for gym in gyms {
            var index = 0

            let gymLatitude = gym["latitude"]!!.doubleValue
            let gymLongitude = gym["longitude"]!!.doubleValue
            let gymLocation = CLLocation(latitude: gymLatitude, longitude: gymLongitude)
            let distance = gymLocation.distanceFromLocation(myLocation!)
            let distanceInMeters = NSNumber(double: distance)
            let metersDouble = distanceInMeters.doubleValue
            let miles = metersDouble * 0.00062137

            if miles > maxDistance {
                gyms.removeAtIndex(index)
            } else {
                // let location = CLLocationCoordinate2D(latitude: gymLatitude, longitude: gymLongitude)
                gymAnnotation = GymAnnotation()
                gymAnnotation!.title = gym["name"] as? String
                gymAnnotation!.subtitle = gym["address"] as? String

                let addressString = gym["address"] as? String
                print("Address: \(addressString)")

                let geocoder = CLGeocoder()
                geocoder.geocodeAddressString(addressString!, completionHandler: {
                    (placemarks, error) -> Void in
                    if error != nil {
                        print("Error", error)
                    }
                    if let placemark = placemarks?.first {
                        print(placemark.location!.coordinate)
                        self.gymAnnotation!.coordinate = placemark.location!.coordinate
                    }
                })

                // gymAnnotation!.coordinate = location
                gymAnnotation!.gymPhoneNumber = gym["phone"] as? String
                gymAnnotation!.gymID = gym["id"] as? String
                if let website = gym["website"] as? String {
                    gymAnnotation!.gymWebsite = website
                }
                gymLocations.append(gymAnnotation!)
            }

            index += 1
        }

        if self.gymLocations.count == 0 {
            let messageString = String(format: "There are no gyms within %.0f miles of your current location. Try changing the search radius.", maxDistance)
            let noGymsAlert = UIAlertController(title: "", message: messageString, preferredStyle: .Alert)

            let okAction = UIAlertAction(title: "OK", style: .Default, handler: {
                (action) -> Void in
                noGymsAlert.dismissViewControllerAnimated(true, completion: nil)
            })

            let radiusAction = UIAlertAction(title: "Change Radius", style: .Default, handler: {
                (action) -> Void in
                noGymsAlert.dismissViewControllerAnimated(true, completion: nil)
                self.changeSearchDistance()
            })

            noGymsAlert.addAction(radiusAction)
            noGymsAlert.addAction(okAction)
            noGymsAlert.view.tintColor = BarItems.greenTintColor

            self.presentViewController(noGymsAlert, animated: true, completion: nil)
        }

        for item in gymLocations {
            print("gymLocations Coordinates: \(item.coordinate)")
        }

        dispatch_async(dispatch_get_main_queue()) {
            self.gymMap.addAnnotations(self.gymLocations)
            self.gymMap.showAnnotations(self.gymMap.annotations, animated: true)
            for item in self.gymMap.annotations {
                print("annotations: \(item.coordinate)")
            }
        }
    }

我的打印声明结果:

Address: Optional("1753 Bardstown Rd, Louisville, KY 40205")
Address: Optional("8609 Westport Rd, Louisville, KY 40205")
Address: Optional("Mid City Mall, 1250 Bardstown Rd, Louisville, KY 40204")
CLLocationCoordinate2D(latitude: 38.235615099999997, longitude: -85.716183599999994)
CLLocationCoordinate2D(latitude: 38.22886264358975, longitude: -85.70138458380427)
CLLocationCoordinate2D(latitude: 38.281753100000003, longitude: -85.593839399999993)

在gymLocations中为每个项目的打印坐标添加了print语句:

gymLocations Coordinates: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
gymLocations Coordinates: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
gymLocations Coordinates: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)

并在showAnnotations()

之后添加了print语句
annotations: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
annotations: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
annotations: CLLocationCoordinate2D(latitude: 38.21228, longitude: -85.679669000000004)
annotations: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)

1 个答案:

答案 0 :(得分:0)

我通过将for循环中的所有代码移动到地理编码器完成处理程序

来解决问题