地图加载后显示带有标题(注释)的 Pin 图 [英] Displaying Pin with Title (annotation) once map loads

查看:21
本文介绍了地图加载后显示带有标题(注释)的 Pin 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 代码:​​

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 代码:

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,title,subtitle 像这样.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

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

按照这个教程:代码并给出解释:

Follow this tutorial:code with explanation is given:

这篇关于地图加载后显示带有标题(注释)的 Pin 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