我有这个代码在地图上显示4个点,带有自定义图像。我已经开始了解,也许我构建它的方式不是最优的,但它就是我所拥有的。
请帮助我了解当有人点击它时,如何使用自定义poi将我创建的标题显示在地图上。
我有两个可以使用视图的类。这些类是MapAnnotation和LocationsViewController。
以下是每个代码。
MapAnnotation.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject <MKAnnotation, MKMapViewDelegate>
{
CLLocationCoordinate2D _coordinate;
NSString * title;
}
@property (nonatomic, copy) NSString *title;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end
MapAnnotation.m
#import "MapAnnotation.h"
#import "LocationsViewController.h"
@implementation MapAnnotation
@synthesize coordinate = _coordinate;
@synthesize title;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self != nil)
{
_coordinate = coordinate;
}
return self;
}
@end
LocationsViewController.h
#import "TechBookAppDelegate.h"
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface LocationsViewController : UIViewController <MKMapViewDelegate, MKAnnotation>
{
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation;
@end
LocationsViewController.m
#import "LocationsViewController.h"
#import "MapAnnotation.h"
@interface LocationsViewController ()
@end
@implementation LocationsViewController
@synthesize coordinate = _coordinate;
@synthesize title;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code
}
return self;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
annotationView.image = [UIImage imageNamed:@"app_icon_sm.png"];
annotationView.annotation = annotation;
return annotationView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
worldView.showsUserLocation = NO;
CLLocationCoordinate2D wisconsin = {.latitude = 45.620775, .longitude = -89.47844};
CLLocationCoordinate2D pennsyvainia = {.latitude = 39.912417, .longitude = -77.663268};
CLLocationCoordinate2D nevada = {.latitude = 36.054362, .longitude = -115.020470};
CLLocationCoordinate2D texas = {.latitude = 32.555038, .longitude = -94.295592};
MapAnnotation *pinWI = [[MapAnnotation alloc] initWithCoordinate: wisconsin] ;
MapAnnotation *pinPA = [[MapAnnotation alloc] initWithCoordinate: pennsyvainia];
MapAnnotation *pinNV = [[MapAnnotation alloc] initWithCoordinate: nevada];
MapAnnotation *pinTX = [[MapAnnotation alloc] initWithCoordinate: texas];
//UIImage *star = [UIImage imageNamed:@"locations_marker.png"];
//////////////////
pinWI.title = @"Rhinelander, WI";///////
pinPA.title = @"Chambersburg, PA";//////I dont understand why these do not output a
pinNV.title = @"Henderson, NV";/////////title if the title property is a part of the
pinTX.title = @"Marshall, TX";////////// MKAnnotation
//////////////////
[worldView addAnnotation:pinTX];
[worldView addAnnotation:pinNV];
[worldView addAnnotation:pinWI];
[worldView addAnnotation:pinPA];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我想我需要一个函数来创建标题?我真的希望我不必创建更多类来完成这项工作。
答案 0 :(得分:1)
您的主要问题是:
当有人点击时,如何使我创建的标题显示在地图上的自定义poi。
在viewForAnnotation
委托方法中,您需要将注释视图的canShowCallout
属性设置为YES
(默认值为NO
):
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation...
annotationView.canShowCallout = YES; // <-- add this line
}
一些与问题无关的其他评论:
在MapAnnotation.h中,此类只会实现MKAnnotation
,而不是MKMapViewDelegate
,因此请将其从协议列表中删除,以避免潜在的编译器警告并使代码更清晰,更清晰:
@interface MapAnnotation : NSObject <MKAnnotation>
在MapAnnotation.m中,您不需要#import "LocationsViewController.h"
。删除它。
在LocationsViewController.h中,此类不正在实现MKAnnotation
,因此将其从协议列表中删除,以避免潜在的编译器警告并使代码更清晰,更清晰:
@interface LocationsViewController : UIViewController <MKMapViewDelegate>
同样在LocationsViewController.h中,没有必要声明您将要实现的委托方法。您可以删除viewForAnnotation
声明。
在LocationsViewController.m中,@synthesize
和coordinate
的{{1}}不属于此处。删除它们。
答案 1 :(得分:0)
-(void)locatePlaceonMap:(NSDictionary*)dic
{
[map_bar setScrollEnabled:YES];
map_bar.delegate = self;
MKCoordinateRegion zoomIn = map_bar.region;
zoomIn.span.latitudeDelta = 0.1f;
zoomIn.span.longitudeDelta = 0.1f;
zoomIn.center.latitude = ApplicationDelegate.latitude;
zoomIn.center.longitude = ApplicationDelegate.longitude;
[map_bar setRegion:zoomIn animated:NO];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.title = [dic valueForKey:@"vName"];
annotation.subtitle = [dic valueForKey:@"t_long_address"];
CLLocationCoordinate2D newlocation = [map_bar.userLocation coordinate];
newlocation.latitude=[[dic valueForKey:@"dLat"]doubleValue];
newlocation.longitude=[[dic valueForKey:@"dLong"]doubleValue];
annotation.coordinate = newlocation;
[map_bar addAnnotation:annotation];
}
#pragma mark MkMapViewDelegate Methods
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = nil;
static NSString *defaultPinID = @"ReusedPin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
if (annotation == mapView.userLocation) {
return nil;
}else{
pinView.image = [UIImage imageNamed:@"pin_trophy.png"];
pinView.pinColor = MKPinAnnotationColorRed;
}
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
return pinView;
}