IOS開發-地圖 (mapkit)實驗

weixin_34119545發表於2012-07-12

 

  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;  
     }
 }


 

 截圖說明,因為我圈是加在中間的, 所以會這樣。 

 

 

相關文章