地图加载后显示带标题(注释)的 Pin 图
我正在开发我的第一个应用程序,在其中我只是尝试单击按钮显示带有图钉的地图(以及该位置图钉上的标题)。我能够加载地图视图并让它显示我想要的坐标。但是,当尝试显示引脚和注释时,我遇到了问题。不确定在哪里编码以及如何进行注释以显示引脚。我搜索并看过很多教程,但大多数都显示不同的地图视图样式,并在用户选择上显示图钉,我想在地图加载上显示图钉。
这是我必须显示正在工作的地图的代码,但没有图钉显示或注释:
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 代码:
#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
提前感谢您提供的任何重要帮助。
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:
#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:
#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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您需要创建注释,创建一个具有 CLLocationCooperative2D、标题、副标题的类,如下所示
.h 文件
和 .m 文件
,然后将以下代码添加到 viewdidload
并实现以下方法
遵循此 教程:给出带有解释的代码:
For that you need to create annotation create one class which has CLLocationCoordinate2D,title,subtitle like this
.h file
and .m file
and then add following code to viewdidload
and implement following method
Follow this tutorial:code with explanation is given:
您需要创建一个“地图注释”对象 - 它实际上可以是任何对象,但它必须符合 MKAnnotation 协议(因此您基本上可以将对象声明为
)。查看手册中的协议,它只有 3 个方法和一个属性(坐标)。设置坐标和标题,然后将注释添加到mapView中([self.mapView addAnnotation:annotation])
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
). 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])