gpt4 book ai didi

iphone - map 加载后显示带标题(注释)的 Pin 图

转载 作者:行者123 更新时间:2023-12-03 18:38:04 25 4
gpt4 key购买 nike

我正在开发我的第一个应用程序,在其中我只是尝试单击按钮显示带有图钉的 map (以及该位置图钉上的标题)。我能够加载 map View 并让它显示我想要的坐标。但是,当尝试显示引脚和注释时,我遇到了问题。不确定在哪里编码以及如何进行注释以显示引脚。我搜索并看过很多教程,但大多数都显示不同的 map View 样式,并在用户选择上显示图钉,我想在 map 加载时显示图钉。

这是我必须显示正在运行的 map 的代码,但没有图钉显示或注释:

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

提前感谢您提供的任何重要帮助。

最佳答案

为此,您需要创建注释,创建一个具有 CLLocationCooperative2D、标题、副标题的类,如下所示.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

和.m文件

#import "DisplayMap.h"


@implementation DisplayMap

@synthesize coordinate,title,subtitle;


-(void)dealloc{
[title release];
[super dealloc];
}

@end

然后将以下代码添加到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;
}

关注此tutorial :给出带有解释的代码:

关于iphone - map 加载后显示带标题(注释)的 Pin 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5589181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com