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;
}
iOS_地图之显示附近微博的更多相关文章
- 百度地图API显示多个标注点带检索框
通过百度地图的api,可以在地图上显示多个标注点,并给所有的标注点实现了带检索功能的信息框 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...
- 百度地图API显示多个标注点并添加百度样式检索窗口
原作者博客地址:http://blog.csdn.net/a497785609/article/details/24009031 在此基础上进行了修改: 1.添加闭包,将i传入内部 2.添加地图和卫星 ...
- Vue系列:在vux的popup组件中使用百度地图遇到显示不全的问题
问题描述: 将百度地图封装成一个独立的组件BMapComponent,具体见 Vue系列:如何将百度地图包装成Vue的组件(http://www.cnblogs.com/strinkbug/p/576 ...
- html5定位并在百度地图上显示
在开发移动端 web 或者webapp时,使用百度地图 API 的过程中,经常需要通过手机定位获取当前位置并在地图上居中显示出来,这就需要用到html5的地理定位功能. navigator.geolo ...
- 百度地图API显示多个标注点,解决提示信息问题以及给标注增加地图旁的文字连接提示的另一种解决办法
原文:百度地图API显示多个标注点,解决提示信息问题以及给标注增加地图旁的文字连接提示的另一种解决办法 公司的网站改版要求在一个页面显示百度地图.上面要同时显示很多标注点,标注点当然要有提示信息嘛,提 ...
- 百度地图API显示多个标注点带百度样式信息检索窗口的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- OpenLayers在地图上显示统计图,饼图线状图柱状图,修复统计图跳动的问题
环境介绍 Openlayers ol.js v5.3.0 Highcharts highcharts.js v7.0.1 jquery jquery-3.3.1.js v3.3.1 显示效果 地图放大 ...
- 百度地图API 显示区域边界及地名定位
百度地图API 显示区域边界及地名定位 这个定位一共用了两个方法组成 一个是定位绘制区域边界线,另一个是地名定位 原理: 当用户输入省.市.县.区这种大地名时,我们要定位用户输入的这个位置,并显示轮廓 ...
- [Xcode 实际操作]四、常用控件-(17)为MKMapView地图上显示提示框
目录:[Swift]Xcode实际操作 本文将演示当点击地图上的标注圆点时,弹出信息窗口. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit ...
随机推荐
- WebService 生成类的命令语句
在开发项目中,有时候需要调用webservice接口程序,根据项目规定有的项目是直接引入接口,有的是需要把接口生成代理类的形式在项目中使用,根据项目需要来取舍. 以下列出项目中常用的Webservic ...
- 程序内部让用户直接上appstore评价游戏的链接地址以及跳转方法
NSString *str = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore ...
- 第一章 JavaScript简史
JavaScript: 一种使网页具有交互能力的程序设计语言. BOM: 浏览器对象模型,指通过JS用来调整Web浏览器的高度.宽度和位置属性的办法. DHTML: 1.利用HTML标记各种元素 ...
- UIScrollerView遇到UINavigationController
今天在UITabBarController 的第一个Tab 页面中放入一个ScrollView, 原本以为可以正常运行. 结果却让人大跌眼镜. 每当我手动滚动或者 缓慢导航到另外一个页面时,当前的 ...
- C++ const
在程序中经常遇到const,但是对他还不是非常了解,今天看到一篇文章讲挺好的,所以复制过来了.... 原文链接:http://blog.csdn.net/Eric_Jo/article/details ...
- css 修改滚动条
::-webkit-scrollbar { width: 10px;}::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgb ...
- Mybatis3.x与Spring4.x整合(转)
http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:cre ...
- linux命令:xargs
1.命令介绍: xargs用来配合find命令查找的结果然后执行相应的命令 2.命令格式: find -type f -print | xargs file
- github提交代码流程:
(1) 检查一遍代码改动 $git status (2) 将工作目录中的代码提交到暂存区 $ git add filename git add -A (3) 提交代码到本 ...
- etl工具
ETL 工具下载全集 包括 Informatica Datastage Cognos( 持续更新) Datastage 8.0 BT种子下载:http://files.cnblogs.com/ta ...