iOS Location Services and map development (4)---show map

Source: Internet
Author: User

Before iOS 6, Apple used Google Maps, iOS 6, and Apple used its own map (as if it was a high-level map in the country), but the API programming interface didn't change much compared to iOS 5.

The IOS app uses the map Kit API to develop a mapping application that uses the Mkmapview class as the map display view with its delegation protocol mkmapviewdelegate.

1. Show Map:

Code in. h Files

#import <MapKit/MapKit.h>@interface yxcviewcontroller <MKMapViewDelegate>@end 

. m file class implementation code

@implementationYxcviewcontroller- (void) viewdidload{[Super Viewdidload]; //set the type of mapSelf.mapView.mapType =Mkmaptypestandard; //Set up proxySelf.mapview.Delegate=Self ;}#pragmaMark-mkmapviewdelegate Proxy method//Failure Callback- (void) Mapviewdidfailloadingmap: (Mkmapview *) Mapview witherror: (Nserror *) error{NSLog (@"Error:%@", error.localizeddescription);}@end

There are 3 types of maps:

Mkmaptypestandard: Standard map.

Mkmaptypesatellite: Satellite map type.

Mkmaptypehybrid: Mixed map type.

2. Add Annotations:

If you want to add a callout point to the map view, you need 2 steps: 1) to trigger the Add action; 2) Implement the Map delegate method Mapview:viewforannotation:, complete the Add callout.

1) Trigger Add action:

-(Ibaction) Geocodequery: (ID) Sender {//Get the query address string from the interface text box    if(_txtquerykey.text = = Nil | | [_txtquerykey.text length] = =0) {        return ; } Clgeocoder*geocoder =[[Clgeocoder alloc] init]; [Geocoder geocodeaddressstring:_txtquerykey.text Completionhandler:^ (Nsarray *placemarks, Nserror *error) {NSLog (@"Query record number:%i", [placemarks Count]); if([Placemarks Count] >0) {            //Remove all label points on the current map "Otherwise click the Query button and you'll see more labeling points on the map."[Self.mapview removeannotations:_mapview.annotations]; }                 for(inti =0; i < [placemarks count]; i++) {Clplacemark*placemark =Placemarks[i]; //turn off the keyboard[_txtquerykey Resignfirstresponder]; //Adjust map position and scaleMkcoordinateregion viewregion = mkcoordinateregionmakewithdistance (Placemark.location.coordinate, +, +);                        [_mapview setregion:viewregion Animated:yes]; //instantiate a custom map callout point class that implements the Mkannotation protocolMapLocation *annoation =[[MapLocation alloc] init]; Annoation.streetaddress=Placemark.thoroughfare; Annoation.city=placemark.locality; Annoation.state=Placemark.administrativearea; Annoation.zip=Placemark.postalcode; Annoation.coordinate=placemark.location.coordinate; //Add a callout point object to the map//This method is bound to invoke the proxy method mapview:viewforannotation:[_mapview addannotation:annoation]; }    }];}

The Mkcoordinateregionmakewithdistance () function encapsulates a structure that represents a map area:

typedef struct {

Cllocationcoordinate2d Center; Center Point

Mkcoordinatespan span; Span

}mkcoordinateregion;

2) Implement the Proxy method to complete the tagging

//delegate method, callback when labeling is added to the map view-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (ID<MKAnnotation>) annotation{//Get Map Callout ObjectsMkpinannotationview *annoationview = (Mkpinannotationview *) [_mapview dequeuereusableannotationviewwithidentifier:@"pin_annoation"]; if(Annoationview = =Nil) {Annoationview= [[Mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:@"pin_annoation"]; }    //set the PIN label the color of the gaze chart is purpleAnnoationview.pincolor=mkpinannotationcolorpurple; //set whether or not to animate on the map when looking at the indicatorAnnoationview.animatesdrop =YES; //Some additional information can be displayed on the callout, and a bubble appears when you click Pin in the Yes case//The text information in the bubbles is encapsulated in the MapLocation objectAnnoationview.canshowcallout =YES; returnAnnoationview;}

Custom Label class MapLocation implementation:

First, you need to introduce the <Mapkit/MapKit.h> header file because the Mkannotation protocol is included in the framework.

-(NSString *) Title: The main title on the callout point

-(NSString *) SubTitle: Callout Point Subtitle

The code in the. h header file:

#import<MapKit/MapKit.h>@interfaceMaplocation:nsobject<mkannotation>//Geographic coordinates@property (nonatomic, ReadWrite) cllocationcoordinate2d coordinate;//Street Information Properties@property (nonatomic, copy) NSString *streetaddress;//City Information Properties@property (nonatomic, copy) NSString *City ;//state, Province, and city information@property (nonatomic, copy) NSString *State ;//Postal Code@property (nonatomic, copy) NSString *zip;@end

. M in code:

@implementationmaplocation-(NSString *) title{return @"Your location! ";}-(NSString *) subtitle{nsmutablestring*ret =[[Nsmutablestring alloc] init]; if(_state) [Ret appendstring:_state]; if(_city) {[ret appendstring:_city]; }        if(_city &&_state) {[ret appendString:@","]; }    if(_streetaddress && (_city | | _state | |_zip)) {[ret appendString:@"?"]; }    if(_streetaddress) {[ret appendstring:_streetaddress]; }        if(_zip) {[ret AppendFormat:@",%@", _zip]; }        returnret; }

3, Tracking User location changes:

Mapkit provides an API to track user location and direction changes without having to write the location service code yourself. Open the Showsuserlocation property of the map view and set the method Setusertrackingmode: Yes, the reference code is as follows:

-(void  ) viewdidload{[super    Viewdidload];     //     Self.mapView.mapType = Mkmaptypestandard;     //  set proxy  Self.mapview.        delegate  = self;     //  allow tracing to display the user's location information     Self.mapView.showsUserLocation = YES;  //  set user tracking mode (3)  Span style= "Color:rgb (0, 0, 0); > [Self.mapview Setusertrackingmode:mkusertrackingmodefollow animated:yes];}  
// implementing a delegate method for a map view // This method is the callback when the location service update completes the user position // in this method, the center point of the map is resized to the current user's center point -(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: ( Mkuserlocation *) userlocation{    = userLocation.location.coordinate;}

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.