2011-11-25 14 views
5

मैं आईओएस 4 के साथ आईफोन पर मैपकिट का उपयोग कर रहा हूं। मैं मानचित्र पर आकृतियों को आकर्षित करने के लिए कस्टम ओवरले और कस्टम ओवरले व्यू का उपयोग कर रहा हूं। फिलहाल, आकार केवल आयताकार हैं, लेकिन मैं कुछ और परिष्कृत योजना बना रहा हूं। यही कारण है कि मैं एमके पोलिगॉन ओवरले प्रकार का उपयोग नहीं कर रहा हूं। यह मेरा ओवरले दृश्य ड्राइंग विधि के लिए कोड है:मैपकिट ओवरले व्यू पर पथ को स्ट्रोक नहीं कर सकता

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context 
{ 
    // Clip context to bounding rectangle 
    MKMapRect boundingMapRect = [[self overlay] boundingMapRect]; 
    CGRect boundingRect = [self rectForMapRect:boundingMapRect]; 
    CGContextAddRect(context, boundingRect); 
    CGContextClip(context); 

    // Define shape 
    CGRect shapeRect = CGRectMake(0.5f, 0.5f, boundingRect.size.width - 1.0f, boundingRect.size.height - 1.0f); 

    // Fill 
    CGContextSetRGBFillColor(context, 0.5f, 0.5f, 0.5f, 0.5f); 
    CGContextFillRect(context, shapeRect); 

    // Stroke 
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.75f); 
    CGContextSetLineWidth(context, 1.0f); 
    CGContextStrokeRect(context, shapeRect); 
} 

समस्या यह है कि आयतों ठीक से जमा प्राप्त है (इसलिए यह उनके सीमांकन रेक्ट ठीक से सेट है प्रकट होता है), लेकिन वे stroked नहीं मिलता। क्या कोई मदद कर सकता है? धन्यवाद!

+0

जबकि मुझे समाधान नहीं पता है, मैं बस आपका ध्यान [सीजीआरईटीनेट] (http://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CGGeometry/Reference/reference पर आकर्षित करना चाहता था। एचटीएमएल # // apple_ref/c/func/CGRectInset) ('सीजीआरईटीएमके' के साथ आयताकार बनाने के बजाय)। – DarkDust

+0

बस मेरे लिए हुआ: क्या आयत आगे बढ़ने पर आयताकार स्ट्रोक हो जाता है? तो सबसे पहले आप इसे भरें, फिर आप रेक्ट को किसी अन्य 0.5 से चिपकाएं और फिर इसे स्ट्रोक करें? – DarkDust

+0

इससे संबंधित एक और प्रश्न मिला: http://stackoverflow.com/questions/5274164/custom-mkoverlayview-line-width ऐसा लगता है कि समस्या लाइन चौड़ाई स्केलिंग के साथ है, और ऐसा लगता है कि मुझे इस तरह अपना कोड ठीक करना है : CGContextSetLineWidth (संदर्भ, 5.0 एफ/ज़ूमस्केल); –

उत्तर

2

जैसा कि कुछ पिछली टिप्पणियों में बताया गया है, समस्या लाइन चौड़ाई के साथ है। अधिक आम तौर पर, सभी ड्राइंग स्वचालित रूप से मानचित्र ज़ूमिंग का पालन करने के लिए स्केल किए जाते हैं, इसलिए यदि आप अपने कुछ ड्राइंग मीट्रिक ज़ूम-स्वतंत्र होने के लिए चाहते हैं, तो आपको इसे ज़ूमस्केल द्वारा विभाजित करना होगा।

यहाँ नए कोड, कि मेरे iPhone 4 पर सही ढंग से काम करता है:

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context 
{ 
    // Clip context to bounding rectangle 
    MKMapRect boundingMapRect = [[self overlay] boundingMapRect]; 
    CGRect boundingRect = [self rectForMapRect:boundingMapRect]; 
    CGContextAddRect(context, boundingRect); 
    CGContextClip(context); 

    // Define shape 
    CGRect shapeRect = CGRectInset(boundingRect, 2.0f/zoomScale, 2.0f/zoomScale); 

    // Fill 
    CGContextSetRGBFillColor(context, 0.5f, 0.5f, 0.5f, 0.5f); 
    CGContextFillRect(context, shapeRect); 

    // Stroke 
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.75f); 
    CGContextSetLineWidth(context, 4.0f/zoomScale); 
    CGContextStrokeRect(context, shapeRect); 
} 

मैं भी है, क्योंकि मुझे लगता है कि यह कर सकते हैं कोड मैं ओवरले में उपयोग कर रहा हूँ की गणना और सीमांकन आयत वापस जाने के लिए रिपोर्ट करेंगे सहायता:

-(MKMapRect)boundingMapRect 
{ 
    // Overlay bounds 
    CLLocationCoordinate2D topLeftcoordinate = <the top-left coordinate of overlay>; 
    CLLocationCoordinate2D bottomRightCoordinate = <the bottom-right coordinate of overlay>; 

    // Convert them to map points 
    MKMapPoint topLeftPoint = MKMapPointForCoordinate(topLeftcoordinate); 
    MKMapPoint bottomRightPoint = MKMapPointForCoordinate(bottomRightCoordinate); 

    // Calculate map rect 
    return MKMapRectMake(topLeftPoint.x, topLeftPoint.y, bottomRightPoint.x - topLeftPoint.x, topLeftPoint.y - bottomRightPoint.y); 
} 

आपकी टिप्पणियों और सुझावों के लिए सभी का धन्यवाद।

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