zoukankan      html  css  js  c++  java
  • IOS开发地图 (mapkit)实验

      IOS 地图控件 : mapkit 

      第一步

          显示地图

    1. - (void)viewDidLoad  
    2. {    
    3.     self.mapView=[[[MKMapView alloc] initWithFrame:self.view.bounds] autorelease];  
    4.     mapView.delegate=self;  
    5.     mapView.autoresizingMask= (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);  
    6.     [self.view addSubview:mapView];  
    7.     [self.mapView setZoomEnabled:YES];  
    8.     [self.mapView setScrollEnabled:YES];     
    9.         
    10.     [super viewDidLoad];  
    11.     // Do any additional setup after loading the view from its nib.   
    12. }   
    - (void)viewDidLoad
    {  
        self.mapView=[[[MKMapView alloc] initWithFrame:self.view.bounds] autorelease];
        mapView.delegate=self;
        mapView.autoresizingMask= (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
        [self.view addSubview:mapView];
        [self.mapView setZoomEnabled:YES];
        [self.mapView setScrollEnabled:YES];   
          
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    } 
    也可以直接托控件。 直接运行,OK . 没问题。  

    第二步:

     启动定位服务,标注自己当前位置   

    1. if (lm) {  
    2.           lm.delegate=nil;  
    3.           [lm release];  
    4.           lm=nil;  
    5.       }   
    6.       lm=[[CLLocationManager alloc] init];    
    7.       lm.delegate=self;  
    8.       lm.desiredAccuracy= kCLLocationAccuracyNearestTenMeters;  
    9.       lm.distanceFilter =1000.0f;   
    10.       [lm startUpdatingLocation];      
      if (lm) {
                lm.delegate=nil;
                [lm release];
                lm=nil;
            } 
            lm=[[CLLocationManager alloc] init];  
            lm.delegate=self;
            lm.desiredAccuracy= kCLLocationAccuracyNearestTenMeters;
            lm.distanceFilter =1000.0f; 
            [lm startUpdatingLocation];    
    

     如果定位成功 :

    1. -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation  
    2.  {  
    3.      if (!newLocation) {  
    4.          [self locationManager:manager didFailWithError:(NSError *)NULL];   
    5.      }   
    6.      if (signbit(newLocation.horizontalAccuracy)) {  
    7.          [self locationManager:manager didFailWithError:(NSError *)NULL];    
    8.          return;  
    9.      }   
    10.      [manager stopUpdatingLocation];   
    11.      CLLocationCoordinate2D _coordination = [newLocation coordinate];   
    12.      now_lat = _coordination.latitude;  
    13.      now_lng =_coordination.longitude;   
    14.      mapView.showsUserLocation =YES;  
    15.      [lm stopUpdatingLocation];   
    16.      ///    
    17.      [NSThread detachNewThreadSelector:@selector(getNear) toTarget:self withObject:nil];  
    18.  }   
    -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
     {
         if (!newLocation) {
             [self locationManager:manager didFailWithError:(NSError *)NULL]; 
         } 
         if (signbit(newLocation.horizontalAccuracy)) {
             [self locationManager:manager didFailWithError:(NSError *)NULL];  
             return;
         } 
         [manager stopUpdatingLocation]; 
         CLLocationCoordinate2D _coordination = [newLocation coordinate]; 
         now_lat = _coordination.latitude;
         now_lng =_coordination.longitude; 
         mapView.showsUserLocation =YES;
         [lm stopUpdatingLocation]; 
         /// 
         [NSThread detachNewThreadSelector:@selector(getNear) toTarget:self withObject:nil];
     } 
    
      这样便可以定自己当前位置。   

    /***

     线程是根据当前经纬度 从网络上获取附近 。

     返回JSON 字符串, 然后解析json 。

     得到每个对象经纬度

     **/  

    第三步:

      在地图上画圈

          首先在 头文件定义 :

    1. @property(nonatomic,retain)MKCircle *circle;  
     @property(nonatomic,retain)MKCircle *circle;

      圈的颜色,属性

    1. -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay  
    2. {   
    3.     MKOverlayView *view=nil;  
    4.     if ([overlay isKindOfClass:[MKCircle class]]) {  
    5.         MKCircleView *cirView =[[MKCircleView alloc] initWithCircle:overlay];   
    6.         cirView.fillColor=[UIColor redColor];   
    7.         cirView.strokeColor=[UIColor redColor];   
    8.         cirView.alpha=0.1;  
    9.         cirView.lineWidth=4.0;  
    10.         view=[cirView autorelease];  
    11.           
    12.     }   
    13.     return view;  
    14. }  
    -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
    { 
        MKOverlayView *view=nil;
        if ([overlay isKindOfClass:[MKCircle class]]) {
            MKCircleView *cirView =[[MKCircleView alloc] initWithCircle:overlay]; 
            cirView.fillColor=[UIColor redColor]; 
            cirView.strokeColor=[UIColor redColor]; 
            cirView.alpha=0.1;
            cirView.lineWidth=4.0;
            view=[cirView autorelease];
            
        } 
        return view;
    }
    第四步:

     显示自己位置和为地图添加标注 

        

    1. @interface POI : NSObject<MKAnnotation>  
    2. {  
    3.     CLLocationCoordinate2D coordinate;  
    4.     NSString *subtitle;  
    5.     NSString *title;   
    6.     NSString *cofeId ;   
    7.     NSString *doroname;  
    8. }  
    9.   
    10. @property (nonatomic,readonly) CLLocationCoordinate2D coordinate;  
    11. @property (nonatomic,copy) NSString *subtitle;  
    12. @property (nonatomic,copy) NSString *title;   
    13. @property (nonatomic,copy) NSString *xId ;    
    14. @property (nonatomic,copy) NSString *name; ;    
    15.   
    16.   
    17. -(id) initWithCoords:(CLLocationCoordinate2D) coords;  
    @interface POI : NSObject<MKAnnotation>
    {
        CLLocationCoordinate2D coordinate;
    	NSString *subtitle;
    	NSString *title; 
        NSString *cofeId ; 
        NSString *doroname;
    }
    
    @property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
    @property (nonatomic,copy) NSString *subtitle;
    @property (nonatomic,copy) NSString *title; 
    @property (nonatomic,copy) NSString *xId ;  
    @property (nonatomic,copy) NSString *name; ;  
    
    
    -(id) initWithCoords:(CLLocationCoordinate2D) coords;

    1. @implementation POI  
    2.    
    3. @synthesize coordinate,subtitle,title;  
    4. @synthesize xId;  
    5. @synthesize doroname;  
    6.   
    7. - (id) initWithCoords:(CLLocationCoordinate2D) coords{  
    8.       
    9.     self = [super init];  
    10.       
    11.     if (self) {  
    12.           
    13.         coordinate = coords;   
    14.     }  
    15.       
    16.     return self;  
    17.       
    18. }  
    19.    
    20. - (void) dealloc  
    21.   
    22. {  
    23.     [title release];  
    24.     [subtitle release];   
    25.     [xId release];   
    26.     [doroname release];  
    27.     [super dealloc];  
    28. }  
    29.   
    30. @end  
    @implementation POI
     
    @synthesize coordinate,subtitle,title;
    @synthesize xId;
    @synthesize doroname;
    
    - (id) initWithCoords:(CLLocationCoordinate2D) coords{
    	
    	self = [super init];
    	
    	if (self) {
    		
    		coordinate = coords; 
    	}
    	
    	return self;
    	
    }
     
    - (void) dealloc
    
    {
        [title release];
    	[subtitle release]; 
        [xId release]; 
        [doroname release];
    	[super dealloc];
    }
    
    @end
     这个网上很多, 可以根据需求自己适当修改 。 
    1. doors=[jsonDic objectForKey:@"roomshops"];      
    2.    for (NSDictionary *dic in doors) {   
    3.        CLLocationCoordinate2D  p1;     
    4.        p1.latitude= [[dic objectForKey:@"roomLng"] doubleValue];   
    5.        p1.longitude=[[dic objectForKey:@"roomLat"] doubleValue];  
    6.        POI *poi = [[[POI alloc] initWithCoords:p1] autorelease];    
    7.        poi.title=[dic objectForKey:@"roomName"];  
    8.        poi.subtitle=[dic objectForKey:@"roomAddress"];    
    9.        poi.xId= [dic objectForKey:@"roomid"];   
    10.        poi.doroname=[dic objectForKey:@"room"];  
    11.        [mapView addAnnotation:poi];  
    12.    }   
         doors=[jsonDic objectForKey:@"roomshops"];    
            for (NSDictionary *dic in doors) { 
                CLLocationCoordinate2D  p1;   
                p1.latitude= [[dic objectForKey:@"roomLng"] doubleValue]; 
                p1.longitude=[[dic objectForKey:@"roomLat"] doubleValue];
                POI *poi = [[[POI alloc] initWithCoords:p1] autorelease];  
                poi.title=[dic objectForKey:@"roomName"];
                poi.subtitle=[dic objectForKey:@"roomAddress"];  
                poi.xId= [dic objectForKey:@"roomid"]; 
                poi.doroname=[dic objectForKey:@"room"];
                [mapView addAnnotation:poi];
            } 
    

      解析json。

    这里要说明以下, google地图先纬度,再经度。 而一般我们都是先经度,再纬度。  

    地图标注 :

     

    1. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation   
    2.  {   
    3.        
    4.         /// 判断是否是自己   
    5.      if ([annotation isKindOfClass:[POI class]]) {  
    6.          MKAnnotationView *view = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[annotation title] ];   
    7.          if (view==nil) {  
    8.              view= [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]] autorelease];   
    9.          }   
    10.          else  
    11.          {  
    12.              view.annotation=annotation;  
    13.          }   
    14.          POI *pview= annotation;    
    15.          if ([pview.doroname isEqual:@"你大爷"]) {  
    16.              [view setImage:[UIImage imageNamed:@"poi.png"]];   
    17.              view.canShowCallout=YES;   
    18.              UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];  
    19.              view.rightCalloutAccessoryView=btn;  
    20.          }    
    21.          else  
    22.          {  
    23.              [view setImage:[UIImage imageNamed:@"大小_选中.png"]];   
    24.              view.canShowCallout=YES;   
    25.              UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];  
    26.              view.rightCalloutAccessoryView=btn;  
    27.          }  
    28.          return  view;  
    29.      }   
    30.      else  
    31.      {   
    32.          
    33.           POI *Mapannotation = annotation;   
    34.          Mapannotation.title=@"当前位置";   
    35.          return nil;    
    36.      }  
    37.  }  
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
     { 
         
            /// 判断是否是自己
         if ([annotation isKindOfClass:[POI class]]) {
             MKAnnotationView *view = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[annotation title] ]; 
             if (view==nil) {
                 view= [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]] autorelease]; 
             } 
             else
             {
                 view.annotation=annotation;
             } 
             POI *pview= annotation;  
             if ([pview.doroname isEqual:@"你大爷"]) {
                 [view setImage:[UIImage imageNamed:@"poi.png"]]; 
                 view.canShowCallout=YES; 
                 UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                 view.rightCalloutAccessoryView=btn;
             }  
             else
             {
                 [view setImage:[UIImage imageNamed:@"大小_选中.png"]]; 
                 view.canShowCallout=YES; 
                 UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                 view.rightCalloutAccessoryView=btn;
             }
             return  view;
         } 
         else
         { 
           
              POI *Mapannotation = annotation; 
             Mapannotation.title=@"当前位置"; 
             return nil;  
         }
     }
    


     截图说明,因为我圈是加在中间的, 所以会这样。 

  • 相关阅读:
    matplotlib的使用——scatter散点图的绘制
    OpenCVPython系列之立体图像的深度图
    YOLOv3 cfg文件详解
    Opencv的使用小教程2——Opencv常用图像处理函数汇总
    数字世界中的纸张——理解 PDF
    go channel初步
    Unity学习记录 导航
    elasticsearch的keyword与text的区别
    markdownitcontainer
    Windows 编译opensll
  • 原文地址:https://www.cnblogs.com/DamonTang/p/2588347.html
Copyright © 2011-2022 走看看