Commit 51cce7a2 authored by Sandy's avatar Sandy

首页定位天气功能

parent 74a48f81
......@@ -48,6 +48,10 @@
8F250D8F1F29CF190031B4BC /* ZJAppInstance.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F250D8E1F29CF190031B4BC /* ZJAppInstance.m */; };
8F250D921F29D4B40031B4BC /* UserInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F250D911F29D4B40031B4BC /* UserInfo.m */; };
8F250D961F29D86E0031B4BC /* ZJPermissionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F250D951F29D86E0031B4BC /* ZJPermissionManager.m */; };
8F6C92381F2ADF4F0060E983 /* HomeWeatherBoard.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F6C92371F2ADF4F0060E983 /* HomeWeatherBoard.m */; };
8F6C923B1F2ADFEC0060E983 /* WeatherModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F6C923A1F2ADFEC0060E983 /* WeatherModel.m */; };
8F6C923E1F2AE3430060E983 /* HomeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F6C923D1F2AE3430060E983 /* HomeViewController.m */; };
8F6C92421F2AE4AD0060E983 /* LocationHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F6C92411F2AE4AD0060E983 /* LocationHelper.m */; };
8F8ED4B71F29EEE600F48461 /* RootNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F8ED4B61F29EEE600F48461 /* RootNavigationController.m */; };
8F973C1F1F21FD54003D3ADB /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F973C121F21FB77003D3ADB /* ViewController.m */; };
8F973C251F21FD54003D3ADB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8F973BF51F21F6DD003D3ADB /* LaunchScreen.storyboard */; };
......@@ -165,6 +169,14 @@
8F250D911F29D4B40031B4BC /* UserInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UserInfo.m; sourceTree = "<group>"; };
8F250D941F29D86E0031B4BC /* ZJPermissionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZJPermissionManager.h; sourceTree = "<group>"; };
8F250D951F29D86E0031B4BC /* ZJPermissionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZJPermissionManager.m; sourceTree = "<group>"; };
8F6C92361F2ADF4F0060E983 /* HomeWeatherBoard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HomeWeatherBoard.h; sourceTree = "<group>"; };
8F6C92371F2ADF4F0060E983 /* HomeWeatherBoard.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HomeWeatherBoard.m; sourceTree = "<group>"; };
8F6C92391F2ADFEC0060E983 /* WeatherModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WeatherModel.h; sourceTree = "<group>"; };
8F6C923A1F2ADFEC0060E983 /* WeatherModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WeatherModel.m; sourceTree = "<group>"; };
8F6C923C1F2AE3430060E983 /* HomeViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HomeViewController.h; sourceTree = "<group>"; };
8F6C923D1F2AE3430060E983 /* HomeViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HomeViewController.m; sourceTree = "<group>"; };
8F6C92401F2AE4AD0060E983 /* LocationHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocationHelper.h; sourceTree = "<group>"; };
8F6C92411F2AE4AD0060E983 /* LocationHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationHelper.m; sourceTree = "<group>"; };
8F8ED4B51F29EEE600F48461 /* RootNavigationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RootNavigationController.h; sourceTree = "<group>"; };
8F8ED4B61F29EEE600F48461 /* RootNavigationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RootNavigationController.m; sourceTree = "<group>"; };
8F973BF11F21F6DD003D3ADB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
......@@ -509,6 +521,15 @@
path = ZJPermissionManager;
sourceTree = "<group>";
};
8F6C923F1F2AE4AD0060E983 /* LocationHelper */ = {
isa = PBXGroup;
children = (
8F6C92401F2AE4AD0060E983 /* LocationHelper.h */,
8F6C92411F2AE4AD0060E983 /* LocationHelper.m */,
);
path = LocationHelper;
sourceTree = "<group>";
};
8F973BDB1F21F6DD003D3ADB = {
isa = PBXGroup;
children = (
......@@ -557,6 +578,7 @@
8F973C041F21FB67003D3ADB /* Tools */ = {
isa = PBXGroup;
children = (
8F6C923F1F2AE4AD0060E983 /* LocationHelper */,
8F250D931F29D8420031B4BC /* ZJPermissionManager */,
8F250D8C1F29CF120031B4BC /* ZJAppInstance */,
8F250D601F2980BB0031B4BC /* ZJBaseHttpManager */,
......@@ -602,6 +624,8 @@
8F973C0A1F21FB77003D3ADB /* Models */ = {
isa = PBXGroup;
children = (
8F6C92391F2ADFEC0060E983 /* WeatherModel.h */,
8F6C923A1F2ADFEC0060E983 /* WeatherModel.m */,
);
path = Models;
sourceTree = "<group>";
......@@ -609,6 +633,8 @@
8F973C0B1F21FB77003D3ADB /* ViewControllers */ = {
isa = PBXGroup;
children = (
8F6C923C1F2AE3430060E983 /* HomeViewController.h */,
8F6C923D1F2AE3430060E983 /* HomeViewController.m */,
);
path = ViewControllers;
sourceTree = "<group>";
......@@ -623,6 +649,8 @@
8F973C0D1F21FB77003D3ADB /* Views */ = {
isa = PBXGroup;
children = (
8F6C92361F2ADF4F0060E983 /* HomeWeatherBoard.h */,
8F6C92371F2ADF4F0060E983 /* HomeWeatherBoard.m */,
);
path = Views;
sourceTree = "<group>";
......@@ -925,9 +953,11 @@
8F250D4F1F297DA80031B4BC /* XWFilterAnimator+XWMod.m in Sources */,
8FEF5E6C1F285E56001496E3 /* NSData+EncodeAdditions.m in Sources */,
8F250D511F297DA80031B4BC /* XWFilterAnimator+XWPageCurl.m in Sources */,
8F6C92381F2ADF4F0060E983 /* HomeWeatherBoard.m in Sources */,
8F250D961F29D86E0031B4BC /* ZJPermissionManager.m in Sources */,
8F250D921F29D4B40031B4BC /* UserInfo.m in Sources */,
8F1040B91F25C8A000B4C16E /* MineTableViewController.m in Sources */,
8F6C923B1F2ADFEC0060E983 /* WeatherModel.m in Sources */,
8F250D801F29BDC40031B4BC /* LoginViewController.m in Sources */,
8F250D471F297DA80031B4BC /* XWFilterAnimator+XWBoxBlur.m in Sources */,
8F250D3F1F297DA80031B4BC /* XWCoolAnimator+XWScanning.m in Sources */,
......@@ -938,6 +968,7 @@
8F973C341F21FE32003D3ADB /* main.m in Sources */,
8FEF5EA61F28612B001496E3 /* BaseIQManagerView.m in Sources */,
8F250D871F29C2420031B4BC /* LoginRequest.m in Sources */,
8F6C92421F2AE4AD0060E983 /* LocationHelper.m in Sources */,
8F250D4B1F297DA80031B4BC /* XWFilterAnimator+XWFlash.m in Sources */,
8FEF5EAC1F28612B001496E3 /* BaseStateModel.m in Sources */,
8FEF5EB01F28612B001496E3 /* ZJBaseFileModel.m in Sources */,
......@@ -970,6 +1001,7 @@
8FEF5E661F285E56001496E3 /* ChineseLogHelper.m in Sources */,
8F250D591F297DA80031B4BC /* XWFilterTransitionView.m in Sources */,
8F250D571F297DA80031B4BC /* XWFilterAnimator.m in Sources */,
8F6C923E1F2AE3430060E983 /* HomeViewController.m in Sources */,
8FEF5EA81F28612B001496E3 /* BaseListViewController.m in Sources */,
8F250D551F297DA80031B4BC /* XWFilterAnimator+XWSwipe.m in Sources */,
8F250D491F297DA80031B4BC /* XWFilterAnimator+XWCopyMachine.m in Sources */,
......@@ -1108,7 +1140,7 @@
INFOPLIST_FILE = "$(SRCROOT)/HDMall/HDMall_TEST.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.gomoretech.HDMall;
PRODUCT_BUNDLE_IDENTIFIER = com.gomoretech.HDMall.zj;
PRODUCT_NAME = "$(TARGET_NAME)";
USER_HEADER_SEARCH_PATHS = "\"${PODS_ROOT}/**";
};
......@@ -1129,7 +1161,7 @@
INFOPLIST_FILE = "$(SRCROOT)/HDMall/HDMall_TEST.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.gomoretech.HDMall;
PRODUCT_BUNDLE_IDENTIFIER = com.gomoretech.HDMall.zj;
PRODUCT_NAME = "$(TARGET_NAME)";
USER_HEADER_SEARCH_PATHS = "\"${PODS_ROOT}/**";
};
......
This diff is collapsed.
//
// WeatherModel.h
// RealEstateManagement
//
// Created by Z on 16/7/6.
// Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface WeatherModel : NSObject
@property (nonatomic, copy) NSString *weather;
@property (nonatomic, copy) NSString *wind;
@property (nonatomic, copy) NSString *temperature;
@property (nonatomic, copy) NSString *date;
@property (nonatomic, copy) NSString *dayPictureUrl;
@property (nonatomic, copy) NSString *nightPictureUrl;
@property (nonatomic, copy) NSString *city;
@end
//
// WeatherModel.m
// RealEstateManagement
//
// Created by Z on 16/7/6.
// Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//
#import "WeatherModel.h"
@implementation WeatherModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
@end
//
// HomeViewController.h
// HDMall
//
// Created by Javen on 2017/7/28.
// Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//
#import "BaseViewController.h"
@interface HomeViewController : BaseViewController
@end
//
// HomeViewController.m
// HDMall
//
// Created by Javen on 2017/7/28.
// Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//
#import "HomeViewController.h"
#import "HomeWeatherBoard.h"
@interface HomeViewController ()
@property (strong, nonatomic) HomeWeatherBoard *homeBoard;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleDefault;
}
ZJLazy(HomeWeatherBoard, homeBoard)
@end
//
// HomeWeatherBoard.h
// HDMall
//
// Created by Javen on 2017/7/28.
// Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "WeatherModel.h"
#import "LocationHelper.h"
@interface HomeWeatherBoard : UIView
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UILabel *city;
@property (weak, nonatomic) IBOutlet UILabel *detail;
@property (strong, nonatomic) WeatherModel *weatherModel;
/** 顶部天气的背景 */
@property (weak, nonatomic) IBOutlet UIImageView *imgHomeTopBg;
- (void)configWithModel:(WeatherModel *)model;
@end
//
// HomeWeatherBoard.m
// HDMall
//
// Created by Javen on 2017/7/28.
// Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//
#import "HomeWeatherBoard.h"
@implementation HomeWeatherBoard
- (void)awakeFromNib {
[super awakeFromNib];
WS(weakSelf);
[[LocationHelper shareInstance] getCityNameSuccess:^(NSString *string) {
[weakSelf httpGetWeather:string];
}
failed:nil];
}
- (void)configWithModel:(WeatherModel *)model {
if (model.city.length > 0) {
self.city.text = model.city;
[self.img sd_setImageWithURL:[NSURL URLWithString:model.dayPictureUrl]];
self.detail.text = [NSString stringWithFormat:@"%@ %@ %@", model.weather, model.wind, model.temperature];
}
}
/** 根据城市名称获取天气 */
- (void)httpGetWeather:(NSString *)cityName
{
if (nil != self.weatherModel) {
return;
}
NSDictionary *myDictionary = @{ @"ak" : BAIDU_AK, @"location" : cityName, @"output" : @"json" };
WS(weakSelf);
[ZJHttpManager GET:WEATHER_SERVER_URL parameters:myDictionary complete:^(id responseObject, NSError *error) {
//更新天气信息
weakSelf.weatherModel = [[WeatherModel alloc] init];
weakSelf.weatherModel.city = myDictionary[@"location"];
[weakSelf.weatherModel setValuesForKeysWithDictionary:responseObject[@"results"][0][@"weather_data"][0]];
[weakSelf configWithModel:weakSelf.weatherModel];
}];
}
@end
......@@ -27,6 +27,10 @@
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始终访问位置</string>
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能访问位置</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
......
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "home_bg.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "homepage_logo_origin.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "location_icon.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "location_icon@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
......@@ -22,6 +22,7 @@
//third
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
//categories
......
......@@ -23,6 +23,11 @@ static NSString *const BASE_IP = @"http://gomoredev.natapp4.cc";
#endif
#define REST_BASE_URL [NSString stringWithFormat:@"%@/palmmall-server/rest", BASE_IP] //常用
#define REST_BASE_URL [NSString stringWithFormat:@"%@/palmmall-server/rest", BASE_IP]
//天气api
static NSString *const WEATHER_SERVER_URL = @"http://api.map.baidu.com/telematics/v3/weather";
static NSString *const BAIDU_AK = @"442b5fb0a1dc4dcbbccd26975e180e29";
#endif /* URL_h */
//
// LocationHelper.h
// RealEstateManagement
//
// Created by Javen on 2016/10/10.
// Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef void (^locationBlock)(NSString *string);
@interface LocationHelper : NSObject
@property (nonatomic, copy) locationBlock blockSuccess;
@property (nonatomic, copy) locationBlock blockFailed;
+ (LocationHelper *)shareInstance;
/**
* 获取当前城市名
*
* @param success 成功回调
* @param failed 失败回调
*/
- (void)getCityNameSuccess:(locationBlock)success failed:(locationBlock)failed;
@end
//
// LocationHelper.m
// RealEstateManagement
//
// Created by Javen on 2016/10/10.
// Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//
#import "LocationHelper.h"
#import <CoreLocation/CoreLocation.h>
@interface LocationHelper ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation LocationHelper
+ (LocationHelper *)shareInstance {
static LocationHelper *helper = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[LocationHelper alloc] init];
});
return helper;
}
- (void)getCityNameSuccess:(locationBlock)success failed:(locationBlock)failed {
self.blockSuccess = success;
self.blockFailed = failed;
[self startLocation];
}
#pragma mark - 定位方法
//开始定位
- (void)startLocation {
if ([CLLocationManager locationServicesEnabled]) {
// CLog(@"--------开始定位");
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
//控制定位精度,越高耗电量越
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// 总是授权
[self.locationManager requestAlwaysAuthorization];
self.locationManager.distanceFilter = 10.0f;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if ([error code] == kCLErrorDenied) {
if (self.blockFailed) {
self.blockFailed(@"访问被拒绝");
}
}
if ([error code] == kCLErrorLocationUnknown) {
if (self.blockFailed) {
self.blockFailed(@"无法获取位置信息");
}
}
}
//定位代理经纬度回调
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *newLocation = locations[0];
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
WS(weakSelf);
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *array, NSError *error) {
if (array.count > 0) {
CLPlacemark *placemark = [array objectAtIndex:0];
//获取城市
NSString *city = placemark.locality;
NSString *zipCode = placemark.postalCode;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
weakSelf.blockSuccess(city);
} else if (error == nil && [array count] == 0) {
NSLog(@"No results were returned.");
} else if (error != nil) {
NSLog(@"An error occurred = %@", error);
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment