Mkannotationview,mkannotation and Mkmapview in conjunction with display coordinate points

Source: Internet
Author: User

Restrictions: The iphone's operating system must be updated to the ios3.0 version, and the SDK for developing the SDK is 3. 0 has built the mapkit Framework.

Effect screen:

Step Description:
Every mark on the map is a mkannotationview, that is, the UI. And each mkannotationview need to have the corresponding data mkannotation, this is protocal, that is, the storage of each coordinates need to use the information of the place. So we have to build a class that uses mkannotation first.

According to the iphone developer file. This protocal needs to declare three attributes and an initialization method. Three attributes are coordinate, title, subtitle, and a method initwithcoords.

The following is the code POI for the Mkannotation class. H

#import
#import
#import 

@interface poi:nsobject  {

	cllocationcoordinate2d coordinate;
	NSString *subtitle;
	NSString *title;
}

@property (nonatomic,readonly) cllocationcoordinate2d coordinate;
@property (Nonatomic,retain) nsstring *subtitle;
@property (Nonatomic,retain) nsstring *title;

-(ID) initwithcoords: (cllocationcoordinate2d) coords;

@end

The following is the code POI for the Mkannotation class. M

#import "POI. H "

@implementation POI

@synthesize coordinate,subtitle,title;

-(ID) initwithcoords: (cllocationcoordinate2d) coords{

	self = [super init];

	if (self!= nil) {

		coordinate = coords; 

	}

	return self;

}

-(void) dealloc

{
	[title release];
	[subtitle release];
	[Super Dealloc];
}

@end

After declaring a category that conforms to the mkannotation protocal, we are going to create a coordinate point on the Google map. On the iphone, first Google Map program can participate in a blog post

Then,
The first step: I declare a function createmappoint create a coordinate point. In this we used to declare the category POI (this conforms to the Mkannotation protocal category), we instantiate a poi, and then the latitude and longitude, headings, subheadings, and so on that we need to sit on the punctuation. Then call

[Mapview Addannotation:poi];

Add the POI we have created to the annotation collection of maps (Mkmapview). Put in the collection is only the data of the punctuation, this time did not really establish a coordinate point

Here's the code for the function Createmappoint:

#import "POI. H "

-(void*) Createmappoint: (Mkmapview *) Mapview Coordinatex: (double) Coorx Coordinatey: (double) coory
				  Title :(nsstring*) title subtitle: (nsstring*) subtitle{

	if (mapview!=nil) {

		//set POI lat and LNG
		Cllocationcoordinate2d P1;
		POI *poi;

		if (Coorx && coory) {

			P1. Latitude=coorx;
			P1. longitude = coory;
			POI = [[Poi alloc] initwithcoords:p1]; 

			if (title!=null)
				poi. Title=title;

			if (subtitle!=null)
				poi. Subtitle=subtitle;

			[Mapview Addannotation:poi];
			[POI release];

		}
	return NULL;
}

Part II: Refer to the Mkmapview documentation can see viewforannotation This method, this is mkmapview actual establishment of coordinates point of place. The Mkmapview category creates a coordinate point according to the data in the annotation collection when rendering the map.

-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (ID <MKAnnotation>) annotation{// Method one: Using default PIN as a placemarker to display on map mkpinannotationview *newannotation = [[Mkpinannotationview alloc
	] initwithannotation:annotation reuseidentifier:@ "Annotation1"]; Newannotation.Pincolor = Mkpinannotationcolorgreen; Newannotation.Animatesdrop = YES; Canshowcallout:to Display the callout view by touch the pin newannotation.Canshowcallout=yes;
	UIButton *button = [UIButton buttonwithtype:uibuttontypedetaildisclosure];
	[Button addtarget:self action: @selector (checkbuttontapped:event:) forcontrolevents:uicontroleventtouchupinside]; Newannotation.Rightcalloutaccessoryview=button;

	return newannotation; Method Two: Using the image as a placemarker to display on map/* Mkannotationview *newannotation=[[mkannotationview alloc] I
	 Nitwithannotation:annotation reuseidentifier:@ "Annotation1"]; Newannotation.Image = [UIImage imagenamed:@ "icon.PNG "]; Newannotation.Canshowcallout=yes;
	 return newannotation; */
}

There are several coordinate points in the Annotation set viewforannotation methods are executed several times. So every time viewforannotation is executed, we have to instantiate a mkannotationview. Mkmapview receives an instance of Mkannotationview, it is first on the map. In the above code is used Mkpinannotationview is integrated from Mkannotationview, the role is to display a spike on the map. You can use

Annotationview. Pincolor = Mkpinannotationcolorgreen;

can set the color of nails, but only red, purple, green three kinds.
Newannotation.canshowcallout=yes;

Set the effect at the point of tack. Row 10th to line 12th dynamically creates a Detaildisclousue type button, sets a UIControlEventTouchUpInside event for the button, and puts it into the bubble view Accessoryview. Finally, the established coordinates are passed back to the Mapview.

The second method of annotation is to establish a coordinate point directly using Mkannotationview and set its Image property.

-(void) checkbuttontapped: (ID) Sender event: (ID) event{

	uialertview *tmp= [[Uialertview alloc] initwithtitle:@ "Hi. "message:@" test "Delegate:nil cancelbuttontitle:@" OK "otherbuttontitles:nil];
	[TMP show];
	[TMP release];
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.