Convert coordinates to City name?

后端 未结 9 1154
Happy的楠姐
Happy的楠姐 2020-12-02 07:41

How to get an address from coordinates using MapKit?

I have this code when long press on the map it gets the coordinates:

func didLongPressMap(sender         


        
9条回答
  •  情深已故
    2020-12-02 08:14

    Thanks to @Kampi for this. This is an updated Swift 2.0 (Xcode 7) Version:

    func setUsersClosestCity()
    {
        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: _point1.coordinate.latitude, longitude: _point1.coordinate.longitude)
        geoCoder.reverseGeocodeLocation(location)
        {
            (placemarks, error) -> Void in
    
            let placeArray = placemarks as [CLPlacemark]!
    
            // Place details
            var placeMark: CLPlacemark!
            placeMark = placeArray?[0]
    
            // Address dictionary
            print(placeMark.addressDictionary)
    
            // Location name
            if let locationName = placeMark.addressDictionary?["Name"] as? NSString
            {
                print(locationName)
            }
    
            // Street address
            if let street = placeMark.addressDictionary?["Thoroughfare"] as? NSString
            {
                print(street)
            }
    
            // City
            if let city = placeMark.addressDictionary?["City"] as? NSString
            {
                print(city)
            }
    
            // Zip code
            if let zip = placeMark.addressDictionary?["ZIP"] as? NSString
            {
                print(zip)
            }
    
            // Country
            if let country = placeMark.addressDictionary?["Country"] as? NSString
            {
                print(country)
            }
        }
    }
    

提交回复
热议问题