2012-01-29 4 views
8

जोड़ेऔंधा चक्र ओवरले दृश्य मैप करने के लिए

मैं यहां दिए गए निर्देशों का पालन किया है (का उपयोग करते हुए, iOS 5 और Xcode 4.2।): http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6-SW15 और मेरे MKMapView पर एक चक्र ओवरले जोड़ने के लिए MKCircle और MKCircleView कक्षाओं का इस्तेमाल किया।

हालांकि क्या मैं वास्तव में चाहते हैं, तो उल्टा चक्र ओवरले है नीचे स्केच में बाईं मानचित्र की तरह (वर्तमान में मैं सही पर एक की तरह एक सर्कल ओवरले हैं):

Inverted and not-inverted circle overlay sketches

औंधा के लिए सर्कल, ओवरले को पूरे मानचित्र को कवर करना चाहिए - दृश्य मंडल के अलावा।

क्या एमकेसीर्कल/एमकेसीर्कल व्यू कक्षाओं का उपयोग करके इसे पूरा करने का कोई आसान तरीका है? या मुझे गहराई से जाना होगा और कस्टम ओवरले ऑब्जेक्ट/व्यू को परिभाषित करना होगा?

आपकी मदद :)

उत्तर

5

यह करने के लिए सबसे अच्छा तरीका है के लिए धन्यवाद, MKMapView उपवर्ग और drawRect विधि कॉल सुपर ओवरराइड, तो रंग आप चाहते हैं के साथ नक्शे पर पेंट करने के लिए किया जाएगा। फिर प्रत्येक बार जब उपयोगकर्ता चलता है, drawRect उचित ड्राइंग द्वारा जवाब देना चाहिए।

+0

क्या आप कृपया सुझाव दे सकते हैं कि आपने drawRect विधि में कैसे लिखा या आपने क्या लिखा? – HarshIT

+0

पूरे मानचित्र पर चित्रकारी क्षेत्र के बीच भी बना देगा, लेकिन वह इसे नहीं चाहता है। जवाब कैसे स्वीकार किया जाता है? –

6

मैं एक ही काम था और यहाँ मैं कैसे इसे हल है:

नोट: इस कोड को केवल iOS7

से शुरू काम करेंगे आपके विचार नियंत्रक में कहीं नक्शा करने के लिए एक ओवरले जोड़ें,:

MyMapOverlay *overlay = [[MyMapOverlay alloc] initWithCoordinate:coordinate]; 
[self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels]; 

MKMapViewDelegate तरीकों में अगले लिखें:

- (MKOverlayRenderer *)mapView:(MKMapView *)map rendererForOverlay:(id<MKOverlay>)overlay { 
    /// we need to draw overlay on the map in a way when everything except the area in radius of 500 should be grayed 
    /// to do that there is special renderer implemented - NearbyMapOverlay 
    if ([overlay isKindOfClass:[NearbyMapOverlay class]]) { 
     MyMapOverlayRenderer *renderer = [[MyMapOverlayRenderer alloc] initWithOverlay:overlay]; 
     renderer.fillColor = [UIColor whateverColor];/// specify color which you want to use for gray out everything out of radius 
     renderer.diameterInMeters = 1000;/// choose whatever diameter you need 

     return renderer; 
    } 
    return nil; 
} 
,210

MyMapOverlay पीछा की तरह ही कुछ किया जाना चाहिए:

@interface MyMapOverlay : NSObject<MKOverlay> 
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate; 
@end 

@implementation MyMapOverlay 

@synthesize coordinate = _coordinate; 

- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate { 
    self = [super init]; 
    if (self) { 
     _coordinate = coordinate; 
    } 
    return self; 
} 

- (MKMapRect)boundingMapRect { 
    return MKMapRectWorld; 
} 

@end 

और MyMapOverlayRenderer:

@interface MyMapOverlayRenderer : MKOverlayRenderer 
@property (nonatomic, assign) double diameterInMeters; 
@property (nonatomic, copy) UIColor *fillColor; 
@end 

@implementation MyMapOverlayRenderer 

/// this method is called as a part of rendering the map, and it draws the overlay polygon by polygon 
/// which means that it renders overlay by square pieces 
- (void)drawMapRect:(MKMapRect)mapRect 
     zoomScale:(MKZoomScale)zoomScale 
     inContext:(CGContextRef)context { 

    /// main path - whole area 
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(mapRect.origin.x, mapRect.origin.y, mapRect.size.width, mapRect.size.height)]; 

    /// converting to the 'world' coordinates 
    double radiusInMapPoints = self.diameterInMeters * MKMapPointsPerMeterAtLatitude(self.overlay.coordinate.latitude); 
    MKMapSize radiusSquared = {radiusInMapPoints, radiusInMapPoints}; 
    MKMapPoint regionOrigin = MKMapPointForCoordinate(self.overlay.coordinate); 
    MKMapRect regionRect = (MKMapRect){regionOrigin, radiusSquared}; //origin is the top-left corner 
    regionRect = MKMapRectOffset(regionRect, -radiusInMapPoints/2, -radiusInMapPoints/2); 
    // clamp the rect to be within the world 
    regionRect = MKMapRectIntersection(regionRect, MKMapRectWorld); 

    /// next path is used for excluding the area within the specific radius from current user location, so it will not be filled by overlay fill color 
    UIBezierPath *excludePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(regionRect.origin.x, regionRect.origin.y, regionRect.size.width, regionRect.size.height) cornerRadius:regionRect.size.width/2]; 
    [path appendPath:excludePath]; 

    /// setting overlay fill color 
    CGContextSetFillColorWithColor(context, self.fillColor.CGColor); 
    /// adding main path. NOTE that exclusionPath was appended to main path, so we should only add 'path' 
    CGContextAddPath(context, path.CGPath); 
    /// tells the context to fill the path but with regards to even odd rule 
    CGContextEOFillPath(context); 
} 

एक परिणाम आप बाईं छवि है कि प्रश्न में पोस्ट किया गया था पर जैसे ठीक उसी दृश्य होगा के रूप में ।

संबंधित मुद्दे