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 ...
随机推荐
- Linq中关键字的作用及用法
Linq中关键字的作用及用法 1.All:确定序列中的所有元素是否都满足条件.如果源序列中的每个元素都通过指定谓词中的测试,或者序列为空,则为 true:否则为 false. Demo: 此示例使用 ...
- HTMlhleper
@{ ViewBag.Title = "Index";} <h2>Index</h2> <div> @{ int id=12121; var I ...
- 第二章 搭建Android开发环境
这一章为我们讲解了如何搭建Android开发环境. 首先要了解的是Android底层开发需要哪些工具:搭建android应用程序开发环境.android NDK开发环境和交叉编译环境,前两个用来测试L ...
- windows下无法创建django工程的问题
环境:python2.7 django1.7 安装好django后,将C:\Python27\Lib\site-packages\Django-1.7.7-py2.7.egg\django\bin; ...
- mybatis中的#{}和${}
#{}:相当于预处理中的占位符?. #{}里面的参数表示接收java输入参数的名称. #{}可以接受HashMap.简单类型.POJO类型的参数. 当接受简单类型的参数时,#{}里面可以是value, ...
- [解决方案] pythonchallenge level 4
http://www.pythonchallenge.com/pc/def/linkedlist.php 查看页面源代码或者点击图片 http://www.pythonchallenge.com/pc ...
- hibrenate @ManyToOne(fetch = FetchType.EAGER) 和 lazy 区别
项目中在spring定时器中定时扫描订单想修改订单详细和会员信息时老是报错,说no session...但是在正常的后台操作action中又能用. 对hibernate一直不是很熟悉,只知道用. 如果 ...
- angular 滚动
AngularJs $anchorScroll.$controller.$document $anchorScroll 根据HTML5的规则,当调用这个函数时,它检查当前的url的hash值并且滚动到 ...
- debug和release之间的时间优化问题
最近跑了一个Vibe的代码,其中 加了一句向量的声明: vector<int> binary_delete1,binary_delete2,binary_delete3; 之后程序就会变得 ...
- HANA学习笔记1-搭建HANA学习环境
一 硬件环境 两台电脑,一台为服务器装跑HANA虚拟机,一台为客户端运行HANA_STUDIO 服务器:内存至少需要16G windows server 2003 64位 ...