iphone 如何使用地图MapKit

来源:互联网 发布:java事务例子 编辑:程序博客网 时间:2024/05/16 06:59

转载地址:http://blog.csdn.net/tangaowen/article/details/6527901

                                                           iphone  如何使用地图MapKit 

 

1。  首先在framework中加入 MapKit.framework

 

2。 类中   #import <MapKit/MapKit.h>

 

3。  类中定义  

       CLLocationManager *  locationManager;

       CLLocationCoordinate2D         curLocation;

 

       MKMapView  *    UsermapView;

 

4。类实现  CLLocationManagerDelegate,MKMapViewDelegate 两个delegate

 

 

5。用上一篇中的方法,获得了 用户的所在的位置  curLocation。

 

    (1) 在获取到当前的位置后,显示当前位置所在位置的1000米所在区域。

 

      

[cpp] view plaincopy
  1. //响应当前位置的更新,在这里记录最新的当前位置  
  2. - (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation  
  3.             fromLocation:(CLLocation *)oldLocation   
  4. {  
  5.     NSLog(@"newLocation:%@",[newLocation description]);  
  6.       
  7.     //如果是第一次加载地图  
  8.     if (UsermapView==nil)  
  9.     {  
  10.         UsermapView = [[MKMapView alloc] initWithFrame:self.view.bounds];  
  11.         UsermapView.mapType = MKMapTypeStandard;   
  12.         UsermapView.zoomEnabled=YES;      
  13.         UsermapView.showsUserLocation=YES;  
  14.         UsermapView.delegate=self;  
  15.         [UsermapView   autorelease];  
  16.                 [self.view  addSubview:UsermapView];  
  17.          }  
  18.         
  19.            
  20.         //设置显示区域  
  21.         MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(newLocation.coordinate,1000 ,1000 );  
  22.         [UsermapView setRegion:region animated:TRUE];   
  23. }  
 

 

      如果要显示经纬度表示的(0.05,0.05)范围:

      将上面的 区域设置为下面的代码即可:

      

[cpp] view plaincopy
  1. //设置显示区域  
  2. MKCoordinateSpan  span=MKCoordinateSpanMake(0.05,0.05);  
  3. MKCoordinateRegion  region=MKCoordinateRegionMake(newLocation.coordinate,span);  
 

 

      (2)  如何在地图上增加地图注解

             2。1  首先要从 MKAnnotationView 派生出一个自定义的 类,用来实现个性化的 注解。

            

[cpp] view plaincopy
  1. @interface MapAnnotation : NSObject <MKAnnotation>  
  2. {  
  3.     CLLocationCoordinate2D  coordinate;  
  4.     NSString   *  title;  
  5.     NSString  *  subtitle;  
  6.       
  7.         //自己定义的其他信息成员  
  8. }  
  9. @property (nonatomic,readonly)  CLLocationCoordinate2D  coordinate;  
  10. @property (nonatomic,retain) NSString   *  title;  
  11. @property (nonatomic,retain) NSString  *  subtitle;  
  12. @end  
 

 

            

[cpp] view plaincopy
  1. @implementation MapAnnotation  
  2. @synthesize   coordinate;  
  3. @synthesize   title;  
  4. @synthesize   subtitle;  
  5.   
  6. - (id) initWithCoordinate:(CLLocationCoordinate2D)   temp_coordinate  
  7. {  
  8.      if ([super  init])  
  9.      {  
  10.          coordinate=temp_coordinate;  
  11.      }    
  12.     return  self;  
  13. }  
  14. - (void)  dealloc  
  15. {  
  16.     self.title=nil;  
  17.     self.subtitle=nil;  
  18.     [super  dealloc];  
  19. }  
  20. @end  
 

 

            2。2 怎么添加一个注解:

            

[cpp] view plaincopy
  1.     //标注用户  
  2.      CLLocationCoordinate2D   userLocation;  
  3.      CLLocationDegrees   latitude=111.005;  
  4.      CLLocationDegrees   longtitude=21.015;  
  5.      userLocation.latitude=latitude;  
  6.   userLocation.longitude=longtitude;  
  7.   
  8.   MapAnnotation *  annotation=[[[MapAnnotation alloc]  initWithCoordinate:userLocation]  autorelease];  
  9.   
  10. annotation.title=@"小A";  
  11. annotation.subtitle=@"20岁";  
  12. ermapView   addAnnotation:annotation];  
 

 

             根据 注解 ,产生注解视图

             - (void) mapView:(MKMapView *)mapView   didAddAnnotationViews:(NSArray*) views

             当增加一个注解的时候,上面的函数就会被调用,以产生一个注解视图,下面的代码是产生一个小人图标,放到地图上。

            

