Displaying a contact with a title (abstract) after loading a map

I am working on my first application, and in it I just try to click on the mouse button to show the map with a pin (and the name on this location pin). I was able to load mapview and show which coordinates I want. But when I try to display the output and annotation, I have problems. Not sure where to encode this and how to make annotations to display the output. I searched and saw a lot of tutorials, but most of them demonstrate a different style of mapview and show the output of the user's choice, I want to show the output when loading the map.

Here is the code that I have to show on a map that works, but does not display output or annotation:

Code 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]; 

}


Code 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 

Thank you for your advanced support.

+7
source share
2 answers

To do this, you need to create an annotation to create one class that has CLLocationCoordinate2D, title, subtitles 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 the following code to viewdidload

 DisplayMap *ann = [[DisplayMap alloc] init]; ann.title=@ "put title here"; ann.coordinate = region.center; [mapView addAnnotation:ann]; 

and implement the 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 : here is an explanation code:

+14
source

You need to create a β€œmap abstract” object, which can be any object, but it must conform to the MKAnnotation protocol (so you can basically declare your object as

 @interface MyAnnotation: NSObject<MKAnnotation> 

) Check this protocol in the manual, these are just 3 methods and one property (coordinate). Set the coordinate and title, and then add the annotation to mapView ([self.mapView addAnnotation: annotation])

+1
source

All Articles