ViewController.m

#import "ViewController.h"
//地图显示需要的头文件
#import <MAMapKit/MAMapKit.h>
//poi搜素需要的头文件
#import <AMapSearchKit/AMapSearchAPI.h>
#import "DetailViewController.h" @interface ViewController ()<MAMapViewDelegate, AMapSearchDelegate,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource> @property (nonatomic,strong)MAMapView *mapView;
@property (nonatomic,strong)AMapSearchAPI *search;
@property (nonatomic,strong)MAUserLocation *location;
@property (nonatomic,strong)AMapPlaceSearchRequest *request;
@property (nonatomic,strong)UISearchBar *searchBar;
@property (nonatomic,strong)UITableView *tableView; @property (nonatomic,strong)NSMutableArray *annotationArr;
@property (nonatomic,strong)NSMutableArray *poisArray; @property (nonatomic,assign)NSInteger index; @end @implementation ViewController
#pragma mark - 页面跳转时需要使用
/* 需要页面跳转时使用
- (void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setPoiPoint:) name:@"test" object:nil];
}
- (void)setPoiPoint:(NSNotification *)notice{
//先移除掉上次搜索的大头针
[self.mapView removeAnnotations:self.annotationArr];
//清空数组
[self.annotationArr removeAllObjects];
NSString *index = notice.object;
AMapPOI *poi = self.poisArray[index.integerValue];
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);
annotation.coordinate = coordinate;
annotation.title = poi.name;
annotation.subtitle = poi.address;
[self.annotationArr addObject:annotation];
[self.mapView addAnnotation:annotation];
}
*/ - (void)viewDidLoad {
[super viewDidLoad]; //增加一个KVO index
[self addObserver:self forKeyPath:@"index" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; self.annotationArr = [[NSMutableArray alloc] init]; [self configApiKey];
[self setMySearchConterl];
[self setMainView];
[self setTableView];
//获取bundleIdentifier
// NSLog(@"bundleIdentifier = %@",[[NSBundle mainBundle] bundleIdentifier]); // Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - 地图显示和搜索部分
/**
* 配置APIKey
*/
- (void)configApiKey{
[MAMapServices sharedServices].apiKey = @"a12bc9db3e3f5ba30482aa704ee0fc29";
}
/**
* 设置地图显示 有这个方法就可以显示用户的位置
*/
- (void)setMainView{
self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
self.mapView.delegate = self;
//设置地图语言 默认是中文
// self.mapView.language = MAMapLanguageEn;
//地图类型 默认是2D栅格地图
// self.mapView.mapType = MAMapTypeSatellite;
//关闭指南针显示
self.mapView.showsCompass = NO;
//关闭比例尺显示
self.mapView.showsScale = NO;
//显示用户位置
self.mapView.showsUserLocation = YES;
//设置跟踪模式
self.mapView.userTrackingMode = MAUserTrackingModeFollow;
[self.view addSubview:self.mapView];
}
/**
* 设置POI搜素请求
*
* @param keyword 搜索需要的关键字
*/
- (void)setPoiSearchMapWithKeyword:(NSString *)keyword{
//初始化检索对象
self.search = [[AMapSearchAPI alloc] initWithSearchKey:[MAMapServices sharedServices].apiKey Delegate:self];
//构建AMapPlaceSearchRequest对象
self.request = [[AMapPlaceSearchRequest alloc] init];
//搜索类型 关键字搜索
self.request.searchType = AMapSearchType_PlaceKeyword;
//设置搜索关键字
self.request.keywords = keyword;
//搜索地点 广州
self.request.city = @[@"guangzhou"];
//开扩展
self.request.requireExtension = YES;
//发起POI搜索
[self.search AMapPlaceSearch:self.request];
}
/**
* POI搜索请求后调用的方法
*
* @param request 搜索请求
* @param response 请求结果
*/
- (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response{
if (response.count == ) {
return;
}
/* 仅仅显示搜索结果的大头针
//先移除掉上次搜索的大头针 不然上一次的大头针会一直存在
[self.mapView removeAnnotations:self.annotationArr];
//清空数组
[self.annotationArr removeAllObjects];
*/
// NSString *responseCount = [NSString stringWithFormat:@"%d",response.count];;
// NSLog(@"responseCount = %@",responseCount);
self.poisArray = [[NSMutableArray alloc] init];
for (AMapPOI *poi in response.pois) {
[self.poisArray addObject:poi];
/* 仅仅显示搜索结果的大头针
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);
annotation.coordinate = coordinate;
annotation.title = poi.name;
annotation.subtitle = poi.address;
[self.annotationArr addObject:annotation];
[self.mapView addAnnotation:annotation];
*/
}
[self.tableView reloadData];
/*需要页面跳转时使用
DetailViewController *dvc = [[DetailViewController alloc] init];
dvc.poisArray = self.poisArray;
[self presentViewController:dvc animated:YES completion:nil];
*/
}
/**
* 设置大头针点击后的气泡
*
* @param mapView mapView
* @param annotation annotation
*
* @return 气泡
*/
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
// if ([annotation isKindOfClass:[MAAnnotationView class]]) {
static NSString *identify = @"annotation";
//在原有的大头针中添加自定义的修饰
MAPinAnnotationView *pointAnnotation = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identify];
if (pointAnnotation == nil) {
//在原有的大头针中创建一个新的自定义的大头针
pointAnnotation = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identify];
}
//设置是否能选中的标题
pointAnnotation.canShowCallout = YES;
//是否允许拖拽
pointAnnotation.draggable = YES;
//是否允许退拽动画
pointAnnotation.animatesDrop = YES;
return pointAnnotation;
}
/**
* 地图定位后就会调用这个方法 酒店
*
* @param mapView 当前的mapView
* @param userLocation userLocation
* @param updatingLocation 位置更新标志
*/
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
// NSLog(@"地图");
if (updatingLocation) {
// NSLog(@"latitude = %f longitude = %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
//确定地图经纬度
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
//设置的当前位置 为地图中心
self.mapView.centerCoordinate = coordinate;
self.location = userLocation;
}
}
#pragma mark - searchBar部分
/**
* 设置searchBar
*/
- (void)setMySearchConterl{
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.frame = CGRectMake(, , self.view.frame.size.width, );
self.searchBar.delegate = self;
self.searchBar.placeholder = @"请输入关键字";
[self.view addSubview:self.searchBar]; }
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
return YES;
}
/**
* 设置左边的“取消”按钮
*
* @param searchBar searchBar
*/
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
self.searchBar.showsCancelButton = YES;
for (id cc in [searchBar.subviews[] subviews]) {
if ([cc isKindOfClass:[UIButton class]]) {
UIButton * cancelButton = (UIButton *)cc;
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
}
}
}// called when text starts editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
return YES;
}// return NO to not resign first responder - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0){
return YES;
}// called before text changes
/**
* 键盘搜索按钮按下就会调用这个方法
*
* @param searchBar searchBar本身
*/
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
// NSLog(@"text = %@",searchBar.text);
//发起POI搜索请求
[self setPoiSearchMapWithKeyword:searchBar.text];
//收起键盘
[searchBar resignFirstResponder];
searchBar.text = @"";
}// called when keyboard search button pressed
/**
* “取消”按钮按下会调用这个方法
* 收起键盘
* @param searchBar searchBar本身
*/
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
self.searchBar.showsCancelButton = NO;
}// called when cancel button pressed #pragma mark - tableView部分
/**
* 设置tableView
*/
- (void)setTableView{
self.tableView = [[UITableView alloc] init];
self.tableView.frame = CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height - );
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
/**
* 设置tableView的row个数
*
* @param tableView tableView本身
* @param section 当前的section
*
* @return 当前section里面的row数
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.poisArray.count;
}
/**
* 设置cell的显示
*
* @param tableView tableView本身
* @param indexPath cell的位置
*
* @return cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identify = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];
}
AMapPOI *poi = (AMapPOI *)self.poisArray[indexPath.row];
cell.textLabel.text = poi.name;
cell.detailTextLabel.text = poi.address;
return cell;
}
/**
* tableView点击时间
*
* @param tableView tableView本身
* @param indexPath 被点击的cell的位置
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.index = indexPath.row;
}
/**
* 实现KVO键值监听的方法
* 值改变后 增加大头针
* @param keyPath keyPath
* @param object self
* @param change 值字典
* @param context
*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(voidvoid *)context{
//先移除掉上次搜索的大头针
[self.mapView removeAnnotations:self.annotationArr];
//清空数组
[self.annotationArr removeAllObjects];
NSString *index = change[@"new"];
AMapPOI *poi = self.poisArray[index.integerValue];
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);
//地图中心点 设置为选中的点
self.mapView.centerCoordinate = coordinate;
annotation.coordinate = coordinate;
//一下两句 就是气泡的显示内容
annotation.title = poi.name;
annotation.subtitle = poi.address;
[self.annotationArr addObject:annotation];
[self.mapView addAnnotation:annotation];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

DetailViewController.m

#import "DetailViewController.h"
#import <AMapSearchKit/AMapSearchAPI.h> @interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation DetailViewController - (void)viewDidLoad {
[super viewDidLoad];
[self setMainView];
// Do any additional setup after loading the view from its nib.
} - (void)setMainView{
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.poisArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identify = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];
}
AMapPOI *poi = (AMapPOI *)self.poisArray[indexPath.row];
cell.textLabel.text = poi.name;
cell.detailTextLabel.text = poi.address;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:[NSString stringWithFormat:@"%d",indexPath.row]];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

原文链接:高德地图---显示/定位/POI搜索/大头针/气泡

AMAP的更多相关文章

  1. 识别网络应用所使用的协议Amap

    识别网络应用所使用的协议Amap   Amap是Kali Linux自带的一款信息收集工具.工作时,它首先向服务器的端口发送内置的触发包(tirgger),然后获取响应.通过分析响应包数据,来识别该端 ...

  2. 时时获得高德地图坐标 http://lbs.amap.com/console/show/picker

    1.高德地图标注 在做开发时,或者做高德地图标注的时候,要用到高德地图的坐标,时时获得高德地图坐标 http://lbs.amap.com/console/show/picker 老的高德地图标注地址 ...

  3. AMap地图加载完成事件

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...

  4. AMap编辑折线、多边形、圆

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...

  5. AMap公交线路查询

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...

  6. AMap行政区查询服务

    AMap.DistrictSearch行政区查询服务插件,提供全国各省.市.县.区的中心点经纬度.行政区边界坐标组.下级行政区等信息.根据行政区边界坐标组可在地图上绘制行政区边界.(本文为原创,并在项 ...

  7. Android 高德地图No implementation found for long com.autonavi.amap.mapcore.MapCore

    此篇博客最后更新时间写自2016.5.18.当下高德地图jar版本为3.3.1. 使用高德地图碰到此问题,纠结许久(接近4个多小时). 记录在此,希望遇到相同问题的读者可以有所借鉴. 错误截图: 导致 ...

  8. vue的地图插件amap

    https://www.jianshu.com/p/0011996b81e2(amap) npm install vue-amap --save

  9. 获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)

    如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)  目标:使用百度定位sdk开发实时移动距离计算功能,根据经纬度的定位,计算行驶公里数并实时刷新界面显示.大家 ...

  10. Android编程 高德地图 中如何重写 定位按键 的触发事件 (com.amap.api.maps2d.LocationSource)点击定位后不仅定位在地图中心点上而且可以设置地图的缩放大小和提示

    在利用高德地图来编写自己的APP的时候,发现了一种对定位按键的重写方法,那就是利用   com.amap.api.maps2d.LocationSource  接口来重写. 什么是定位按键呢,下图中右 ...

随机推荐

  1. 如何通过PowerShell在Visual Studio的Post-build中预热SharePoint站点

    问题现象 Visual Studio在开发SharePoint的时候,发布部署包后,首次打开及调试站点页面的时候会非常的慢 解决方案 使用PowerShell脚本,加载SharePoint插件后遍历所 ...

  2. 带删除的EditText

    在安卓开发中EditText是比较常用的控件之一,那我们平常看到EditText填写了内容之后右边会出现一个删除的按钮,这样可以方便用户对其中文本清空操作,是非常人性化的,我们可以重写EditText ...

  3. OC语言-03-OC语言-三大特性

    一.封装 1> 封装的定义 隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别 2> 封装的好处 可以通过set方法防止为成员变量设置不合理的值 仅向外部提供公 ...

  4. iOS开发 -- 为本地文件添加自定义属性的工具类

    前言:实际开发,我们可能会有这样的需求,就是为文件添加自定义的属性,或者是可以将文件的相关信息添加进该文件的属性中,这样可以以备下次读取利用. 那么本文就是要介绍"拓展文件属性的工具类&qu ...

  5. Cocos2d入门--1--初涉相关属性或代码

    Cocos2d vision:  cocos2d-x-3.8.1 万丈高楼,起于累土.对于一个游戏框架的学习,其实在于框架功能的使用积累,学会了如何在cocos2d游戏引擎的基础上使用它提供的各种功能 ...

  6. win32 应用程序 添加资源

    一.资源 1.字符串资源 LoadString LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 二.窗口类 1.系统类 T ...

  7. 最新Burpsuite Pro v1.7.03 介绍和破解版下载

    0x00 介绍 Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程.所有的工具都共享一个能处理并显示HTTP 消息, ...

  8. Monyer.cn黑客小游戏

    花了一天的时间,Monyer给大家带来了一个有趣的东东——拥有15个关卡的黑客小游戏. 入口http://monyer.com/game/game1 因为一直以来都是大家跟我一起学习网络技术嘛,所以这 ...

  9. 使用Apache Tomcat Maven插件部署运行 Web 项目

    什么是Apache Tomcat Maven Plugin? Maven Plugin 是Apache Tomcat 提供的一个Maven插件,它可以在你没有tomcat容器时将任何一个war项目文件 ...

  10. JJ Ying:越来越跨界的界面设计

    2013年6月29号  星期六  小雨  @大众点评 利用非界面设计的专业知识来提升界面设计 向平面设计跨界 向工业设计的跨界 向摄影跨界 向动向的的跨界 向程序跨界 讲师介绍: JJ Ying /  ...