[cpp] view plaincopy
  1. //delegate   MKMapViewDelegate  
  2. - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation  
  3. {  
  4.     //判断是否是自己  
  5.        if ([annotation isKindOfClass:[MapAnnotation class]]==YES)  
  6.     {  
  7.         MKAnnotationView *  view;  
  8.         view=(MKAnnotationView *) [mapView  dequeueReusableAnnotationViewWithIdentifier:annotation.title];  
  9.           
  10.           
  11.         if (view==nil)  
  12.         {  
  13.             view=[[[MKAnnotationView  alloc] initWithAnnotation:annotation reuseIdentifier:annotation.title]  autorelease];  
  14.         }  
  15.         else   
  16.         {  
  17.             view.annotation=annotation;  
  18.         }  
  19.           
  20.           
  21.         //设置图标  
  22.         MapAnnotation * Mapannotation=annotation;  
  23.         [view   setImage:[UIImage  imageNamed:@"avatar_boy.png"] ];  
  24.   
  25.         view.canShowCallout=TRUE;  
  26.         return   view;  
  27.     }  
  28.     else   
  29.     {  
  30.         MapAnnotation * Mapannotation=annotation;  
  31.         Mapannotation.title=@"当前位置";  
  32.           
  33.         return  nil;  
  34.     }  
  35. }  
 

 

             当用户点击地图上面的注解视图的时候,也就是点击小人的时候,会调用下面的函数:

             - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

             

            

[cpp] view plaincopy
  1. //当用户点击小人图标的时候,就进入这里,即将显示 AnnotationView  
  2. - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view  
  3. {  
  4.       
  5.     if ([view.annotation isKindOfClass:[MapAnnotation class]]==NO)  
  6.     {  
  7.         return  ;  
  8.     }  
  9.       
  10.         //设置显示的视图的内容  
  11.         MapAnnotation *  annotation=(MapAnnotation *) view.annotation;  
  12.         //通过MapAnnotation就可以获得自己设置的一些个性化信息。  
  13.         //然后,根据这些信息来设置,这里是获得头像的文件路径,然后  
  14.         //设置到 VIEW  
  15.                 UIImageView   *  headImageView= ( UIImageView   *) view.leftCalloutAccessoryView ;  
  16.             [headImageView    setImage:[UIImage  imageWithContentsOfFile: nsFilePath] ];  
  17. }  
 

 

 

             设置VIEW的左右边分别是什么内容,下面是设置左边为一个Image,右边是一个箭头的button。

 

            

[cpp] view plaincopy
  1. - (void) mapView:(MKMapView *)mapView   didAddAnnotationViews:(NSArray*) views  
  2. {  
  3.     int   i=0;  
  4.     for (MKPinAnnotationView     *mkview   in   views     )   
  5.     {  
  6.         //判断是否是自己  
  7.         if ([mkview.annotation isKindOfClass:[MapAnnotation class]]==NO)  
  8.         {  
  9.             continue;     
  10.         }  
  11.         else  
  12.         {  
  13.               UIImageView   *  headImageView=[[UIImageView  alloc] initWithImage:[UIImage   imageNamed:@"online.png"] ];  
  14.               [headImageView  setFrame:CGRectMake(1, 1, 30, 32)];  
  15.               [headImageView  autorelease];  
  16.               mkview.leftCalloutAccessoryView=headImageView;      
  17.           
  18.           
  19.                UIButton  *  rightbutton=[UIButton  buttonWithType:UIButtonTypeDetailDisclosure];  
  20.                mkview.rightCalloutAccessoryView=rightbutton;  
  21.         }  
  22.         i++;  
  23.     }  
  24.       
  25.       
  26. }  
 

 

             当用户点击VIEW后,将会调用下面的函数:

             -(void) mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view  calloutAccessoryControlTapped:(UIControl *) control

 

            

[cpp] view plaincopy
  1. -(void) mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view  calloutAccessoryControlTapped:(UIControl *) control  
  2. {  
  3.     MapAnnotation  *   annotation= view.annotation;  
  4.        
  5.         // 根据 MapAnnotation,取出个性化的个人信息,然后创建自己  
  6.         // 的新的VIEW,并且显示。  
  7. }  
 

 

   (3)  如何检测到地图显示范围的改变

          - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

 

          

[cpp] view plaincopy
  1. //当地图显示范围发生变化的时候  
  2. - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated  
  3. {  
  4.      MKCoordinateRegion   curRegin=UsermapView.region;  
  5.   
  6. }  
    

 

 (4)  如何计算 当前显示范围的  显示半径

 

          

[cpp] view plaincopy
  1. MKCoordinateRegion   curRegin=UsermapView.region;  
  2. MKMapPoint    mapPointCenter=MKMapPointForCoordinate(curRegin.center);  
  3. CLLocationCoordinate2D  outerCoordinate=curRegin.center;  
  4. outerCoordinate.latitude+= curRegin.span.latitudeDelta/2.0;  
  5. outerCoordinate.longitude+=curRegin.span.longitudeDelta/2.0;  
  6. MKMapPoint   mapPointOuter=MKMapPointForCoordinate(outerCoordinate);  
  7.   
  8. CLLocationDistance          double_distance= MKMetersBetweenMapPoints(mapPointCenter,mapPointOuter);  
  9. unsigned  int    cur_distance=(unsigned  int) double_distance;  
  10. NSLog(@"cur_distance=%d",cur_distance);  
 

 

0 0
原创粉丝点击