目前几乎所有服务行业的产品都考虑到加入地图的功能,这大大提升了软件的易用和直观性。在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 alloc] initWithFrame:CGRectMake(0, 44, 320, self.view.frame.size.height -88)];
[mapView setMapType: MKMapTypeStandard];
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 alloc] initWithAnnotation: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 alloc] initWithAnnotation: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 alloc] initWithCoordinate:coordinate] autorelease];
annotation.tag = -1;
annotation.title = @"提示信息";
[mapView addAnnotation:annotation];
[mapView selectAnnotation:annotation animated:YES];
}