iOS_地图之显示附近微博

iOS_地图之显示附近微博  iOS_地图之显示附近微博    iOS_地图之显示附近微博

1、首先需要新建一个MKMapView地图对象,在storyBoard中拖拽一个,在工程中导入MapKit.framework;

2、遵守MKMapViewDelegate协议,设定显示地图的显示内容和范围;下面使用的为*的经纬度;注意因为中国的地图有偏移,所以在地图上会定位到*附近;

viewController的viewdidLoad方法中进行设定

 self.mapView.delegate = self;
//116°23′29.29,经度
double longitude = +23.0/+29.29//;
//39°54′24.15,纬度
double latitude = +54.0/+24.15//;
// 注意region为结构体,不能直接赋值;
MKCoordinateRegion region;
region.center.longitude = longitude;
region.center.latitude = latitude;
region.span.latitudeDelta = 0.005;
region.span.longitudeDelta = 0.005; self.mapView.region = region;

3、创建大头针对象的类。只要遵守了 <MKAnnotation>协议的对象,实现[self.mapView addAnnotation:<#(id<MKAnnotation>)#>]即可作为大头针添加到地图上。新建一个WeiBo类来作为大头针对象;实现3个属性的getter方法,确定了大头针的标题描述和位置

weibo.m:

 #import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface WeiBo : NSObject<MKAnnotation>
@property (nonatomic, strong) NSString * userName;
@property (nonatomic , strong) UIImage * userImage;
@property (nonatomic , strong) NSString *text;
/**
* latitude 纬度
longitude 经度
*/
@property (nonatomic , strong) NSDictionary * location;
@end
-(NSString *)title
{
returnself.userName;
} -(NSString *)subtitle
{
returnself.text;
} -(CLLocationCoordinate2D)coordinate
{
CLLocationCoordinate2D co2D;
double latitude = [self.location[@"latitude"] doubleValue];
double longitude = [self.location[@"longitude"] doubleValue];
co2D.latitude = latitude;
co2D.longitude = longitude;
return co2D;
}

4、新建一个manager类来获取数据,想新浪发送网络请求附近地点的微博,并把请求到的数据解析出来,赋值给weibo对象存到一个数组中返回;

manager.h:

 #import <Foundation/Foundation.h>
#import "AFNetworking.h" typedefvoid(^ReturnValueBlock)(NSArray * value); @interface Manager : NSObject + (instancetype)shared; - (void)requestNearbyWeiBoWithLat:(CGFloat)lattude
andLong:(CGFloat)longitude
andRange:(NSInteger)range
andCount:(NSInteger)count
andValue:(ReturnValueBlock)value;
@end

manager.m

 #define Token @"2.002PAyaD0jZRAv478009fa180Dydir"
#define PlaceURL @"https://api.weibo.com/2/place/nearby_timeline.json"
#import "Manager.h"
#import "WeiBo.h" @interfaceManager () @property (nonatomic, strong) AFHTTPRequestOperationManager * afManager; @end @implementation Manager + (instancetype)shared
{
staticManager * m = nil;
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
m = [[Manageralloc] init];
});
return m;
} - (instancetype)init
{
self = [superinit];
if (self) {
self.afManager = [[AFHTTPRequestOperationManageralloc] init];
self.afManager.responseSerializer = [AFHTTPResponseSerializerserializer];
}
returnself;
} -(void)requestNearbyWeiBoWithLat:(CGFloat)lattude andLong:(CGFloat)longitude andRange:(NSInteger)range andCount:(NSInteger)count andValue:(ReturnValueBlock)value
{
NSDictionary * dic = @{@"access_token":Token,@"lat":@(lattude),@"long":@(longitude),@"count":@(count),@"range":@(range)}; [self.afManagerGET:PlaceURLparameters:dic success:^void(AFHTTPRequestOperation * op, NSData * data) {
NSDictionary * dicData = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];
NSArray * arr = dicData[@"statuses"];
NSLog(@"请求结果:%ld",arr.count);
NSMutableArray * arrWeiBo = [NSMutableArrayarrayWithCapacity:arr.count];
for (NSDictionary * dic in arr)
{
WeiBo * weiBoObj = [selffetchWeiBoModelWithDic:dic];
[arrWeiBo addObject:weiBoObj];
}
value([NSArrayarrayWithArray:arrWeiBo]); } failure:^void(AFHTTPRequestOperation * op, NSError * error)
{
NSLog(@"%@",error.localizedDescription);
}]; } - (WeiBo *)fetchWeiBoModelWithDic:(NSDictionary *)dic
{
WeiBo * weibo = [[WeiBoalloc] init];
weibo.userName = dic[@"user"][@"name"];
weibo.text = dic[@"text"];
NSURL * imageURL = [NSURLURLWithString:dic[@"user"][@"profile_image_url"]];
NSData * data = [NSDatadataWithContentsOfURL:imageURL];
UIImage * image = [UIImageimageWithData:data];
weibo.userImage = image; weibo.location = @{@"longitude":dic[@"geo"][@"coordinates"][],@"latitude":dic[@"geo"][@"coordinates"][]};
return weibo;
}

5、在viewController中请求微博数据,并在地图上显示;通过weibo中的3个getter方法就将数据传给大头针了;

    [self.managerrequestNearbyWeiBoWithLat:latitude andLong:longitude andRange:200andCount:20andValue:^(NSArray *value) {
self.arrWeiBo = value;
for (WeiBo * wbObj in value)
{
WeiBo * wb = wbObj;
[self.mapViewaddAnnotation:wb];
}
}];

6、实现代理方法自定义大头针,显示用户头像;在vc中添加了一个int型成员变量 _count来更换用户头像;

 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKAnnotationView * view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"wb"];
if (view == nil)
{
view = [[MKAnnotationViewalloc] initWithAnnotation:annotation reuseIdentifier:@"wb"];
}
WeiBo * wb = self.arrWeiBo[_count];
     使用了自己封装的一个方法来生成一个圆形带边框的头像;
view.image = [UIImagegetCircleIconWithImage:wb.userImageandRadius:20andBorder:3andColor:[UIColorblueColor]]; UIImageView * imageV = [[UIImageViewalloc]initWithFrame:CGRectMake(, , , )];
imageV.image = wb.userImage;
view.leftCalloutAccessoryView = imageV;
 是否可以点击大头针显示详细信息;
view.canShowCallout = YES;
//[view setSelected:YES animated:NO]; _count ++;
return view;
}
上一篇:WPF学习笔记4——Layout之2


下一篇:C#初学单例模式