Swift 如何在我的应用程序的其他位置使用 locationManager 函数的经度和纬度值
How to use Longitude and Latitude values from locationManager function elsewhere in my app in Swift
我正在使用 CoreLocation
框架在用户打开我的应用程序时获取他们的位置。我使用这个功能:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
获取用户的经纬度位置,我可以通过将它们打印到日志中来查看它们。这很好用。
在我的应用程序的其他地方(但在同一个 viewController.swift
文件中)我有使用 OpenWeatherMap API 的代码,并且我有一个包含 url 的字符串,其中 return JSON。
在我的 viewDidLoad
中,我使用:
getWeatherData("http://api.openweathermap.org/data/2.5/weather?lat=XXXXXX&lon=XXXXXX&appid=(MY-APP-ID)")
我需要将我在 locationManager
函数中获取的 Long 和 Lat 值放入这个字符串中,我知道我可以通过 url 中的 "\()"
来完成] 字符串。
我的问题是,我目前只能在 locationManager
函数中使用这些值。如何将它们存储在此函数之外的值中,以便将它们添加到我的 URL 字符串中?
谢谢
希望这能回答您的问题。
import UIKit
import MapKit
class myClass {
var userLocation: CLLocationCoordinate2D? // The user location as an Optional stored as a var in class "myClass".
// !!! This can be accessed everywhere within the class "myClass" (and deeper)
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate // Change it from var to let since it's only read not writen
// !!! This can be accessed everywhere within the func "locationManager" (and deeper)
userLocation = locValue // !!! Store it if necessary
// Why would you call this in viewDidLoad? I doubt the user location will be available at this point (but it might). You can move this anywhere if you want
// note the "\(name)" this will turn the var name into a string
// if != nil not necessary here since it cannot be nil but still added it regardless
// Maybe you want to add a check so this only gets called on the first location update. It depends on what you need it for.
if userLocation != nil {
getWeatherData("http://api.openweathermap.org/data/2.5/weather?lat=\(userLocation!.latitude)&lon=\(userLocation!.latitude)&appid=(MY-APP-ID)") // Why would you call this in viewDidLoad? I doubt user doubt the user location will be available at this point (but it might)
}
else {
print("Error: User not Located (yet)")
}
}
}
我正在使用 CoreLocation
框架在用户打开我的应用程序时获取他们的位置。我使用这个功能:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
获取用户的经纬度位置,我可以通过将它们打印到日志中来查看它们。这很好用。
在我的应用程序的其他地方(但在同一个 viewController.swift
文件中)我有使用 OpenWeatherMap API 的代码,并且我有一个包含 url 的字符串,其中 return JSON。
在我的 viewDidLoad
中,我使用:
getWeatherData("http://api.openweathermap.org/data/2.5/weather?lat=XXXXXX&lon=XXXXXX&appid=(MY-APP-ID)")
我需要将我在 locationManager
函数中获取的 Long 和 Lat 值放入这个字符串中,我知道我可以通过 url 中的 "\()"
来完成] 字符串。
我的问题是,我目前只能在 locationManager
函数中使用这些值。如何将它们存储在此函数之外的值中,以便将它们添加到我的 URL 字符串中?
谢谢
希望这能回答您的问题。
import UIKit
import MapKit
class myClass {
var userLocation: CLLocationCoordinate2D? // The user location as an Optional stored as a var in class "myClass".
// !!! This can be accessed everywhere within the class "myClass" (and deeper)
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate // Change it from var to let since it's only read not writen
// !!! This can be accessed everywhere within the func "locationManager" (and deeper)
userLocation = locValue // !!! Store it if necessary
// Why would you call this in viewDidLoad? I doubt the user location will be available at this point (but it might). You can move this anywhere if you want
// note the "\(name)" this will turn the var name into a string
// if != nil not necessary here since it cannot be nil but still added it regardless
// Maybe you want to add a check so this only gets called on the first location update. It depends on what you need it for.
if userLocation != nil {
getWeatherData("http://api.openweathermap.org/data/2.5/weather?lat=\(userLocation!.latitude)&lon=\(userLocation!.latitude)&appid=(MY-APP-ID)") // Why would you call this in viewDidLoad? I doubt user doubt the user location will be available at this point (but it might)
}
else {
print("Error: User not Located (yet)")
}
}
}