Two months ago, with an interest in iOS development began to contact the development of Apple equipment, began to contact Object-c, later with OC to replace, I new one, will often write something to share, there is do not in place, please forgive us. Today is my first blog post. What is wrong or is not written in place. Please correct me, everybody progress together. Today I'd like to introduce some of the map classes in the iOS development process.
OC is an object-oriented language, and the C++,java we touch are the same. In OC, in addition to the basic data types, the others are basically called through objects. In the process of iOS development, the main classes and data structures used to add maps are:Mkmapview is the view used to display the map,cllocationcoordinate2d is used to represent the geographic coordinates of a region, in Cllocationcoordinate2d , it is important to note that the longitude and latitude are the positive coordinates. mkcoordinateregion is used to represent the display area of a map, which is defined as
Typedefstruct {
Cllocationcoordinate2d center;//used to represent the center coordinates of a position
Mkcoordinatespan span;//Horizontal and vertical display of the size of the map, also defines the zoom level of the map
} mkcoordinateregion;
Mkmapview has a lot of properties can be set such as : zoomenabled scrollenabled showsuserlocation and other methods, can be set according to requirements. Find out more about Apple's official documentation.
self. Mapview =[[mkmapviewalloc] initwithframe:self. View. Bounds];
self. Mapview . Maptype = Mkmaptypestandard;
Self . Mapview . zoomenabled = YES ;
self. Mapview . scrollenabled = YES ;
self. Mapview . Showsuserlocation =NO;
Self. Mapview. delegate =self;
cllocationcoordinate2d are initialized in the following ways:
1.cllocationcoordinate2d
coordinate = {,};
2. cllocationcoordinate2d coordinate;
coordinate. latitude = 34.4668711;
Coordinate. Longitude =113.453221
// cllocationcoordinate2d coordinate ={34.4668711,113.453221}; cllocationcoordinate2d coordinate; Coordinate.latitude =34.4668711;
Coordinate. Longitude = 113.453221;
Mkcoordinatespan span = {1,1};//
Represents the precision of the display area, the smaller the value, the more accurate the range, and the larger the value represents, but not the exact
The following is a value for creating a representation range based on geographic coordinates and span
There are several ways to initialize Mkcoordinateregion:
1. Mkcoordinateregion region = {Coordinate,span}; Initialize Method
2. mkcoordinateregion region ;
Region = {Coordinate,span};
Region = Mkcoordinateregionmake(coordinate, span);
3. Region = mkcoordinateregionmakewithdistance (coordinate, 111*1000*span.latitudedelta, 111*1000*span.longit Udedelta); The parameters of the intermediate pass are already defined.
Mkcoordinateregion region ={Coordinate,coordinatespan}; // // mkcoordinateregion region; // Region =mkcoordinateregionmake (coordinate, coordinatespan); // Region =mkcoordinateregionmakewithdistance (coordinate, Coordinatespan.latitudedelta, Coordinatespan.longitudedelta); // // [Self.mapview setregion:region];
Cllocationmanager used to manage location information. Apple's official documents are as follows:
The Cllocationmanager class defines the interface for configuring the delivery of Location-and heading-related E Vents to your application. You use a instance of this class to establish the parameters, determine when location and heading events should is de Livered and to start and stop the actual delivery of those events. You can also use a location Manager object to retrieve the most recent location and heading data.
Self.locationmanager =[[Cllocationmanager alloc] init]; Self.locationmanager. Delegate = = = kcllocationaccuracybest; [Self.locationmanager startupdatinglocation];
With the above method, you can simply implement a map function.
Let's take a look at some of the methods used, and I'll just mention some of the more common and major methods.
ways to get location information in Cllocationmanager
1.-(void) Locationmanager: (cllocationmanager *) Manager didupdatetolocation: ( Cllocation *) newlocation
Fromlocation: (cllocation *) oldlocation//This method was canceled after 6.0.
-(void) Locationmanager: (cllocationmanager *) Manager didupdatetolocation: (cllocation *) NewLocation fromlocation: (cllocation *) oldlocation
{
Cllocationdistance distance =[newlocation distancefromlocation:oldlocation];
NSLog (@ "Dis is%f", distance/1000.0);
}
2. -(void) Locationmanager: (cllocationmanager *) Manager
Didupdatelocations: (nsarray *) Locations
Both methods can obtain location information, but if two methods are written, only the second method is executed. So we recommend two ways to use it.
-(void) Locationmanager: (cllocationmanager *) Manager
Didfailwitherror: (nserror *) error
This method can obtain the error message during the execution of the program, and the empty position is not available.
With the above method we can create a simple program to display the map.
Of course, a more important class on the map is the Annotation class, which provides annotations to the map. So how do you implement a annotation on a map?
Let's take a look at annotation. There are protocols and delegates in Mkannotation (a red Pin's note icon will be generated by default if not implemented)
The basic steps for implementing the annotation class are:
* Create annotation class (requires declaration implementation Mkannotation Protocol)
* Instantiate annotation Class object
* Add the annotated class object instantiated to the Mkmapview object
* Similar to the UITableViewCell method to create the Mkannotationview object, which is used to eventually display on the map.
-(mkannotationview *) Mapview: (mkmapview *) Mapview viewforannotation: (ID < Mkannotation>) annotation;
When the annotation object is added to the Mapview , this method is called to set the display interface;
-(mkannotationview*) Mapview: (Mkmapview *) Mapview viewforannotation: (ID<MKAnnotation>) annotation{if([Annotation iskindofclass:[mkuserlocationclass]]) { returnNil; } StaticNSString *indetifier =@"Indentifier"; Qycustomview*pinannotationview = (Qycustomview *) [Mapview Dequeuereusableannotationviewwithidentifier:indetifier]; if(Nil = =Pinannotationview) {Pinannotationview=[[Qycustomview alloc] initwithannotation:annotation reuseidentifier:indetifier]; }Pinannotationview.canshowcallout =YES;Uiimageview*leftimagview =[[uiimageview Alloc] Initwithframe:cgrectmake (0,0, -, -)];leftimagview.image =[uiimage imagenamed:@ "Icon 2"];Pinannotationview.leftcalloutaccessoryview =Leftimagview; UIButton*BTN =[UIButton buttonwithtype:uibuttontypedetaildisclosure]; [Btn addtarget:self Action: @selector (Onbutton:) forcontrolevents:uicontroleventtouchupinside]; Pinannotationview.rightcalloutaccessoryview=btn; returnPinannotationview; }
-(void) Mapview: (mkmapview *) Mapview Annotationview: (mkannotationview *) view calloutaccessorycontroltapped: (uicontrol *) control;//This method is called when the Mkannotationview object is clicked
Of course, these methods are proxy methods, which should be implemented at the beginning of the delegate.
-(void) Mapview: (Mkmapview *) Mapview Annotationview: (Mkannotationview *) View calloutaccessorycontroltapped :(Uicontrol *) control{ *alertview =[[uialertview alloc] Initwithtitle:@ "Tapped" delegate: nil cancelbuttontitle:@ "OK" Otherbuttontitles:nil, nil]; [Alertview show];}
Here's an introduction to how to implement a pin
Mkpointannotation *pointannotation =[[Mkpointannotationalloc] init]; =coordinate; =@ " Zhengzhou, Henan "; =@ " Henan Qingyun ";
It is important to note here that Mkpointannotation is equivalent to a model, just to provide the data, if only the information is not displayed, it is attached to the Mkannotationview object, the way to add a pin from the sky is to add a Mkannotationview object Pinannotationview Pinannotationview.animatesdrop =yes; it's all right.
Let's talk about the location of the anti-coding , the specific understanding should be know cllocation object information, how to obtain specific information about the country, province, capital, Street. The specific anti-coding to use the Clgeocoder object, there is a Clplacemark class,Clplacemark the location of the results, including location information country, province, district (city), street, location name.
Clgeocoder *geocoder =[[Clgeocoder alloc] init]; Cllocation*location =[[Cllocation alloc] InitWithLatitude:coordinate.latitude longitude:coordinate.longitude]; [Geocoder reversegeocodelocation:location Completionhandler:^ (Nsarray *placemarks, Nserror *error) {if(Nil! =error) {NSLog (@"Error info is%@", error); return
} //Clplacemark for(Clplacemark *placemarkinchplacemarks) {
NSLog (@"%@,%@,%@,%@", Placemark.country,placemark.administrativearea,placemark.locality,placemark.thoroughfare); }
}];
What information do you want to print to see what you need, please check Apple official documentation for details. There is the concept of block blocks in the middle, if you do not understand, please refer to block blocks of information. Block blocks are used in the code that begins with ^.
With the above information we can basically complete the common functions of the implementation, but now has the location information of the app, like the great map, tick-tock Taxi and other app display rich content, finally we will be how to customize their own view.
First, customize one of your own classes to inherit from Mkannotationview, such as:Qyannotationview: mkannotationview. And then rewrite this class.
-(void) setselected: (BOOL) Selected This method, of course, you can not define your own class.
- (void) setselected: (BOOL) Selected {
[Super setselected:selected]; UIView*view = [[UILabel alloc] Initwithframe:cgrectmake (- +, - +, -, +)] View.backgroundcolor=[Uicolororangecolor]; Uiimageview*imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (0,0, +, +)]; Imageview.image= [UIImage imagenamed:@"icon"]; [View Addsubview:imageview]; [Self addsubview:view]; }
Another way is to rewrite
-(void) setselected: (bool) selected animated: (bool) Animated This method, produces the same effect, But this adds an animation to the back. Interested can write two separately to see what the difference is, here is not more said
But is this done? You will find that when you click on the icon, there are two overlapping views, not the results you want to do?
So how to solve this problem, it is very simple, is in the definition of Mkannotationview object above the Canshowcallout = NO, the test is available, hoping to help people who are troubled by this problem.