IOS学习之——地图1 显示地图 +现实地图

本文介绍了地图应用中使用三种不同类型的地图(标准地图、卫星地图、混合地图)及其比例调整方法,并详细解释了如何在地图上显示大头针注释信息,包括实现地图视图代理协议、定义自定义标注类以及位置获取与错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

欢迎转载,转载请注明出处

本文地址:http://blog.csdn.net/zhenggaoxing/article/details/42676033


三种地图

我们日常见到的地图有三种分别是 卫星地图 标准地图 混合地图
代码中做如下设置:
- (IBAction)mapStand:(id)sender {
    mapView.mapType=MKMapTypeStandard;// 标准地图
}

- (IBAction)mapSatellite:(id)sender {
    mapView.mapType=MKMapTypeSatellite; // 卫星地图
}

- (IBAction)mapHybrid:(id)sender {
    mapView.mapType=MKMapTypeHybrid;  // 混合地图
}

混合地图:(卫星照片+道路)


卫星地图:(卫星照片)


标准地图:(抽象地图+路线)


调整地图比例

选好地图类型了,接下来显示怎样的地图区域呢? 
下面看代码:
            //调整地图比例 region:地区范围部位
            MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 1000, 1000);
            
            // animate:又生气的,激活
            // 设置重新显示区域
            [mapView setRegion:viewRegion animated:YES];
第一个参数是 CLLocationCoordinate2D类型的,里面包含经度和纬度
第二个参数表示横跨南北的距离,单位是米
第三个参数表示东西的距离,单位是米

是不是很像笛卡尔坐标:原点,x轴,y轴


大头针注释信息

首先:啥是大头针呢?(pin)就是下图这个紫色的东西

然后啥是大头针注释信息呢?annotation(注释) 你点击大头针出来的文字就是大头针注释

您的位置是title信息
下面这一堆英文,是subtitle信息

如何在地图上显示大头针注释信息呢?

先看代码:
/*-----------------------------------实现mapviewdelegate协议方法------------------------------------*/
// -(某标注视图)地图视图:(某地图视图)为标注准备的视图:某实现了mkannotation的对象
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {

这是一个很典型的代理模式,mapview需要找个人实现title 和subtitle  ,这个人必须会写这两个东东,让他来写就好啦


广告牌:实现效果


X联招聘 :MkAnnotation 协议
@protocol MKAnnotation <NSObject>

// Center latitude and longitude of the annotation view.
// The implementation of this property must be KVO compliant.
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@optional

// Title and subtitle for use by selection UI.
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;

哥行……(实现协议的类)
#import <MapKit/MapKit.h>
// <MKAnnotation> 注释协议
// 实现<MKAnnotation> 协议,就是这个类的全部实名
@interface MapLocation : NSObject<MKAnnotation>


// 街道信息
@property(nonatomic,copy) NSString *streetAddressString;
// 城市信息
@property (nonatomic,copy) NSString *cityString;
// 生,州信息
@property(nonatomic,copy) NSString *stateString;
// 邮政编码
@property(nonatomic,copy) NSString *zipString;
// 坐标
@property (nonatomic,readwrite) CLLocationCoordinate2D coordinate;


@end

#import "MapLocation.h"

@interface MapLocation()<MKAnnotation>


@end

@implementation MapLocation
@synthesize stateString;
@synthesize cityString;
@synthesize streetAddressString;
@synthesize zipString;

// 标题
-(NSString*)title{
    return @"您的位置";
}

// 副标题
-(NSString*)subtitle
{
    NSMutableString *ret=[[NSMutableString alloc]init];
    if(stateString)
        [ret appendString:stateString];
    if (cityString) {
        [ret appendString:cityString];
    }
    if (cityString&&stateString) {
        [ret appendString:@","];
    }
    if (stateString&&(cityString||stateString||zipString)) {
        [ret appendString:@"."];
    }
    if (streetAddressString) {
        [ret appendString:streetAddressString];
    }
    if (zipString) {
        [ret appendFormat:@",%@",zipString];
    }
    
    return ret;
}

@end

老板招工:(指明要实现协议的对象)
/*-----------------------------------实现mapviewdelegate协议方法------------------------------------*/
// -(某标注视图)地图视图:(某地图视图)为标注准备的视图:某实现了mkannotation的对象
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
    
    MKPinAnnotationView *annotationView
    // reusable:可复用的 通过标识列出可复用的注释视图
    = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];
    if(annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                         reuseIdentifier:@"PIN_ANNOTATION"];
    }
    
    annotationView.pinColor = MKPinAnnotationColorPurple;// 紫色的大头真注释
    // drop 滴下落下
    //A Boolean value indicating whether the annotation view is animated onto the screen.
    // 一个布尔值表面这个注释视图是否在屏幕上激活了
    
    // When this property is YES, the map view animates the appearance of pin annotation views by making them appear to drop onto the map at the target point. This animation occurs whenever the view transitions from offscreen to onscreen.
    
    // 当这个值是YES的的时候,地图视图上激活了大头针注释视图,通过让他们出现并且插在目标点的方式。
    
    annotationView.animatesDrop = YES;
    annotationView.canShowCallout = YES;// 显示附加信息
    
    return annotationView;
}

获取位置和错误处理

// 刷新地图定位  根据用户位置刷新地图位置
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    mapView.centerCoordinate=userLocation.location.coordinate;
}

// 地图视图加载失败
-(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"error :%@",[error description]);
}


源代码

https://git.oschina.net/zhengaoxing/NO15.2.1map






































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值