2011-06-27 19 views
15

क्या MKAnnotation के लिए एक साधारण उदाहरण परियोजना है? मुझे नहीं पता कि "addAnnotation" पर क्या पास करना है, क्योंकि यह कुछ "id<Annotation>" चाहता है। डेवलपर साइट पर उदाहरण सभी सरणी या पार्सर्स के साथ हैं या तो, मुझे समझने के लिए बस एक बहुत ही आसान उदाहरण की आवश्यकता है, पूरी बात कैसे काम करती है।एमकेएएनोटेशन, सरल उदाहरण

अग्रिम धन्यवाद ..

संपादित करें:

मैं एक वर्ग Pin की स्थापना की। .h में यह लिखा गया है कि

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface Pin : NSObject <MKAnnotation> { 
CLLocationCoordinate2D coordinate; 
NSString *title; 
NSString *subTitle; 
} 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readonly) NSString *title; 
@property (nonatomic, readonly) NSString *subTitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description; 
@end 

क्याnownow उत्तर के अनुसार लिखता है।

Pin.m में, मैं इस तरह लागू किया, विभिन्न उदाहरणों के अनुसार:

#import "Pin.h" 

@implementation Pin 

@synthesize title, subTitle; 

- (CLLocationCoordinate2D) coordinate { 
CLLocationCoordinate2D coords; 
coords.latitude = coordinate.latitude; //i want it that way 
coords.longitude = 7.1352260; //this way it works 

return coords; 

- (NSString *) title { 
return @"Thats the title"; 
} 

- (NSString *) subtitle { 
return @"Thats the sub"; 
} 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description { 

return self; 
} 

- (void) dealloc { 
[super dealloc]; 
} 

@end 

तो अगर मैं हाथ से निर्देशांक सेट, टिप्पणी में बताया है, यह काम करता है। लेकिन मैं पहली टिप्पणी में मूल्य को गतिशील रूप से सेट करने में सक्षम होना चाहता हूं। मैंने विभिन्न तरीकों की कोशिश की, लेकिन कोई भी काम नहीं किया। जैसा कि आप - (CLLocationCoordinate2D) coordinate निर्दिष्ट नहीं करते हैं, मैंने coordinate को संश्लेषित करने का प्रयास किया है, लेकिन ऐसा लगता है कि यह या तो काम नहीं करेगा।

क्या आप मुझे अपना MapPin.m दिखा सकते हैं?

संपादित करें 2:

मैं mapCenter तरह

- (void)viewWillAppear:(BOOL)animated { 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
self.locationManager.delegate = self; 
[self.locationManager startUpdatingLocation]; 

CLLocation *curPos = locationManager.location; 
location = curPos.coordinate; 

CLLocationCoordinate2D mapCenter; 
mapCenter.latitude = location.latitude; 
mapCenter.longitude = location.longitude; 

MKCoordinateSpan mapSpan; 
mapSpan.latitudeDelta = 0.005; 
mapSpan.longitudeDelta = 0.005; 

MKCoordinateRegion mapRegion; 
mapRegion.center = mapCenter; 
mapRegion.span = mapSpan; 

mapKitView.region = mapRegion; 
mapKitView.mapType = MKMapTypeStandard; 
mapKitView.showsUserLocation=TRUE; 
} 

कोई भी चीज जो के साथ गलत सेट?

उत्तर

34

आपको MKAnnotation प्रोटोकॉल को लागू करने वाली कक्षा होने की आवश्यकता है।

@interface MapPin : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
} 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readonly) NSString *title; 
@property (nonatomic, readonly) NSString *subtitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description; 

@end 

फिर, जहां नक्शा बनाने आपको कॉल करेंगे:: यहाँ अपनी परियोजनाओं में से एक से एक टुकड़ा है

pin = [[MapPin alloc] initWithCoordinates:[track startCoordinates] placeName:@"Start" description:@""]; 
[map addAnnotation:pin]; 

संपादित करें:

यहाँ दिया गया है:

@implementation MapPin 

@synthesize coordinate; 
@synthesize title; 
@synthesize subtitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description { 
    self = [super init]; 
    if (self != nil) { 
     coordinate = location; 
     title = placeName; 
     [title retain]; 
     subtitle = description; 
     [subtitle retain]; 
    } 
    return self; 
} 

- (void)dealloc { 
    [title release]; 
    [subtitle release]; 
    [super dealloc]; 
} 


@end 

उम्मीद है कि यह मदद करता है, ओनो।

+0

धन्यवाद, जो पहले से ही काफी मदद करता है। इस तरह मैं नया हूं कि Google के लिए क्या करना है, और इसे चलाने के लिए मिला, लेकिन मुझे काफी कुछ नहीं मिला। मेरे पास अब तक के कोड के लिए मेरा अद्यतन प्रश्न देखें। मुझे नहीं पता कि एमएम कैसा दिखना चाहिए .. – Nareille

+0

मेरा संपादित उत्तर देखें। – onnoweb

+0

धन्यवाद, जिसने फिर से मदद की :) कुछ अजीब व्यवहार के लिए एक आखिरी सवाल: मैं अपने .h में "समन्वय" जैसी कुछ परिभाषित नहीं करता हूं, लेकिन एक्सकोड मुझे बताता है कि मुझे इसे संश्लेषित करने की आवश्यकता है। यदि मैं ऐसा करता हूं, तो मेरे मैप सेंटर को कुछ सौ मीटर पश्चिम में स्थानांतरित किया जाता है। अगर मैं संश्लेषण को समाप्त करता हूं, तो यह फिर से उचित रहता है। चेतावनी से छुटकारा पाने के लिए कैसे? – Nareille

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