如何在mapVIew中添加乘法注释

时间:2017-09-28 06:30:12

标签: ios swift firebase mapkit mkmapview

我有 mapView ,并且来自 firebase 的地址。现在我只能在 mapView 中添加一个注释,但在 print(“地址(self.allAddresses)”)中我有三个地址:

地址Москва,Пресненскаянабережнаяд.8,квартира195,подъезд94

地址Москва,ул。 Правдыд.24строение3

地址Москва,ул.Электрозаводскаяд.21

我如何添加三个或更多注释?

我的火力基地:

class AddressesMapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

    var allAddresses: String = ""

    var count = 0

    let geocoder = CLGeocoder()

    var addressRef: DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()

        addressRef = Database.database().reference(withPath: "Address")

        loadAddressFromFirebase()

    }
    func loadAddressFromFirebase() {

        addressRef.observe(.value, with: { (snapshot) in

            for address in snapshot.children.allObjects as! [DataSnapshot] {

                let value = address.value as! NSDictionary

                self.allAddresses = value["address"] as? String ?? ""

                print("Address \(self.allAddresses)")

                if self.count < self.allAddresses.count {

                    let addresses = self.allAddresses

                    self.geocoder.geocodeAddressString(addresses, completionHandler: { (placemarks, error) in

                        print("Address - \(addresses)")

                        guard error == nil else { return }

                        guard let placemarks = placemarks else { return }

                        if let placemark = placemarks.first {

                            self.mapView.addAnnotation(MKPlacemark(placemark: placemark))

                        }

                        self.count += 1
                        print("Count - \(self.count)")
                    })
                }   
            }  
        }) 
    }
}

我的代码:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "26.0.2"

    defaultConfig {
        applicationId "net.mobilegts.redemption.evolution"
        minSdkVersion 14
        targetSdkVersion 23

        ndk {
            moduleName "player_shared"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.google.android.gms:play-services:+'
    compile files('libs/dagger-1.2.2.jar')
    compile files('libs/javax.inject-1.jar')
    compile files('libs/nineoldandroids-2.4.0.jar')
    compile files('libs/support-v4-19.0.1.jar')
}

1 个答案:

答案 0 :(得分:1)

  • 尝试使用&#39;来分配geoCoder。循环。
  • 调用geocoder.cancelGeocode()方法。

            let geocoder = CLGeocoder()
            geocoder.geocodeAddressString(addresses, completionHandler: { (placemarks, error) in
    
                print("Address - \(addresses)")
    
                guard error == nil else { return }
    
                guard let placemarks = placemarks else { return }
    
                if let placemark = placemarks.first {
    
                    self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
    
                }
    
                self.count += 1
                print("Count - \(self.count)")
                geocoder.cancelGeocode()
            })