Displaying Pin with Title (annotation) once map loads

亡梦爱人 提交于 2019-11-30 15:17:00

问题


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.


回答1:


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:




回答2:


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



来源:https://stackoverflow.com/questions/5589181/displaying-pin-with-title-annotation-once-map-loads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!