iphone MKMapView

目前几乎所有服务行业的产品都考虑到加入地图的功能,这大大提升了软件的易用和直观性。在iphone 中运用地图并不是一件困难的事情,因为SDK中已经提供了地图控件。


下边是我总结iphone地图的用法。




1。

现在.h文件中添加地图头文件

#import <MapKit/MapKit.h>



2

在.h文件中添加地图委托<MKMapViewDelegate>



3。

在.h文件中声明地图成员变量以及用到的函数

//地图变量

MKMapView *mapView;


//地图自动缩放。用于在设置过MapAnnotation地标后,执行次函数,就会自动的缩放地图到合适的大小。

- (void)zoomToFitMapAnnotations:(MKMapView*)inMapView;



4。

在.m的init函数中创建地图控件

//map

mapView = [[MKMapView allocinitWithFrame:CGRectMake(044320self.view.frame.size.height -88)];

[mapView setMapTypeMKMapTypeStandard];

mapView.delegate=self;

[self.view addSubview:mapView];



5

在dealloc函数中释放

[mapView release];



移除所有地标

[mapView removeAnnotations: [mapView annotations]];



#pragma mark -

#pragma mark 地图委托


- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)iAnnotation{

DebugLog(@"viewForAnnotation");

if ([iAnnotation isKindOfClass:[MKUserLocation class]])

        return nil;

    

    if ([iAnnotation isKindOfClass:[MapAnnotation class]]) // for Golden Gate Bridge

    {

MapAnnotation *annotation = iAnnotation;

static NSString *AnnotationIdentifier = @"AnnotationIdentifier";

static NSString *UserIdentifier = @"UserIdentifier";

NSString *Identifier = nil;

if (annotation.tag == -1) {

Identifier = AnnotationIdentifier;

}else {

Identifier = UserIdentifier;

}

        MKAnnotationView* pinView = (MKAnnotationView *) [mapViewdequeueReusableAnnotationViewWithIdentifier:Identifier];

//如果不存在的情况就创建

if (!pinView)

        {

if ([Identifier isEqualToString:AnnotationIdentifier]) {

//自定义的大头针样式,这样的好处是,点击此按钮,会弹出一个提示框,并且提升框可以响应点击。

pinView = [[[MKPinAnnotationView allocinitWithAnnotation:iAnnotation reuseIdentifier:Identifier]autorelease];

[(MKPinAnnotationView *)pinView setPinColor:MKPinAnnotationColorRed];

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

[button addTarget:self action:@selector(curPositionButtonPressed:)forControlEvents:UIControlEventTouchUpInside];

[pinView setImage:[UIImage imageNamed:@"datouzhen.png"]];

pinView.rightCalloutAccessoryView = button;


/*

//默认的红色大头针,点击只能显示提示框,但不能点击此按钮。

pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:iAnnotation reuseIdentifier:Identifier] autorelease];

[(MKPinAnnotationView *)pinView setPinColor:MKPinAnnotationColorRed];

*/


}else {

pinView = [[[MKAnnotationView allocinitWithAnnotation:iAnnotation reuseIdentifier:Identifier]autorelease];

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

button.tag = annotation.tag;

[button addTarget:self action:@selector(annotationButtonPressed:)forControlEvents:UIControlEventTouchUpInside];

[pinView setImage:[UIImage imageNamed:@"ann.png"]];

pinView.rightCalloutAccessoryView = button;

}

            pinView.canShowCallout =YES;

            return pinView;

        }

//如果这个已经存在,就需要重新给pinView.annotation设置值。

        else

        {

            pinView.annotation = iAnnotation;

        }

        return pinView;

    }

return nil;

}



- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)viewcalloutAccessoryControlTapped:(UIControl *)control {

}



//选中了红色大头针,会显示提示按钮。点击按钮执行的函数。

- (void)curPositionButtonPressed:(UIButton *)inButton {

//inButton.tag

}


//单击某一个annotation标签的时候,会显示一个提示,提示框上有一个右箭头按钮。点击此按钮的时候,执行此函数。

- (void)annotationButtonPressed:(UIButton *)inButton {

//inButton.tag

}


//插入图钉。并设置图钉的名称和位置

-(void) setPointAnnotation{

[mapView removeAnnotations: [mapView annotations]];

CLLocationCoordinate2D coordinate = {_position.latitude_position.longitude};

MapAnnotation *annotation = [[[MapAnnotation allocinitWithCoordinate:coordinate] autorelease];

annotation.tag = -1;

annotation.title = @"提示信息";

[mapView addAnnotation:annotation];

[mapView selectAnnotation:annotation animated:YES];

}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值