使用扩展将用户位置存储在可用变量中
Using extension to store user location in a useable variable
我有一个使用 LocationManager
创建的扩展,用于将用户位置存储在一个变量中,以便在 Swiftui Map()
和我的扩展中使用。我注意到当我初始化你的变量时,扩展需要 lat 和 long 才能在我只需要 userLat 的地方使用。
latitude: userLocation.init(userLat: <#Double#>, userLong: <#Double#>).self.userLat,
struct ContentView_v2: View {
// MARK: Get current location
class userLocation {
let locationManager = LocationManager()
var userLat: Double
var userLong: Double
init(userLat: Double, userLong: Double){
self.userLat = locationManager.lastLocation?.coordinate.latitude ?? 0.0
self.userLong = locationManager.lastLocation?.coordinate.longitude ?? 0.0
}
}
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(
latitude: userLocation.init(userLat: <#Double#>, userLong: <#Double#>).self.userLat,
longitude: userLocation.init(userLat: <#Double#>, userLong: <#Double#>).self.userLong
),
span: MKCoordinateSpan(
latitudeDelta: 0.01,
longitudeDelta: 0.01
)
)
var body: some View {
Map(
coordinateRegion: $region,
interactionModes: MapInteractionModes.all,
showsUserLocation: true,
userTrackingMode: $userTrackingMode,
annotationItems: yelpbusinessmapdata?.businesses ?? []
)
{ business in
MapAnnotation(
coordinate:
business.coordinates.coordinate2D,
anchorPoint: CGPoint(x: 0.5, y: 0.5)
){
VStack {
Image(systemName: "mappin")
.font(.system(size: 18, weight: .medium))
.foregroundColor(.red)
Text(business.name)
}
}
}
如果我正确理解了您的代码,那么您可能想要以下...
class userLocation {
let locationManager = LocationManager()
var userLat: Double
var userLong: Double
init(){
self.userLat = locationManager.lastLocation?.coordinate.latitude ?? 0.0
self.userLong = locationManager.lastLocation?.coordinate.longitude ?? 0.0
}
}
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(
latitude: userLocation().userLat,
longitude: userLocation().userLong
),
我有一个使用 LocationManager
创建的扩展,用于将用户位置存储在一个变量中,以便在 Swiftui Map()
和我的扩展中使用。我注意到当我初始化你的变量时,扩展需要 lat 和 long 才能在我只需要 userLat 的地方使用。
latitude: userLocation.init(userLat: <#Double#>, userLong: <#Double#>).self.userLat,
struct ContentView_v2: View {
// MARK: Get current location
class userLocation {
let locationManager = LocationManager()
var userLat: Double
var userLong: Double
init(userLat: Double, userLong: Double){
self.userLat = locationManager.lastLocation?.coordinate.latitude ?? 0.0
self.userLong = locationManager.lastLocation?.coordinate.longitude ?? 0.0
}
}
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(
latitude: userLocation.init(userLat: <#Double#>, userLong: <#Double#>).self.userLat,
longitude: userLocation.init(userLat: <#Double#>, userLong: <#Double#>).self.userLong
),
span: MKCoordinateSpan(
latitudeDelta: 0.01,
longitudeDelta: 0.01
)
)
var body: some View {
Map(
coordinateRegion: $region,
interactionModes: MapInteractionModes.all,
showsUserLocation: true,
userTrackingMode: $userTrackingMode,
annotationItems: yelpbusinessmapdata?.businesses ?? []
)
{ business in
MapAnnotation(
coordinate:
business.coordinates.coordinate2D,
anchorPoint: CGPoint(x: 0.5, y: 0.5)
){
VStack {
Image(systemName: "mappin")
.font(.system(size: 18, weight: .medium))
.foregroundColor(.red)
Text(business.name)
}
}
}
如果我正确理解了您的代码,那么您可能想要以下...
class userLocation {
let locationManager = LocationManager()
var userLat: Double
var userLong: Double
init(){
self.userLat = locationManager.lastLocation?.coordinate.latitude ?? 0.0
self.userLong = locationManager.lastLocation?.coordinate.longitude ?? 0.0
}
}
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(
latitude: userLocation().userLat,
longitude: userLocation().userLong
),