Fashion Map 1

Source: Internet
Author: User

Reproduced iOS map development and positioning(2013-03-05 16:38:20) reproduced Original Address:iOS map development and positioning Cry, Han. Source: http://blog.csdn.net/zhuiyi316/article/details/8306615

First we need a view to render the map, the Apple comes with a map view of the class, called Mkmapview, can be found in the framework of Mapkit, so the map needs to be #import in the header file, so that you can already see a map, you can drag and zoom.

Here is an introduction to how to operate the map.

Here I would like to introduce some important properties and methods about the map.

First attribute: @property (nonatomic) Mkmaptype Maptype;

This is a struct with three attributes that determine the rendering style of the map. (not used in all-room applications, but I'll mention it here) here is an introduction to these three properties:

Standard: Labeling maps, showing street and street names

Satellite: Satellite picture Area

Hybrid: Satellite map with street and street names in the corresponding area

Second attribute: @property (nonatomic) Mkcoordinateregion region;

Also this is a struct, see the name should think of this is a map area shown on the image. Basically an area is determined by the center point, and the longitude and latitude of the span, which is the maximum latitude or longitude on the view and the minimum latitude or longitude difference.

typedef struct {

Cllocationcoordinate2d Center;

Mkcoordinatespan span;

} mkcoordinateregion;

Giving a definition of the structure should make people understand a lot.

Here you can directly modify this property to change the map on the view of the rendering area, so that it does not produce animation effect. If you want to animate the effect, you can use this method:

-(void) Setregion: (mkcoordinateregion) region animated: (BOOL) animated;

-We understand that the region has related only the method of changing the regional center can see for yourself, in the MKMapView.h header file.

Third attribute: @property (nonatomic) BOOL showsuserlocation;

This is used to determine whether the user location is displayed, the blue dots in the graph are the system's own to show the user's location.

This is a few common properties, here are some common methods:

-(Cgpoint) Convertcoordinate: (cllocationcoordinate2d) Coordinate Topointtoview: (UIView *) view;

-(Cllocationcoordinate2d) Convertpoint: (cgpoint) point tocoordinatefromview: (UIView *) view;

-(CGRect) Convertregion: (mkcoordinateregion) region Torecttoview: (UIView *) view;

-(Mkcoordinateregion) Convertrect: (cgrect) rect toregionfromview: (UIView *) view;

These methods are the conversion of the view area to the real-world location, which is often used.

Before I introduce the following method, I want to explain the "pin" and its usage.

In the use of maps, we often add some to the map to such a small view, these small images are Mkannotationview objects, we call it a pin bar.

Also like the demand, that is, click on the pin will pop up a view, in fact, it is not a view, this view is basically painted, will be changed with the position of the pin, will also be the length of the information automatically scaling, its length and so can not be changed, the system will be appropriate to change it.

How do you put this information? We need to customize a class, called Pinview. Need to implement a protocol mkannotation; I'm just going to give you the header file.

@interface pinview:nsobject{

cllocationcoordinate2d coordinate;

NSString *title;

NSString *subtitle;

}

It simply contains coordinates, headings, and subheadings. The title is: Golden Lion Spa Garden, the subtitle is: sale: 112 sets.

Now that we know how to make this information, but do not know how to put the pin up, the following will use a few methods of Mkmapview:

-(void) Addannotation: (id) annotation;

-(void) Addannotations: (Nsarray *) annotations;

-(void) Removeannotation: (id) annotation;

-(void) Removeannotations: (Nsarray *) annotations;

The first method is to add, but this is not enough, the way the pin view and style is determined by our own, which used the Mkmapview delegate method (since it is a delegate we need to set its proxy to the appropriate controller):

-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (id) annotation;

This delegate method has two parameters, the first one is the map itself, and the second is our custom Pinview object.

There is also a return value, just like the cell we returned when we were using tableview, and we need to make a pin return, so let's take a look at the following code and believe it will be clear.

-(Mkannotationview *) Mapview: (Mkmapview *) MV viewforannotation: (id) annotation

{

static NSString *indentifer = @ "Quanfangpinid";//A reusable identity look down, you'll see.

Mkpinannotationview *pinview = nil;

Nsdictionary *tempdic;

if (annotation! = mv.userlocation)//Display the user location of the blue dot is also a pin, I do not want to change it, so here to judge

{

Pinview *pin = (Pinview *) annotation; Cast a variable of type ID

Pinview = (Mkpinannotationview *) [MV dequeuereusableannotationviewwithidentifier:indentifer];//Familiar, Like a reuse mechanism that doesn't want to tableview, yes, the map will reuse the pin.

if (Pinview = nil) Pinview = [[[Mkpinannotationview alloc] initwithannotation:annotation Reuseidentifier:indentifer] autorelease];//here an empty Mkpinannotationview object is basically initialized, but if it's just pointless, we're going to turn it into what we want.

for (UIView * view in [Pinview Subviews])

{

[View Removefromsuperview];

}

Pinview.image = nil;//This is the shape of the big head, we set it to empty, and in the back we add our own defined view on the PIN, of course you can define your own look here.

Pinview.canshowcallout = yes;//when clicking on the pin will pop up the black box, look at my hair above the map to know, this is the decision can not pop.

Pinview.animatesdrop = no;//map When adding a PIN will bring the drop animation, here can be decided to use no, if not, you can define other animations, below I will introduce

Pinview.draggable = no;//here to decide whether Pinview can be dragged, it should often be set to NO

UIImage *image = [UIImage imagenamed:@ "45.png"];

Uiimageview *imageview = [[Uiimageview alloc] initwithimage:image];

Imageview.frame = CGRectMake (-13,-1, 40.5, 48);

[Pinview Addsubview:imageview];

UILabel *labeltitle = [[UILabel alloc] Initwithframe:cgrectmake (-9, 0, 35, 30)];

Labeltitle.backgroundcolor = [Uicolor Clearcolor];

Labeltitle.font = [Uifont boldsystemfontofsize:11];

Labeltitle.textcolor = [Uicolor colorwithred:62.0/255.0 green:26.0/255.0 blue:2.0/255.0 alpha:1.0];

Labeltitle.textalignment = Uitextalignmentcenter;

Labeltitle.numberoflines = 2;

if (![ Pin.title hassuffix:@ ")"])

{

if (currentviewstate = = secondhouseingdidappear)

{

Tempdic = [[Secondhousearr objectforkey:@ "content"] objectatindex:pin.tag];//[self Searchpinfromdic:secondhousearr ForTitle:pin.title];

Labeltitle.text = [NSString stringwithformat:@ "%.1FW", ([[Tempdic objectforkey:@ "Dis_saleprice"] floatValue]/10000)] ;

}

else if (currentviewstate = = sendhousedidappear)

{

Tempdic = [[Renthousearr objectforkey:@ "content"] objectAtIndex:pin.tag];

Labeltitle.text = [NSString stringwithformat:@ "%@", [Tempdic objectforkey:@ "Dis_rentprice"]];

}

[Pinview Addsubview:labeltitle];

}

else {

Tempdic = [Mapshoparr ObjectAtIndex:pin.tag];

}

[Labeltitle release];

[ImageView release];

uibutton* Rightbutton = [UIButton buttonwithtype:uibuttontypedetaildisclosure];;

Rightbutton.frame = CGRectMake (0, 0, 26, 26); Rightbutton.tag = Pin.tag;

Pinview.rightcalloutaccessoryview = Rightbutton;

}

return pinview;

}

The following is a delegate method, after your PIN has been set up after the transfer, the corresponding, basically in the above this delegate method after the successful execution of the call.

-(void) Mapview: (Mkmapview *) MV Didaddannotationviews: (Nsarray *) views;

So here you can get all the pins you've made, here you can make the animations they appear, which is the custom animation I said above.

In fact, its delegate and TableView very similar, will be in the selection, deselect the time to call the corresponding delegate, you can look at the Mkmapview's head file. Others are not very common, I also do not use, please add.

And here's the Mkmapview method.

-(void) Selectannotation: (ID) annotation animated: (BOOL) animated;

-(void) Deselectannotation: (ID) annotation animated: (BOOL) animated;

Its basic function is whether or not to automatically select a pin in an animated way, the effect is the equivalent of clicking or canceling the click of a PIN.

The above is some basic operation of the map, if the error please correct, welcome to shoot bricks and add.

Here I would like to introduce two things, and the map is actually closely related,

One is Google's Svgeocoder

The other one is the system location management

Let's talk about Svgeocoder, there are so many ways

+ (svgeocoder*) GeoCode: (NSString *) address completion: (void (^) (Nsarray *placemarks, Nserror *error)) block;

+ (svgeocoder*) GeoCode: (NSString *) address bounds: (mkcoordinateregion) bounds completion: (void (^) (Nsarray * Placemarks, Nserror *error)) block;

+ (svgeocoder*) GeoCode: (NSString *) Address region: (NSString *) region completion: (void (^) (Nsarray *placemarks, Nserror *error)) block;

+ (svgeocoder*) Reversegeocode: (cllocationcoordinate2d) Coordinate completion: (void (^) (Nsarray *placemarks, NSError * Error)) block;

Basically, the latitude and longitude and the specific address information is converted to each other.

The following describes the location management

Need to import #import

Here's a section to create a location management code

cllocationmanager* locationmanager_ = [[Cllocationmanager alloc] init];//Create location Manager

locationmanager_.delegate=self;//Setting up Agents

Locationmanager_.desiredaccuracy=kcllocationaccuracybest;//specifying the required level of precision

Locationmanager_.distancefilter = 1000.0f;//Set Distance filter

[Locationmanager_ startupdatinglocation];//start Position Manager

Here are two delegate methods, one is the callback after the successful positioning, and one is the callback after the failure, which is commonly used.

-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error

{

[Manager Stopupdatinglocation];

[Costomuikit mbpsuccess:@ "location failed"];

}

=========================== The following methods have been deprecated in iOS6 =======

-(void) Locationmanager: (Cllocationmanager *) Manager

Didupdatetolocation: (cllocation *) newlocation

Fromlocation: (cllocation *) oldlocation;

=============================================

The alternative method for the above method in IOS6 is:

-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations

Use an array to store all the location information, and if you need to view the previous location information, use [locations Lastobject] to return to the original oldlocation.

Mention the cllocation, which is the class of the management position, the most basic is composed of longitude and latitude.

We can calculate the distance between the two latitude and longitude by the following method:

-(Cllocationdistance) Distancefromlocation: (const cllocation *) location;

Returns a double type of data.

Fashion Map 1

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.