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_地图之显示附近微博的更多相关文章

  1. 百度地图API显示多个标注点带检索框

    通过百度地图的api,可以在地图上显示多个标注点,并给所有的标注点实现了带检索功能的信息框 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...

  2. 百度地图API显示多个标注点并添加百度样式检索窗口

    原作者博客地址:http://blog.csdn.net/a497785609/article/details/24009031 在此基础上进行了修改: 1.添加闭包,将i传入内部 2.添加地图和卫星 ...

  3. Vue系列:在vux的popup组件中使用百度地图遇到显示不全的问题

    问题描述: 将百度地图封装成一个独立的组件BMapComponent,具体见 Vue系列:如何将百度地图包装成Vue的组件(http://www.cnblogs.com/strinkbug/p/576 ...

  4. html5定位并在百度地图上显示

    在开发移动端 web 或者webapp时,使用百度地图 API 的过程中,经常需要通过手机定位获取当前位置并在地图上居中显示出来,这就需要用到html5的地理定位功能. navigator.geolo ...

  5. 百度地图API显示多个标注点,解决提示信息问题以及给标注增加地图旁的文字连接提示的另一种解决办法

    原文:百度地图API显示多个标注点,解决提示信息问题以及给标注增加地图旁的文字连接提示的另一种解决办法 公司的网站改版要求在一个页面显示百度地图.上面要同时显示很多标注点,标注点当然要有提示信息嘛,提 ...

  6. 百度地图API显示多个标注点带百度样式信息检索窗口的代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. OpenLayers在地图上显示统计图,饼图线状图柱状图,修复统计图跳动的问题

    环境介绍 Openlayers ol.js v5.3.0 Highcharts highcharts.js v7.0.1 jquery jquery-3.3.1.js v3.3.1 显示效果 地图放大 ...

  8. 百度地图API 显示区域边界及地名定位

    百度地图API 显示区域边界及地名定位 这个定位一共用了两个方法组成 一个是定位绘制区域边界线,另一个是地名定位 原理: 当用户输入省.市.县.区这种大地名时,我们要定位用户输入的这个位置,并显示轮廓 ...

  9. [Xcode 实际操作]四、常用控件-(17)为MKMapView地图上显示提示框

    目录:[Swift]Xcode实际操作 本文将演示当点击地图上的标注圆点时,弹出信息窗口. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit ...

随机推荐

  1. SharePoint Site "Regional Settings"功能与CSOM的对应

    博客地址:http://blog.csdn.net/FoxDave SharePoint网站中的区域设置:"Regional Settings",可以用CSOM通过Site的一些 ...

  2. iOS 上拉下拉刷新简单实现代码

    一般说到上拉刷新下拉刷新,很多人可能想到的是一个第三方开源框架EGORefresh,下面说下,如何自己写代码实现. UITableView本身是一个UIScrollView,所以UITableView ...

  3. Ruby-1

    Ruby API 文档 http://www.ruby-doc.org/core-2.0.0/ Programming Ruby http://ishare.iask.sina.com.cn/f/22 ...

  4. oracle全文索引

    1.检查数据库是否具有全文检索功能(这是针对已经建成使用的数据库) 查看用户中是否存在ctxsys用户,查询角色里是否存在ctxapp角色.以上两个中的1个不满足(不存在),则说明没有装过全文检索功能 ...

  5. C++ mem_fun 和 mem_fun_ref 的用法

    假设我们有以下的一个类: 另外有一个包含 class A 对象的数组: vector<A> vec; 如何对每一个类的对象调用成员函数print. 做法1: 利用下标 for(int i= ...

  6. c语言中gets ,getschar 和fgets 的用法及三者之间的差别,还有scanf

    ①gets [1]函数:gets(字符指针) [2]头文件:stdio.h(c中),c++不需包含此头文件 [3]原型:char*gets(char*buffer); [4]功能:从stdin流中读取 ...

  7. C#栈

    线性表.栈和队列这三种数据结构的数据元素以及数据元素间的逻辑关系完全相同,差别是线性表的操作不受限制,而栈和队列的操作受到限制.栈的操作只能在表的一端进行, 队列的插入操作在表的一端进行而其它操作在表 ...

  8. linux命令:chgrp

    1.命令介绍: chgrp用来改变文件或目录所属组的权限,要改变成的组必须在/etc/group文件存在才可以. 2.命令格式: chgrp [选项] 组 文件 3.命令参数: 必要参数: -c 当发 ...

  9. Linux服务器之间的目录共享

    1.在Redhat Linux中查看是否已安装NFS及portmap的命令如下:rpm -qa |grep portmaprpm -qa |grep nfs2.如果没有安装这两个软件包,安装命令如下: ...

  10. sql文件批量导入mysql数据库

    有一百多个sql文件肿么破?一行一行地导入数据库肯定是极其愚蠢的做法,但是我差点就这么做了... 网上首先找到的方法是:写一个xxx.sql文件,里边每一行都是source *.sql ...,之后再 ...