Hello,
I am trying to decode a JSON data stream that contains the latitude and longitude of a given location on Earth…
Here is the geocoding data structure…
// ================================================================================
/// Under "results" there are a number of elements including "place_id", however we are only interested in the latitude and longitude that is found under "geometry" Element
struct resultsData : Decodable
{
var geometry: geometryLocation
}// close struct geometryResults : Decodable
// ================================================================================
/// under the "geometry" Element is the "Location" Element that contains the latitude and longitude
struct geometryLocation :Decodable
{
var location : locationLatLng
}// close struct geometryLocation :Decodable
// ================================================================================
/// Under the "Location" Element is the items that we wish to return, the latitude and longitude for
/// the string address.
struct locationLatLng :Decodable
{
var lat: Double
var lng: Double
}// close struct locationLatLng :Decodable
// ================================================================================
And here is part of my code to pull out the lat [latitude]… but I am getting an error I cannot resolve…
import Foundation
import SwiftUI
import Combine
// ===================================================================================
class AddressDecodeLatLng:ObservableObject
{
//—————————————————————————————————————————————————————————
//—————————————————————————————————————————————————————————
// Properties
@ObservedObject var geocodedaddress = addressGeoDecoding()
/// this is creating a new Observed object instance of the class "addressGeocodeLatLng()"
var gcdedResultsData: geocodeResultsData? = nil
///initialise the variables representing the latitude and longitude
@State var latitud: Double? = 0.00
@State var longitud: Double? = 0.00
//—————————————————————————————————————————————————————————
//—————————————————————————————————————————————————————————
// Methods
//—————————————————————————————————————————————————————————
//
func retrieveGecondingLatLngData(addressUncoded : String )
{
geocodedaddress.fetchGecondingLatLngData(addressUncoded: addressUncoded)
}// close func retrieveGecondingLatLngData(addressUncoded : addressUncoded )
//—————————————————————————————————————————————————————————
/// This method decodes the latitude information for from the JSON data returned from Google address geocoded data
func retrieveLatitude ( ) -> Double
{
let locationResultsArray = geocodedaddress.gcodedResultsData?.results[0]
let locationGeometry = locationResultsArray?.geometry
let locationElement = locationGeometry?.location
if (locationElement != nil)
{
latitud = locationElement?.lat
return latitud
// get an error message Value of optional type 'Double?' must be unwrapped to a value of type 'Double'
}// close if let locationLocation = locationGeometry?.location
else
{
print("Geocoding data for Latitude not available")
}// close if let locationLocation = locationGeometry?.location
}// close retrieveLatitude ( )
I am getting the following error message from the compiler…
" Value of optional type ‘Double?’ must be unwrapped to a value of type ‘Double’"
I thought by wrapping “latitud = locationElement?.lat” in an if … else block I could resolve any issues with returning the value of “latiud”… and it was also defined in my code as “Double”…
Any suggestion how I can resolve this?
Thank you in advance…