显示引脚兼容标题(注释)一次地图加载 [英] Displaying Pin with Title (annotation) once map loads
问题描述
我的工作我的第一个应用程序,并在其中我只是想有按钮单击显示用针(和标题该引脚的位置上)的地图。我能够加载的MapView,并把它显示我想要的坐标。但是,当试图显示引脚和注释我有问题。不知道从何处code这个问题以及如何使注释显示引脚。我搜索,看到很多教程,但大多数表现出不同的图形页面的风格,并显示在用户选择引脚,我想在地图上显示的负载引脚。
I am working on my first app and within it I'm just trying to have on button click show a map with a pin (and title on this pin of location). I was able to load the mapview and to have it show the coordinates I want. But when trying to display the pin and annotation I am having issues. Not sure where to code this and how to make annotation to display pin. I've searched and seen many tutorials, but most show a different mapview style and are showing pin on user selection, I want to show pin on load of map.
下面是code我必须证明这是工作的地图,但没有针显示或注释:
Here is the code I have to show the map which is working, but has no pin display or annotation:
FirstLocateViewController.m code:
FirstLocateViewController.m code:
#import "FirstLocateViewController.h"
@implementation FirstLocateViewController
@synthesize dismissViewButton;
-(IBAction)dismissView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0,0, 320,420);
mapView = [[MKMapView alloc] initWithFrame:frame];
mapView.mapType = MKMapTypeStandard;
CLLocationCoordinate2D coord = {latitude: 12.3456, longitude: -7.890};
MKCoordinateSpan span = {latitudeDelta: 0.05, longitudeDelta: 0.05};
MKCoordinateRegion region = {coord, span};
[mapView setRegion:region];
[self.view addSubview:mapView];
}
FirstLocateViewController.h code:
FirstLocateViewController.h code:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
@interface FirstLocateViewController : UIViewController <MKMapViewDelegate> {
UIButton *dismissViewButton;
MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet UIButton *dismissViewButton;
- (IBAction)dismissViewButton:(id)sender;
@end
感谢您先进任何显著的帮助。
Thank you in advanced for any significant help.
推荐答案
对于您需要创建注释创建一个具有CLLocationCoordinate2D,标题,副标题这样一个类
.h文件中
For that you need to create annotation create one class which has CLLocationCoordinate2D,title,subtitle like this .h file
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface DisplayMap : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
和.m文件
#import "DisplayMap.h"
@implementation DisplayMap
@synthesize coordinate,title,subtitle;
-(void)dealloc{
[title release];
[super dealloc];
}
@end
然后添加以下code到viewDidLoad中
and then add following code to viewdidload
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title=@"put title here";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
和实现下面的方法
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else {
[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}
按照此<一个href=\"http://www.edumobile.org/iphone/iphone-programming-tutorials/mapkit-example-in-iphone/\">tutorial:$c$c与给出的解释是:
Follow this tutorial:code with explanation is given:
这篇关于显示引脚兼容标题(注释)一次地图加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!