我似乎无法在地图视图中弹出我的注释。我有一个来自plist的大量注释,除了一个之外,他们没有出现在地图上。请帮忙吗?
map viewcontroller.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface Map_ViewController : UIViewController
<CLLocationManagerDelegate, MKMapViewDelegate >
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
映射ViewController.m
#import "Map ViewController.h"
#import "MADAnnotation.h"
@interface Map_ViewController ()
@end
#define CHARMANDER_LATITUDE 40.0078429;
#define CHARMANDER_LONGITUDE -105.26966709999999;
#define CHARMELEON_LATITUDE 40.0104104;
#define CHARMELEON_LONGITUDE -105.28965619999997;
#define CHARIZARD_LATITUDE 40.0064377;
#define CHARIZARD_LONGITUDE -105.27301119999998;
#define BULBASAUR_LATITUDE 40.0096216;
#define BULBASAUR_LONGITUDE -105.27282120000001;
#define IVYSAUR_LATITUDE 40.0108876;
#define IVYSAUR_LONGITUDE -105.27389820000002;
#define VENUSAUR_LATITUDE 40.0079411;
#define VENUSAUR_LONGITUDE -105.27507350000002;
#define SQUIRTLE_LATITUDE 40.0043601;
#define SQUIRTLE_LONGITUDE -105.26956860000001;
#define WARTORTLE_LATITUDE 40.0104104;
#define WARTORTLE_LONGITUDE -105.26633140000001;
#define BLASTOISE_LATITUDE 40.0104961;
#define BLASTOISE_LONGITUDE -105.26594160000002;
#define THE_SPAN 0.20f;
@implementation Map_ViewController
{
CLLocationManager *locationManager;
MADAnnotation *annotation;
}
@synthesize mapView;
- (void)viewDidLoad {
mapView.delegate=self;
mapView.mapType=MKMapTypeHybrid;
locationManager=[[CLLocationManager alloc] init]; locationManager.delegate=self; locationManager.desiredAccuracy=kCLLocationAccuracyBest; locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
[super viewDidLoad];
// Create the region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = BULBASAUR_LATITUDE;
center.longitude = BULBASAUR_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span=span;
//Set our mapView
[mapView setRegion:myRegion animated:YES];
NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PokeLocations" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *anns = [dict objectForKey:@"PokeLocations"];
NSLog(@"read1");
for(int i = 0; i < [anns count]; i++) {
NSLog(@"read2");
float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];
NSLog(@"read3");
MADAnnotation *myAnnotation = [[MADAnnotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Subtitle"];
[mapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
//[myAnnotation release];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
MKCoordinateSpan span;
span.latitudeDelta=.001;
span.longitudeDelta=.001;
MKCoordinateRegion region;
region.center= manager.location.coordinate;
region.span=span;
[mapView setRegion:region animated:YES];
if(annotation){
[annotation moveAnnotation:manager.location.coordinate];
}
else {
annotation=[[MADAnnotation alloc] initWithCoordinate:manager.location.coordinate];
[mapView addAnnotation:annotation]; }
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSString *errorType;
if (error.code == kCLErrorDenied) {
errorType=@"Access Denied";
} else errorType=@"Error";
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error"
message:errorType delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation1
{
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation1 reuseIdentifier:AnnotationViewID];
}
//Custom Pin
annotationView.image = [UIImage imageNamed:@"pokeballsmall2.png"];
//Custom Thumbnail (left side)
UIImageView *IconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pokeballsmall.png"]];
annotationView.leftCalloutAccessoryView = IconView;
annotationView.canShowCallout = YES;
annotationView.annotation = annotation1;
return annotationView;
}
@end
MADAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MADAnnotation : NSObject <MKAnnotation>
@property (nonatomic, readonly)CLLocationCoordinate2D coordinate;
@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *subtitle;
@property (nonatomic, copy) UIImageView * leftCalloutAccessoryView;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coor;
-(void) moveAnnotation:(CLLocationCoordinate2D)newCoordinate;
@end
MADAnnotation.m
#import "MADAnnotation.h"
@implementation MADAnnotation
@synthesize coordinate, title, subtitle, leftCalloutAccessoryView;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coor{ coordinate=coor;
title=@"You are here";
subtitle=[NSString stringWithFormat:@"Latitude: %f. Longitude: %f", coordinate.latitude, coordinate.longitude]; return self;
}
-(void) moveAnnotation:(CLLocationCoordinate2D)newCoordinate{
[self willChangeValueForKey:@"coordinate"]; [self willChangeValueForKey:@"subtitle"]; coordinate = newCoordinate;
subtitle = [[NSString alloc] initWithFormat:@"Latitude: %f. Longitude: %f", coordinate.latitude, coordinate.longitude];
[self didChangeValueForKey:@"coordinate"];
[self didChangeValueForKey:@"subtitle"];
}
@end
annotations.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PokeLocations</key>
<array>
<dict>
<key>Title</key>
<string>#004 Charmander</string>
<key>Subtitle</key>
<string>Fire Type</string>
<key>Latitude</key>
<string>40.0078429</string>
<key>Longitude</key>
<string>-105.26966709999999 </string>
</dict>
<dict>
<key>Title</key>
<string>#005 Charmeleon</string>
<key>Subtitle</key>
<string>Fire Type</string>
<key>Latitude</key>
<string>40.0104104</string>
<key>Longitude</key>
<string>-105.28965619999997</string>
</dict>
<dict>
<key>Title</key>
<string>#006 Charizard</string>
<key>Subtitle</key>
<string>Fire/Flying Type</string>
<key>Latitude</key>
<string>40.0064377 </string>
<key>Longitude</key>
<string>-105.27301119999998 </string>
</dict>
<dict>
<key>Title</key>
<string>#001 Bulbasaur</string>
<key>Subtitle</key>
<string>Grass/Poison Type</string>
<key>Latitude</key>
<string>40.0096216</string>
<key>Longitude</key>
<string>-105.27282120000001 </string>
</dict>
<dict>
<key>Title</key>
<string>#002 Ivysaur</string>
<key>Subtitle</key>
<string>Grass/Poison Type</string>
<key>Latitude</key>
<string>40.0108876</string>
<key>Longitude</key>
<string>-105.27389820000002 </string>
</dict>
<dict>
<key>Title</key>
<string>#003 Venusaur</string>
<key>Subtitle</key>
<string>Grass/Poison Type</string>
<key>Latitude</key>
<string>40.0079411</string>
<key>Longitude</key>
<string>-105.27507350000002 </string>
</dict>
<dict>
<key>Title</key>
<string>#007 Squirtle</string>
<key>Subtitle</key>
<string>Water Type</string>
<key>Latitude</key>
<string>40.0043601</string>
<key>Longitude</key>
<string>-105.26956860000001 </string>
</dict>
<dict>
<key>Title</key>
<string>#008 Wartortle</string>
<key>Subtitle</key>
<string>Water Type</string>
<key>Latitude</key>
<string>40.0104961</string>
<key>Longitude</key>
<string>-105.26633140000001</string>
</dict>
<dict>
<key>Title</key>
<string>#009 Blastoise</string>
<key>Subtitle</key>
<string>Water Type</string>
<key>Latitude</key>
<string>40.0104961</string>
<key>Longitude</key>
<string>-105.26594160000002 </string>
</dict>
</array>
</dict>
</plist>
答案 0 :(得分:0)
我无法回复评论,因此我将其作为答案发布
您可以使用以下方法为注释添加属性:
-(MKAnnotationView *) mapView:(MKMapView *)mapMKMapView viewForAnnotation: (id<MKAnnotation>)annotation
{
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
MyPin.pinColor = MKPinAnnotationColorPurple; // <--for coloring purpose
UIButton *adverButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[adverButton addTarget:self action:@selector(button:)forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = adverButton;
MyPin.draggable = YES;
MyPin.highlighted = YES;
MyPin.animatesDrop = TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
-(void)button:(id)sender
{
//add your nextView you want to open here
}