iOS map Development Mapkit

Source: Internet
Author: User

Now the application is very fire, because the geography-based social and e-commerce applications have very broad prospects

Two-way mobile carrier is the mobile phone (flat basic ignore), so will point on the phone map development or good

First Apple has encapsulated a set of map frames for us to use, first to use the map framework provided by Apple to import the Mapkit framework

Can be used after import is complete

First drag a mapview into your view,

Then run on the line ~ this is where you will see the map (if you use the real machine best, the simulator a bit of egg pain)

But the reality is a big map, bright no use, we need a more accurate map, then go into the MKMapView.h file to see how to use this view

First See is a proxy ~ This means that Mapview notify the outside of the way one of the ways to use the agent, point into the agent will see a lot of methods, the specific methods have what to explore with their own it ~

And then see is a maptype, then an enumeration variable, you can see the map shows the pattern of three, one is the standard, one is a satellite, and one is mixed, set up and then run to know their differences

Looking down is a region attribute, here is a setregion method, click the option to look at the help seems to be what we want, that can track the location of the property

We try to set up, but it seems that we do not know the latitude and longitude of their location, this time to see the agent inside there is a method called update, should be the location of the user after the call, it should already have latitude and longitude

Try to use a little bit.

-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation{    NSLog (  @ "%f%f", UserLocation.coordinate.latitude, UserLocation.coordinate.longitude );         // Show user peripheral information     -  - );    [Mapview setregion:region animated:yes];}

Set up the agent, and then implement this method, sure enough will be positioned to the current location, the structure of the above are not difficult, their own head file is good, in fact, one is the latitude and longitude, one is the proportion of the display

Sometimes we need to display the surrounding location information, and there is a prominent icon, this time to use the Mkannotation protocol, this Protocol is not the usual agent protocol, this Protocol is another protocol

This kind of protocol requires our custom class to obey, and contains the attributes inside and so on.

/* * The use of attributes defined in the protocol guarantees that all objects that adhere to the Protocol, all with a protocol-  defined attribute,  can typically copy the properties defined in the agreement directly into the new class, canceling the ReadOnly descriptor  of the property.  * *@interface myannotation:nsobject<MKAnnotation>** * ImageName; @end

Above is a custom class that implements the protocol

How do I add a custom point to a map? Adding a point from the top is actually adding a latitude and longitude, the following code is adding two custom points on the Mapview

  //Add a PINMyannotation *annotation1 =[[Myannotation alloc] init]; Annotation1.coordinate= Cllocationcoordinate2dmake ( -, the); Annotation1.title=@"my turf."; Annotation1.subtitle=@"ha ha haha"; Annotation1.imagename=@"head0";        [_mapview Addannotation:annotation1]; Myannotation*annotation2 =[[Myannotation alloc] init]; Annotation2.coordinate= Cllocationcoordinate2dmake ( -,106); Annotation2.title=@"Chongqing"; Annotation2.subtitle=@"ha ha haha"; Annotation2.imagename=@"head0"; [_mapview Addannotation:annotation2];

This is added to the run, and sometimes we need to define our own icons instead of the red-headed pin that comes with the system.

This custom method should be in the agent of Mapview, so we go to agent inside look for, see there is no way to return UIView or its subclasses,

And then I found it.

The following code is used to return from the top view, instead of the system-provided

#pragmaMark Custom Pin View-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (ID<MKAnnotation>) annotation{//determine if the annotation parameter is myannotation//if it's not myannotaion, it's a system pin, no processing .    if(! [Annotation iskindofclass:[myannotationclass]]) {        //using the system default PIN        returnNil; }        //Reusable Identifiers    StaticNSString *id =@"Myanno"; //to query for reusable pinsMkannotationview *annoview =[Mapview Dequeuereusableannotationviewwithidentifier:id]; //if not found, then instantiate the pin    if(Annoview = =Nil) {Annoview=[[Mkannotationview alloc] initwithannotation:annotation Reuseidentifier:id]; //Custom Pin View, if you want to accept user response, you need to set this propertyAnnoview.canshowcallout =YES; }        //set a PINAnnoview.annotation =annotation; //convert into Myannotation//set the image of the pin viewMyannotation *anno =annotation; Annoview.image=[UIImage ImageNamed:anno.imageName]; NSLog (@"Custom Pins"); returnAnnoview;}

The reason to determine the type is because the system's annotation protocol object does not have an image property, and if it does not, it crashes.

In addition, we often use to calculate the distance between two points, the demand for Apple also thought of. So this method Apple has been packaged, the following is the implementation of code

// calculates the distance between two points based on latitude and longitude    Cllocation *location1 = [[Cllocation alloc] Initwithlatitude: Longitude: $];     *location2 = [[Cllocation alloc] Initwithlatitude: Longitude:106];         = [Location1 distancefromlocation:location2];        NSLog (@ " Distance:%f", distance);

How's it going to be? Good, but general business-grade applications are using the sdk~ and Baidu map so really want to learn something, out of the need to understand Apple this framework also to study Baidu and the German SDK

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.