IOS开发-地图 (mapkit)实验

来源:互联网 发布:erp软件是什么 编辑:程序博客网 时间:2024/04/27 04:49
经过一个星期,翻阅各种资料。终于达到基本要求。  记录一下, 以后备用。 

  IOS 地图控件 : mapkit  

  第一步 

      显示地图

[cpp] view plaincopyprint?
  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. }   

也可以直接托控件。 直接运行,OK . 没问题。   

第二步: 

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

[cpp] view plaincopyprint?
  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];      

 如果定位成功 : 

 

[cpp] view plaincopyprint?
  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.  }   
  这样便可以定自己当前位置。    

/***

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

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

 得到每个对象经纬度

 **/   

第三步: 

  在地图上画圈

      首先在 头文件定义 :

[cpp] view plaincopyprint?
  1. @property(nonatomic,retain)MKCircle *circle;  

  圈的颜色,属性

[cpp] view plaincopyprint?
  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. }  
第四步: 

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

    

[cpp] view plaincopyprint?
  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;  

[cpp] view plaincopyprint?
  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  
 这个网上很多, 可以根据需求自己适当修改 。  

 

[cpp] view plaincopyprint?
  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.    }   

  解析json。 

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

地图标注 :

 

[cpp] view plaincopyprint?
  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.  }  


 截图说明,因为我两点相差太远, 所以会这样。

原创粉丝点击