码迷,mamicode.com
首页 > 移动开发 > 详细

AppleMap苹果原生地图

时间:2015-09-22 21:52:00      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:

//使用苹果原生地图
#import "RootViewController.h"
#import <MapKit/MapKit.h>
@interface RootViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>
{
    //声明地图视图
    MKMapView *mapview;
    //供定位使用的对象
    CLLocationManager *manager;
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUI];
}
-(void)createUI{
    mapview=[[MKMapView alloc] initWithFrame:self.view.frame];
    mapview.delegate=self;     //设置代理
    [self.view addSubview:mapview];
    //地图在显示时,显示的是指定的地理位置      39.896304, 116.410103
    //经纬度如何确定,打开谷歌地图,找到要定位的位置,点击复制经纬度    东软(38.889722,121.535167)
    //确定坐标
    CLLocationCoordinate2D coor=CLLocationCoordinate2DMake(38.889722, 121.535167);
    //确定放大倍数
    MKCoordinateSpan span=MKCoordinateSpanMake(0.1, 0.1);
    //关联坐标位置和放大倍数
    MKCoordinateRegion region=MKCoordinateRegionMake(coor, span);
    //把设置赋值给地图视图
    [mapview setRegion:region];
    //设置自己在地图上的位置
    mapview.showsUserLocation=YES;
    //让自己跟随地图走,导航
    manager=[[CLLocationManager alloc] init];
    //设置代理
    manager.delegate=self;
    //设置人走多远,回调一次地图更新函数
    manager.distanceFilter=500;
    //设置开始定位
    [manager startUpdatingLocation];
    
    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(long_Press:)];
    [mapview addGestureRecognizer:longPress];
}
-(void)long_Press:(UIGestureRecognizer *)sender{
    //添加大头针
    //获得长按手势的中心点坐标
    CGPoint point=[sender locationInView:mapview];
    //根据获得的坐标,计算 出在地图上的经纬度(计算出的经纬度就是大头针的位置)
    CLLocationCoordinate2D coord=[mapview convertPoint:point toCoordinateFromView:mapview];
    //创建大头针
    MKPointAnnotation *pa=[[MKPointAnnotation alloc]  init];
    pa.coordinate=coord;
    pa.title=@"九哥烧烤";
    pa.subtitle=@"满100送30";
    [mapview addAnnotation:pa];
}
//定制系统默认的大头针的样式
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //【说明】大头针的生产与单元格的复用策略相同
    MKPinAnnotationView *pin=(MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:@"applemap"];
    if (pin==nil) {
        pin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"applemap"];
    }
    pin.animatesDrop=YES;
    pin.canShowCallout=YES; //显示气泡
    pin.pinColor=MKPinAnnotationColorRed;
    UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    iv.image=[UIImage imageNamed:@"9y.png"];
    pin.leftCalloutAccessoryView=iv;//大头针左侧视图
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeInfoDark];
    btn.backgroundColor=[UIColor blackColor];
    [btn setTitle:@"点我" forState:UIControlStateNormal];
    pin.rightCalloutAccessoryView=btn; //大头针右侧视图
    return pin;
}
//当使用定位导航时,地图跟随移动,根据设定的距离回调此函数
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation *newlocation=[locations lastObject];   //获取新的坐标位置
    [UIView animateWithDuration:1 animations:^{
        //更新地图坐标
        [mapview setRegion:MKCoordinateRegionMake(newlocation.coordinate, MKCoordinateSpanMake(0.1, 0.1))];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

效果:

技术分享

 

AppleMap苹果原生地图

标签:

原文地址:http://www.cnblogs.com/crushing/p/4830441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!