IOS开发-地图 (mapkit)实验

 
 经过一个星期,翻阅各种资料。终于达到基本要求。  记录一下, 以后备用。

  IOS 地图控件 : mapkit 

  第一步

      显示地图

- (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 . 没问题。  

第二步:

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

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

 如果定位成功 :

 

-(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 。

 得到每个对象经纬度

 **/  

第三步:

  在地图上画圈

      首先在 头文件定义 :

 @property(nonatomic,retain)MKCircle *circle;

  圈的颜色,属性

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)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;
}
第四步:

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

    

@interface POI : NSObject
{
    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;

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

 

     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地图先纬度,再经度。 而一般我们都是先经度,再纬度。  

地图标注 :

 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )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;  
     }
 }


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





 

 

你可能感兴趣的:(mapkit,ios,cllocation,manager,json,interface)