xcode 地图加载后显示带有标题(注释)的 Pin 图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5589181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying Pin with Title (annotation) once map loads
提问by 474N
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.
我正在开发我的第一个应用程序,在其中我只是尝试单击按钮显示带有图钉的地图(以及此图钉上的标题)。我能够加载地图视图并让它显示我想要的坐标。但是在尝试显示图钉和注释时,我遇到了问题。不确定在哪里对此进行编码以及如何进行注释以显示引脚。我搜索并看过很多教程,但大多数都显示了不同的地图视图样式,并且在用户选择时显示了图钉,我想在地图加载时显示图钉。
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 代码:
#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 代码:
#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.
在此先感谢您提供的任何重要帮助。
回答by nehal
For that you need to create annotation create one class which has CLLocationCoordinate2D,title,subtitle like this .h file
为此,您需要创建注释创建一个类,它具有 CLLocationCoordinate2D,title,subtitle 像这个 .h 文件
#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
and .m file
和 .m 文件
#import "DisplayMap.h"
@implementation DisplayMap
@synthesize coordinate,title,subtitle;
-(void)dealloc{
[title release];
[super dealloc];
}
@end
and then add following code to viewdidload
然后将以下代码添加到 viewdidload
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title=@"put title here";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
and implement following method
并实现以下方法
-(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;
}
Follow this tutorial:code with explanation is given:
按照本教程:给出了带有解释的代码:
回答by SVD
You need to create a "map annotation" object - which can actually be any object, but it must conform to MKAnnotation protocol (so you can basically declare your object as
您需要创建一个“地图注释”对象 - 它实际上可以是任何对象,但它必须符合 MKAnnotation 协议(因此您基本上可以将您的对象声明为
@interface MyAnnotation: NSObject<MKAnnotation>
). Check out that protocol in the manual, it is just 3 method and one property (coordinate). Set the coordinate and title, and then add the annotation to the mapView ([self.mapView addAnnotation:annotation])
)。查看手册中的协议,它只有 3 个方法和一个属性(坐标)。设置坐标和标题,然后给mapView添加注解([self.mapView addAnnotation:annotation])