苹果地图2,定位,标注,返回中心。

时间:2014-04-29 15:05:05   收藏:0   阅读:596
mamicode.com,码迷
//
//  ViewController.m
//  mapdemo001
//
//  Created by apple on 14-4-28.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "MyAnnotation.h"

@interface ViewController () <MKMapViewDelegate>
{
    MKMapView *_mapview;
    UIButton *_backBtn;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    MKMapView *mapview = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapview.delegate = self;
    mapview.showsUserLocation = YES;
    [self.view addSubview:mapview];
    
    _backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_backBtn setTitle:@"返回中心位置" forState:UIControlStateNormal];
    _backBtn.frame = CGRectMake(30, 50, 100, 30);
    [_backBtn addTarget:self action:@selector(myselect) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_backBtn];
}

- (void)myselect
{
    // 设置中心位置
    [_mapview setCenterCoordinate:_mapview.userLocation.location.coordinate];
    // 设置跨度
    MKCoordinateSpan span = MKCoordinateSpanMake(0.812988, 0.888449);
    // 显示区域
    MKCoordinateRegion region = MKCoordinateRegionMake(_mapview.userLocation.location.coordinate, span);
    
    // 设置显示区域
    [_mapview setRegion:region animated:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    if (_mapview) {
        return;
    }
    // 设置中心位置
    [mapView setCenterCoordinate:userLocation.location.coordinate];
    // 设置跨度
    MKCoordinateSpan span = MKCoordinateSpanMake(0.812988, 0.888449);
    // 显示区域
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    
    // 设置显示区域
    [mapView setRegion:region animated:YES];
    _mapview = mapView;
}

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    // 当前的中心位置
    //CLLocationCoordinate2D coord = mapView.region.center;
    // 在这个代理方法里找到车或者人,让后添加标注。添加标注以后,会在viewforannotation方法里把标注显示出来
    //mapView addAnnotation:<#(id<MKAnnotation>)#>
    
    
    for (int i=0; i<10; i++) {
        MyAnnotation *anno = [[MyAnnotation alloc] init];
        anno.coordinate = CLLocationCoordinate2DMake(39+arc4random()%100/100.0, 116+arc4random()%100/100.0);
        //
        NSString *str = [NSString stringWithFormat:@"%d",i];
        anno.title = str;
        [mapView addAnnotation:anno];
    }
    
    NSLog(@"%f %f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta);
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *ID = @"MYID";
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
        annotationView.canShowCallout = YES;
    }
    annotationView.annotation = annotation;
    if ([annotation isKindOfClass:[MyAnnotation class]]) {
        // 这个是是标注周围的车 标注周围的车或者商家
        annotationView.image = [UIImage imageNamed:@"ic_category_1.png"];
    } else {
        // 返回nil,表示标注当前位置ic_category_78  标注当前位置
        annotationView.image = [UIImage imageNamed:@"ic_category_78"];
        //return nil;
    }
    
    return annotationView;

}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    MyAnnotation *anno = (MyAnnotation *)view.annotation;
    NSLog(@"%@",anno.title);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end








mamicode.com,码迷
mamicode.com,码迷
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@end
mamicode.com,码迷

 

苹果地图2,定位,标注,返回中心。,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!