地图加载后显示带标题(注释)的 Pin 图

发布于 2024-10-31 03:01:50 字数 1360 浏览 0 评论 0原文

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

这是我必须显示正在工作的地图的代码,但没有图钉显示或注释:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪菊怪哟 2024-11-07 03:01:50

为此,您需要创建注释,创建一个具有 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;
}

遵循此 教程:给出带有解释的代码:

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

and .m file

#import "DisplayMap.h"


@implementation DisplayMap

@synthesize coordinate,title,subtitle;


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

@end

and then add following code to 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:

还不是爱你 2024-11-07 03:01:50

您需要创建一个“地图注释”对象 - 它实际上可以是任何对象,但它必须符合 MKAnnotation 协议(因此您基本上可以将对象声明为

@interface MyAnnotation: NSObject<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

@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])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文