@interface NearBrandStoreVC ()<MKMapViewDelegate,UIActionSheetDelegate>{
MKMapView *mapView;
MKAnnotationView *annotationView;
有以下属性
@property (nonatomic, assign) CLLocationDegrees strLatitude;//经度
@property (nonatomic, assign) CLLocationDegrees strLongitude;//维度
1.创建地图
#pragma mark -- 地图
//标注目的地
- (void)createMap{
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, _safeInsets.top+44+47, SCREEN_WIDTH, SCREEN_HEIGHT-_safeInsets.top-44-_safeInsets.bottom-47)];
[self.view addSubview:mapView];
mapView.hidden = YES;
mapView.delegate = self;
//用户位置追踪(用户位置追踪用于标记用户当前位置,此时会调用定位服务)
mapView.userTrackingMode = MKUserTrackingModeFollow;
//设置地图类型
mapView.mapType=MKMapTypeStandard;
//添加大头针
2.这是地图的代理方法之一-自定义大头针的样式,以及每个大头针的赋值
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
//由于当前位置的标注也是一个大头针,所以此时需要判断,此代理方法返回nil使用默认大头针视图
if ([annotation isKindOfClass:[NearBrandAnnotation class]]) {
static NSString *key1=@"NearBrandAnnotation";
annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:key1];
//如果缓存池中不存在则新建
if (!annotationView) {
annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:key1];
annotationView.canShowCallout=YES;//允许交互点击
annotationView.calloutOffset=CGPointMake(0, 1);//定义详情视图偏移量
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
btn.backgroundColor = BLUECOLOR;
[btn setTitle:@"到这去" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView=btn;//定义详情左侧视图
annotationView.selected = YES;
objc_setAssociatedObject(btn, @"annotation",annotation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//修改大头针视图
//重新设置此类大头针视图的大头针模型(因为有可能是从缓存池中取出来的,位置是放到缓存池时的位置)
annotationView.annotation=annotation;
annotationView.image=[UIImage imageNamed:@"brandPin"];//设置大头针视图的图片
return annotationView;
}else {
return nil;
下面是我们自己的列表数据---以及如何将地图的数据加载
- (void)addAnnotations{
NSMutableArray *addAnnotations = [NSMutableArray new];
for (int i = 0; i< _nearBrandStores.count; i++) {
YuQuanBrandJiaMengShangListModel *mdoel = _nearBrandStores[i];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(mdoel.last_latitude.floatValue, mdoel.last_longitude.floatValue);
NearBrandAnnotation *annotation=[[NearBrandAnnotation alloc]init];
annotation.title= mdoel.store_name;
annotation.subtitle= mdoel.address;
annotation.coordinate=location;
// [mapView setCenterCoordinate:location zoomLevel:10 animated:NO];
[addAnnotations addObject:annotation];
[mapView addAnnotations:addAnnotations];
其中,需要注意的是,数据的绑定,每个大头针对应一个我们获取到的数据模型,用于点击跳转的时候使用
- (void)turnAction:(UIButton *)button{
NearBrandAnnotation *annotation = objc_getAssociatedObject(button, @"annotation");
_model = [YuQuanBrandJiaMengShangListModel new];
_model.address = annotation.subtitle;
_model.last_longitude = [NSString stringWithFormat:@"%f",annotation.coordinate.longitude];
_model.last_latitude = [NSString stringWithFormat:@"%f",annotation.coordinate.latitude];
[self mapBtnclick];
下面就是上次的跳转三方地图了
- (void)mapBtnclick{
if (![NSString isNotEmptyString:_currentadress]) {
[self.locationManager startUpdatingLocation];
[SDIndicator showInfoWithMessage:@"正在定位,请稍候..."];
return;
_actionSheet= [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil];
NSMutableArray *mapsArray=[[NSMutableArray alloc] initWithCapacity:0];
_mapsUrlArray=[[NSMutableArray alloc] init];
NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
[mapsArray addObject:@"使用iPhone自带地图"];
NSString *urlString=[NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",_strLatitude,_strLongitude,[_model.last_latitude floatValue],[_model.last_longitude floatValue] ];
[_mapsUrlArray addObject:urlString];
NSURL * baidu_App = [NSURL URLWithString:@"baidumap://"];
if ([[UIApplication sharedApplication] canOpenURL:baidu_App]) {
[mapsArray addObject:@"使用百度地图"];
NSString *stringURL =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02",[_model.last_latitude floatValue],[_model.last_longitude floatValue],_model.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[_mapsUrlArray addObject:stringURL];
NSURL * gaode_App = [NSURL URLWithString:@"iosamap://"];
if ([[UIApplication sharedApplication] canOpenURL:gaode_App]) {
[mapsArray addObject:@"使用高德地图"];
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&t=0",@"龙巅鱼邻",_strLatitude,_strLongitude,_currentadress,[_model.last_latitude floatValue],[_model.last_longitude floatValue],_model.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[_mapsUrlArray addObject:urlString];
for (int x=0; x<mapsArray.count; x++) {
[_actionSheet addButtonWithTitle:[mapsArray objectAtIndex:x]];
if (_mapsUrlArray.count>0) {
[_actionSheet showInView:self.view.window];
}else{
[SDIndicator showInfoWithMessage:@"建议您安装高德或者百度地图"];
当然,跳转需要定位自己的经纬度,这个上一篇讲过了