Получить координаты с адреса и отобразить в MKMapView - PullRequest
0 голосов
/ 11 ноября 2018

Требования для этого приложения таковы, что конечный пользователь должен иметь возможность выбирать из множества мест. Я могу выбрать lat/long coordinates на основе выбранного местоположения, используя GeocodeAddressString, однако у меня возникают трудности с отображением координат выбранного местоположения в mapView. Очень ценю любую помощь.

fileprivate func updateFields(){
    if locationArray?.count > 0
    {
        let location = locationArray![selectedLocationIndex!]
        name.text = location.name
        street.text = location.street
        city.text = location.city
        stateProvince.text = location.stateProvince
        zipPostal.text = location.zipPostal
        country.text = location.country
        phone.text = location.phone

        guard let country = country.text else { return }
        guard let street = street.text else { return }
        guard let city = city.text else { return }
        guard let state = stateProvince.text else { return }
        guard let zipPostal = zipPostal.text else { return }

        // Create Address String

        let address = ("\(country), \(street), \(city), \(state), \(zipPostal)")

        // Geocode Address String
        geocoder.geocodeAddressString(address) { (placemarks, error) in
            // Process Response
            self.processResponse(withPlacemarks: placemarks, error: error)
        }
    }
}

private func processResponse(withPlacemarks placemarks: [CLPlacemark]?, error: Error?) {
    // Update View
    if let error = error {
        print("Unable to Forward Geocode Address (\(error))")
        locationLabel.text = "Unable to Find Location for Address"
    } else {
        var location: CLLocation?
        if let placemarks = placemarks, placemarks.count > 0 {
            location = placemarks.first?.location
        }

        if let location = location {
            let coordinate = location.coordinate
            locationLabel.text = "\(coordinate.latitude), \(coordinate.longitude)"
        } else {
            locationLabel.text = "No Matching Location Found"
        }
    }
}

Изображение показывает массив местоположений с информацией о местоположении и долготой / широтой местоположения

Image shows location array with location information and location longitude/latitude

Я пробовал несколько вещей и не смог успешно

...