2016-05-13 5 views
2

アドレス文字列から座標を取得してマップビューにマーカーを配置しようとしていますが、ジオコーダーから正しい座標を取得できません。私が見ている問題はいくつかあります。ジオコーディングアドレスが正しい座標を返さない

1つは、地図上の注釈がどこにあるのかわからないため、ジオコーダーから正しい座標を取得できません。彼らはすべてかなり接近しているべきですが、そうではありません。

2つのアノテーションが必要ですが、2つしか表示されません。私は何か間違っているのですか?

私の完全な方法。私のデータベースは現在、緯度と経度を持っていますが、私はこれを得ることができればそこにそれを持つことを避けようとしています。

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

マイprint文の結果:gymLocationsに各アイテムの座標を印刷する

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) 

を追加しました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) 
+0

私の推測では、 'geocodeAddressString()'はasyncです。例えば、あなたがログインを入れると、例えば 'gymLocations'に注釈を追加したときにロギングし、' showAnnations() 'のときにログを表示します。 – Larme

+0

@Larme私の質問への更新を参照してください。 – raginggoat

+0

質問は次のとおりです:どのような順序でログが表示されますか?私はあなたの問題はまだ非同期だと思っています。 – Larme

答えて

0

forループ内のすべてのコードをジオコーダーコンプリートハンドラーに移動することで問題を解決しました

関連する問題