- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 CLLocation 来捕获经度和纬度,然后使用 Alamofire 中的经度和纬度来获取天气。每次,经度和纬度都不会停止更新,也不会打印天气数据(如果你想查看这里是数据的示例链接:http://forecast.weather.gov/MapClick.php?lat=37.33233141&lon=-122.0312186&FcstType=json)
class SampleViewController: UIViewController, CLLocationManagerDelegate {
var locationManager:CLLocationManager!
var startLocation: CLLocation!
var isFetchingWeather = false
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
}
override func viewDidAppear(_ animated: Bool) {
getCurrentLocation()
}
func getCurrentLocation(){
if CLLocationManager.locationServicesEnabled(){
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var userLocation:CLLocation = locations[0]
if isFetchingWeather != false{
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
let requestLink = "http://forecast.weather.gov/MapClick.php?lat=\(userLocation.coordinate.latitude)&lon=\(userLocation.coordinate.longitude)&FcstType=json"
print(requestLink)
Alamofire.request(requestLink).validate().responseJSON
{ response in
switch response.result {
case .success(let data):
let json = JSON(data)
self.weatherData = json["data"].arrayValue
for weather in self.weatherData{
let temp = weather["weather"].stringValue
self.weatherString.append(temp)
}
print (self.weatherString)
if self.startLocation == nil {
self.startLocation = userLocation as! CLLocation
self.locationManager.stopUpdatingLocation()
}
case .failure(let error):
print(error)
}
}
}
else{
print("is fetching weather is false")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}
谢谢。
最佳答案
你真的不应该在你的位置委托(delegate)中运行你的天气请求。相反,在 didUpdateLocations
委托(delegate)中获取您的位置并将其保存到 var。接下来,调用 stopUpdatingLocation()
,然后调用一个单独的函数来发出天气请求。像这样:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last
//check accuracy and timestamp of location to make sure its not a cached/old location (if you don't care about accuracy or time, you can remove this check)
let timeDiff = newLocation?.timestamp.timeIntervalSinceNow
if timeDiff < 5.0 && (newLocation?.horizontalAccuracy)!<=self.accuracyNeeded{
//stop updating location
self.locationManager.stopUpdatingLocation()
//set currentUserLocation
self.myLocation=newLocation?.coordinate
//call function to get weather
//remove delegate
self.locationManager.delegate = nil
}
}
关于swift - CLLocation + 天气(Alamofire)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42893851/
有谁知道在给定顶点列表的情况下以编程方式创建冷/暖锋矢量的方法(注意顶点不包含小三 Angular 形或半圆;[-105W,40.45N]等...)? 我正在尝试在网络 map 应用程序上放置“当前前
在 iOS 天气应用程序的右下角,有一个表格/列表图标。我想知道这是一个栏按钮项目,还是一个自定义图标。 最佳答案 这是一个带有自定义图标的自定义按钮。要查找来自 Apple 的所有模板图标,请转到
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。为了帮助澄清这个问题以便可以重新打开它,visit the help center
很多用户为了提高自己的工作效率,都会在电脑桌面上添加日历,天气,时钟等小工具,防止忘记一些重要的事项。那如何在win10系统桌面添加日历,天气,时钟等小工具呢?下面就跟着小编一起来看看吧。 Win
我试图使用 NOAA 进行每小时预报,但它不允许我输入东经。请求必须是西经。 他们甚至为印度等亚洲国家提供东经服务。 这是我使用的链接 http://graphical.weather.gov/xml
我正在使用 pywapi 来获取不同地点的天气状况。我使用的命令是: alaska = pywapi.get_weather_from_noaa('PABI') 其中“PABI”是阿拉斯加的电台 ID
我目前正在使用 xCode 开发一个应用程序,我是 iOS 新手。我的天气格式当前正在向我的数据库提交“摄氏度”和“华氏度”,而不是发送 C 或 F。我已经尝试了几件事,但看起来它也将我的显示文本更改
我在一些地方看到人们正在寻找天气应用程序效果。这些问题中有很多是针对 Android 应用程序或 JAVA 的,但我正在寻找在 HTML/CSS/Javascript 网站上的使用。这可能吗? 当您查
我正在使用雅虎! Weather RSS Feed 以获取我所在城镇的预报。我可以解析 XML 并获取天气描述,但随后我得到如下结果: Current Conditions: Light Rain,
这个问题已经有答案了: How can I access and process nested objects, arrays, or JSON? (31 个回答) 已关闭 6 年前。 所以,我正在尝
我正在使用 SAX 从 google 天气 API 中提取信息,但我遇到了“条件”问题。 具体来说,我正在使用这段代码: public void startElement (String uri, S
我正在使用一个简单的 Web API 来获取伦敦城市的天气详细信息。但是,我没有得到所需的响应,而是收到 401 错误。我做错了什么吗? HTML $(document).ready(func
我正在使用 Open Weather API,但是当我发送城市数据请求时,出现网络错误。 这是获取资源的操作。 actions/index.js import axios from "axios";
这段代码中的所有内容我都在尝试为用户提供本地的位置和温度但是不知何故温度以摄氏度显示的方式更少而且下面也没有更新是我尝试过的就像它是 4-5 小时回溯数据到 10 度摄氏度不太像如果温度是 22(摄氏
我正在尝试使用 CLLocation 来捕获经度和纬度,然后使用 Alamofire 中的经度和纬度来获取天气。每次,经度和纬度都不会停止更新,也不会打印天气数据(如果你想查看这里是数据的示例链接:h
fullfillment page至于我个人的兴趣,我想使用天气 api 制作一个天气聊天机器人。为此,我使用对话流,但是当我按照 Dialogflow github 进行 webhook 连接时但出
我正在开发一款应用程序,我相信它会对学生有所帮助。我想根据位置包括天气(仅温度)。例如,如果我要从一个城市前往另一个城市,我希望它能针对新城市自动更新。我该怎么做?对于我的第二个问题,我还想将日期(即
有谁知道如何制作像天气/新闻应用程序中的 Nexus One 选项卡这样的选项卡,我的意思是通过轻弹屏幕即可转到下一个选项卡的功能,谢谢 视频:http://www.youtube.com/watch
我正在尝试制作一个天气应用程序。到目前为止,我设法从 rss 中获得了一些基本信息。我正在使用以下代码: if ([currentElement isEqualToString:@"descr
我有一个由站点标识符代码(“usaf”)和日期组织的表面天气观测数据框(fzraHrObs)。 fzraHrObs 有几列天气数据。车站代码和日期(日期时间对象)如下所示: usaf dat
我是一名优秀的程序员,十分优秀!