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 = [[MKMapViewallocinitWithFrame:CGRectMake(0,44320self.view.frame.size.height - 88)];

[mapViewsetMapTypeMKMapTypeStandard];

mapView.delegate=self;

[self.viewaddSubview:mapView];

 

 

5

在dealloc函数中释放

[mapViewrelease];

 

 

移除所有地标

[mapViewremoveAnnotations: [mapViewannotations]];

 

 

#pragma mark -

#pragma mark地图委托

 

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

DebugLog(@"viewForAnnotation");

 

if ([iAnnotationisKindOfClass:[MKUserLocationclass]])

        return nil;

    

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

    {

MapAnnotation *annotation = iAnnotation;

 

staticNSString *AnnotationIdentifier = @"AnnotationIdentifier";

staticNSString *UserIdentifier = @"UserIdentifier";

NSString *Identifier =nil;

 

if (annotation.tag == -1) {

Identifier = AnnotationIdentifier;

}else {

Identifier = UserIdentifier;

}

 

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

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

if (!pinView)

        {

if ([IdentifierisEqualToString:AnnotationIdentifier]) {

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

pinView = [[[MKPinAnnotationViewallocinitWithAnnotation:iAnnotationreuseIdentifier:Identifier]autorelease];

[(MKPinAnnotationView *)pinViewsetPinColor:MKPinAnnotationColorRed];

UIButton *button = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];

[buttonaddTarget:selfaction:@selector(curPositionButtonPressed:)forControlEvents:UIControlEventTouchUpInside];

[pinViewsetImage:[UIImageimageNamed:@"datouzhen.png"]];

pinView.rightCalloutAccessoryView = button;

 

/*

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

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

[(MKPinAnnotationView *)pinView setPinColor:MKPinAnnotationColorRed];

*/

 

 

}else {

pinView = [[[MKAnnotationViewallocinitWithAnnotation:iAnnotationreuseIdentifier:Identifier]autorelease];

 

UIButton *button = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];

button.tag =annotation.tag;

[buttonaddTarget:selfaction:@selector(annotationButtonPressed:)forControlEvents:UIControlEventTouchUpInside];

[pinViewsetImage:[UIImageimageNamed:@"ann.png"]];

pinView.rightCalloutAccessoryView = button;

}

 

            pinView.canShowCallout =YES;

            return pinView;

        }

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

        else

        {

            pinView.annotation = iAnnotation;

        }

 

        return pinView;

    }

returnnil;

}

 

 

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

 

}

 

 

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

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

//inButton.tag

}

 

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

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

//inButton.tag

}

 

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

-(void) setPointAnnotation{

[mapViewremoveAnnotations: [mapViewannotations]];

CLLocationCoordinate2D coordinate = {_position.latitude,_position.longitude};

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

annotation.tag = -1;

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

[mapViewaddAnnotation:annotation];

[mapViewselectAnnotation:annotationanimated:YES];

}

 

 

玩过google app的都知道,我们在地图上加一个目的地的时候,annotationview是从上掉下来的,如何实现这样的效果?经过实战,我找到有两种方法可以完成这样的效果。

第一种是实现MKMapViewDelegate的一个方法,然后自已实现下落的动画效果,代码如下:

  1. - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {   
  2.     MKAnnotationView *aV;   
  3.     for (aV in views) {  
  4.         CGRect endFrame = aV.frame;  
  5.           
  6.         aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - 230.0, aV.frame.size.width, aV.frame.size.height);  
  7.           
  8.         [UIView beginAnimations:nil context:NULL];  
  9.         [UIView setAnimationDuration:0.45];  
  10.         [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  11.         [aV setFrame:endFrame];  
  12.         [UIView commitAnimations];  
  13.           
  14.     }  
  15. }  

 

第二种方法很简单,只需要设置一个annotationview的属性值,也是在MKMapViewDelegate的一个方法中实现,代码如下:
  1. - (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation  
  2. {  
  3.     if (annotation == mV.userLocation) {  
  4.         return nil;  
  5.     }  
  6.     MKPinAnnotationView *pinView = nil;   
  7.     static NSString *defaultPinID = @"custom pin";  
  8.     pinView = (MKPinAnnotationView *)[mV dequeueReusableAnnotationViewWithIdentifier:defaultPinID];  
  9.     if ( pinView == nil )  
  10.     {  
  11.         pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];  
  12.         [pinView setDraggable:YES];  
  13.           
  14.     }  
  15.     pinView.pinColor = MKPinAnnotationColorRed;  
  16.       
  17.     pinView.canShowCallout = YES;  
  18.     pinView.animatesDrop = YES;  
  19.       
  20.     return pinView;  
  21. }  


注意,就是

  1. pinView.animatesDrop = YES;  
起的作用。

posted on 2012-11-15 17:47  无量少年  阅读(414)  评论(0)    收藏  举报

导航