[Switch] iPhone mapkit: Add coordinates on the map using mkannotation and mkannotationview

Source: Internet
Author: User
IPhone mapkit-add coordinate points to map


IPhone developme
2010-01-22 00:01:24
Read948
Comment0

Font Size:LargeMediumSmall Subscription

☉Goal: Add a position (pois) to the Google map created in the previous example. When a position is selected, the corresponding event is generated.
☉Step by step:
In
Each base point on the geographic chart is a mkannotationview, that is, the UI. Each mkannotationview requires corresponding information.
Mkannotation: This is protocal, that is, the information required for storing each base point. Therefore, we must first create
Class.

According to the iPhone issuer's documentation. This protocal requires three features and an initialization method. The three features are coordinate, title, subtitle, and initwithcoords.

The following is the program mongopoi. H of the mkannotation type.

  1. # ImpORT <Foundation. h>
  2. # ImpORT <mapkit. h>
  3. # ImpORT <corelocation. h>
  4. @ Interface poi: nsobject <mkannotation> {
  5. Cllocationcoordinate2d coordinate;
  6. Nsstring * subtitle;
  7. Nsstring * title;
  8. }
  9. @ Property (nonatomic, readonly) cllocationcoordinate2d coordinate;
  10. @ Property (nonatomic, retain) nsstring * subtitle;
  11. @ Property (nonatomic, retain) nsstring * title;
  12. -(ID) initwithcoords :( cllocationcoordinate2d) coords;
  13. @ End

The following is the program named POI. m for mkannotation.

  1. # ImpOrt "Poi. H"
  2. @ Implementation poi
  3. @ Synthesize coordinate, subtitle, title;
  4. -(ID) initwithcoords :( cllocationcoordinate2d) coords {
  5. Self = [Super init];
  6. If(Self! = Nil ){
  7. Coordinate = coords;
  8. }
  9. ReturnSelf;
  10. }
  11. -(Void) Dealloc
  12. {
  13. [Title release];
  14. [Subtitle release];
  15. [Super dealloc];
  16. }
  17. @ End

After declaring the class that complies with mkannotation protocal, we need to establish a base point on Google map. Show Google map programs on the iPhone to take the test againUse mkmapview for Google Map.

Next,
STEP (1 ):
I announced a function called createmappoint to create a base point. Here we use the other poi we announced earlier (this is
Protocal), we create a poi, and then place the parameters, standards, and sub-standards required for the location. Next call

    1. [Mapview addannotation: poi];

Add the created poi to the annotation set of the region (mkmapview.Only the information of the base point is put into the set. At this time, no real base point has been set up..

The createmappoint program example is as follows:

  1. # ImpOrt "Poi. H"
  2. -(Void*) Createmappoint :( mkmapview *) mapview coordinatex :(Double) Coorx coordinatey :(Double) Coory
  3. Title :( nsstring *) Title subtitle :( nsstring *) Subtitle {
  4. If(Mapview! = Nil ){
  5. // Set poi lat and LNG
  6. Cllocationcoordinate2d P1;
  7. Poi * poi;
  8. If(Coorx & coory ){
  9. P1.latitude = coorx;
  10. P1.longpolling = coory;
  11. Poi = [[poi alloc] initwithcoords: P1];
  12. If(Title! = NULL)
  13. Poi. Title = title;
  14. If(Subtitle! = NULL)
  15. Poi. Subtitle = subtitle;
  16. Mapview. Delegate = self;
  17. [Mapview addannotation: poi];
  18. [Poi release];
  19. }
  20. }
  21. ReturnNULL;
  22. }

STEP (2): You can see the description file of mkmapview.Viewforannotation this method, which is the place where mkmapview creates a base point.. When mkmapview is used in the render region, the base point is set based on the information of the annotation set.

  1. -(Mkannotationview *) mapview :( mkmapview *) mapview viewforannotation :( id <mkannotation>) annotation {
  2. // Method 1: Using default pin as a placemarker to display on Map
  3. Mkpinannotationview * newannotation = [[mkpinannotationview alloc] initwithannotation: annotation reuseidentifier :@"Annotation1"];
  4. Newannotation. pincolor = mkpinannotationcolorgreen;
  5. Newannotation. animatesdrop = yes;
  6. // Canshowcallout: to display the callout view by touch the pin
  7. Newannotation. canshowcallout = yes;
  8. Uibutton * button = [uibutton buttonwithtype: uibuttontypedetaildisclosure];
  9. [Button addtarget: Self ActIon: @ selector (checkbuttontapped: Event :) forcontrolevents: uicontroleventtouchupinside];
  10. Newannotation. rightcalloutaccessoryview = button;
  11. ReturnNewannotation;
  12. // Method 2: using the image as a placemarker to display on Map
  13. /* 
  14. Mkannotationview * newannotation = [[mkannotationview alloc] initwithannotation: annotation reuseidentifier: @ "annotation1"]; 
  15. Newannotation. Image = [uiimage imagenamed: @ "icon.png"]; 
  16. Newannotation. canshowcallout = yes; 
  17. Return newannotation; 
  18. */
  19. }

The vieforannotation method in the annotation set will be processed twice.
Therefore, every time viewforannotation is executed, we need to create a mkannotationview object and return the object.
When mkmapview receives the mkannotationview, it will be displayed on the region. This is the location we want to display on the region. The above program uses
Mkpinannotationview is an object generated from mkannotationview. It is used to display a large volume on the local region. You can use

    1. Annotationview. pincolor = mkpinannotationcolorgreen;

Set the colors of the big head, But there are only three colors (it seems a little sparse xd), which are colors: red, purple, and yellow ).

    1. Newannotation. canshowcallout = yes;

Determines whether the bubble window appears when you select a large node..
A detaildisclousue type is set for the 10th-12th rows, and
Uicontroleventtouchupinside event, and put it into the accessoryview of the bubble window. Finally, the created slot will be returned
Mapview. The second method to be resolved is to directly use mkannotationview to create a base point and set her image modularity,Therefore, the location is not necessarily a big head, There can be more changes and flowers.

    1. -(Void) Checkbuttontapped :( ID) sender event :( ID) event {
    2. Uialertview * TMP = [[uialertview alloc] initwithtitle :@"Interest! "Message :@"Callout plugin"Delegate: Nil cancelbuttontitle :@"OK"Otherbuttontitles: Nil];
    3. [TMP show];
    4. [TMP release];
    5. }

The above is the event handling Regular Expression of the row in the uicontroleventtouchupinside event. When detaildisclousue is selected, an alert will pop out, indicating that the event is handled correctly.

In this example, we can see the relationship between mkannotation and mkannotationview.

 

copy from http://blog.finalevil.com/

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.