0
我已經從XML文件中讀取緯度和經度,並且我想添加一些註釋。但除了用戶的位置,他們都不是mapview.annotations的added.The計數總是empty.Here是我的代碼:我無法使用緯度和經度在地圖視圖中添加註釋
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (newLocation != nil) {
self.myLocation = newLocation;
}
mapView.region = MKCoordinateRegionMake(self.myLocation.coordinate, MKCoordinateSpanMake(0.005f, 0.005f));
mapView.zoomEnabled = YES;
//mapView.showsUserLocation = YES;
mapView.centerCoordinate = self.myLocation.coordinate;
MKPointAnnotation *userLocation = [[MKPointAnnotation alloc] init];
userLocation.coordinate = myLocation.coordinate;
userLocation.title = @"my position";
userLocation.subtitle = @"my position now";
[mapView removeAnnotations:mapView.annotations];
[mapView addAnnotation:userLocation];
[userLocation release];
if ([self.positionArray count] < 5) {
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"position" ofType:@"xml"];
NSData *positionData = [[NSData alloc] initWithContentsOfFile:dataPath];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:positionData];
parser.delegate = self;
[parser parse];
}
if (self.myLocation != nil) {
[self.manager stopUpdatingLocation];
}
}
,這裏是MapInfo的類:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface MapInfo : NSObject<MKAnnotation>
{
NSString *subtitle;
NSString *title;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)location;
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
@end
然後在這裏是委託方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *AnnotationIdentifier = @"AnnotationIdentifer";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if ([annotation isKindOfClass:[MapInfo class]]) {
if (pinView == nil) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
}
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
}else if([annotation isKindOfClass:[MKPointAnnotation class]])
{
if (pinView == nil) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
}
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
}
return pinView;
}
我真的不知道爲什麼只可添加用戶的位置,有人幫我請!它已經困擾了我好幾天!