JXMapView.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // MapView.m
  3. //
  4. //
  5. // Created by Jian-Ye on 12-10-16.
  6. // Copyright (c) 2012年 Jian-Ye. All rights reserved.
  7. //
  8. #import "JXMapView.h"
  9. #import "CallOutAnnotationView.h"
  10. #import "CalloutMapAnnotation.h"
  11. #import "BasicMapAnnotation.h"
  12. @interface JXMapView ()<MKMapViewDelegate,CallOutAnnotationViewDelegate>
  13. @property (nonatomic,strong)id<JXMapViewDelegate> delegate;
  14. @property (nonatomic,strong)CalloutMapAnnotation *calloutAnnotation;
  15. @end
  16. @implementation JXMapView
  17. @synthesize mapView = _mapView;
  18. @synthesize delegate = _delegate;
  19. - (id)init
  20. {
  21. if (self = [super init]) {
  22. self.backgroundColor = [UIColor clearColor];
  23. MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.bounds];
  24. mapView.delegate = self;
  25. [self addSubview:mapView];
  26. self.mapView = mapView;
  27. self.span = 40000;
  28. }
  29. return self;
  30. }
  31. - (id)initWithDelegate:(id<JXMapViewDelegate>)delegate
  32. {
  33. if (self = [self init]) {
  34. self.delegate = delegate;
  35. }
  36. return self;
  37. }
  38. - (void)setFrame:(CGRect)frame
  39. {
  40. self.mapView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
  41. [super setFrame:frame];
  42. }
  43. - (void)beginLoad
  44. {
  45. for (int i = 0; i < [_delegate numbersWithCalloutViewForMapView]; i++) {
  46. CLLocationCoordinate2D location = [_delegate coordinateForMapViewWithIndex:i];
  47. MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location,_span ,_span );
  48. MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:region];
  49. [_mapView setRegion:adjustedRegion animated:YES];
  50. BasicMapAnnotation * annotation=[[BasicMapAnnotation alloc] initWithLatitude:location.latitude andLongitude:location.longitude tag:i];
  51. [_mapView addAnnotation:annotation];
  52. }
  53. }
  54. - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
  55. {
  56. if ([view.annotation isKindOfClass:[BasicMapAnnotation class]]) {
  57. BasicMapAnnotation *annotation = (BasicMapAnnotation *)view.annotation;
  58. if (_calloutAnnotation.coordinate.latitude == annotation.latitude&&
  59. _calloutAnnotation.coordinate.longitude == annotation.longitude)
  60. {
  61. return;
  62. }
  63. if (_calloutAnnotation) {
  64. [mapView removeAnnotation:_calloutAnnotation];
  65. self.calloutAnnotation = nil;
  66. }
  67. self.calloutAnnotation = [[CalloutMapAnnotation alloc]
  68. initWithLatitude:annotation.latitude
  69. andLongitude:annotation.longitude
  70. tag:annotation.tag];
  71. [mapView addAnnotation:_calloutAnnotation];
  72. [mapView setCenterCoordinate:_calloutAnnotation.coordinate animated:YES];
  73. }
  74. }
  75. - (void)didSelectAnnotationView:(CallOutAnnotationView *)view
  76. {
  77. CalloutMapAnnotation *annotation = (CalloutMapAnnotation *)view.annotation;
  78. if([_delegate respondsToSelector:@selector(calloutViewDidSelectedWithIndex:)])
  79. {
  80. [_delegate calloutViewDidSelectedWithIndex:annotation.tag];
  81. }
  82. [self mapView:_mapView didDeselectAnnotationView:view];
  83. }
  84. - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
  85. {
  86. if (_calloutAnnotation)
  87. {
  88. if (_calloutAnnotation.coordinate.latitude == view.annotation.coordinate.latitude&&
  89. _calloutAnnotation.coordinate.longitude == view.annotation.coordinate.longitude)
  90. {
  91. [mapView removeAnnotation:_calloutAnnotation];
  92. self.calloutAnnotation = nil;
  93. }
  94. }
  95. }
  96. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
  97. {
  98. if ([annotation isKindOfClass:[CalloutMapAnnotation class]])
  99. {
  100. CalloutMapAnnotation *calloutAnnotation = (CalloutMapAnnotation *)annotation;
  101. CallOutAnnotationView *annotationView = (CallOutAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutView"];
  102. if (!annotationView)
  103. {
  104. annotationView = [[CallOutAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CalloutView" delegate:self];
  105. }
  106. for (UIView *view in annotationView.contentView.subviews) {
  107. [view removeFromSuperview];
  108. }
  109. [annotationView.contentView addSubview:[_delegate mapViewCalloutContentViewWithIndex:calloutAnnotation.tag]];
  110. return annotationView;
  111. } else if ([annotation isKindOfClass:[BasicMapAnnotation class]])
  112. {
  113. BasicMapAnnotation *basicMapAnnotation = (BasicMapAnnotation *)annotation;
  114. MKAnnotationView *annotationView =[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];
  115. if (!annotationView)
  116. {
  117. annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
  118. reuseIdentifier:@"CustomAnnotation"];
  119. annotationView.canShowCallout = NO;
  120. annotationView.image = [_delegate baseMKAnnotationViewImageWithIndex:basicMapAnnotation.tag];
  121. }
  122. return annotationView;
  123. }
  124. return nil;
  125. }
  126. @end