Baidu Map Common functions

Source: Internet
Author: User

Reference: http://www.jianshu.com/p/9f00911bf530

Preface: Used to do the project with the German Map SDK, need to register the account and AppID, and then download the SDK integration into the project, more trouble, look at the Apple comes with the mapkit frame, feel very good, the official documents are also introduced in detail, to achieve the map display, display the user's current position, place a pin, Pop bubbles and other basic functions. Hope to help the new contact Mapkit Friends ~ Dry point here

To import the primary header file:

#import

Mapkit Framework Usage Notes:

The prefix for all data types in the Mapkit framework is Mk.

Mapkit has a more important UI control: Mkmapview, which is designed for map display.

Trace Displays the user's location

Setting Mkmapview's Usertrackingmode property to track the current location of the user

Mkusertrackingmodenone: Do not track the user's location              

Mkusertrackingmodefollow: Tracks and displays the user's current location on the map

Mkusertrackingmodefollowwithheading: Tracks and displays the user's current location on the map. The map will rotate with the user's direction                                                        ,         &NB Sp                    

The Blue glow dot is the user's current position                                  ,         &NB Sp                          ,         &NB Sp     Blue Glow origin, professional term called "pin"

Type of Map

You can set the map type by setting the Maptype of the Mkmapview Mkmaptypestandard: General map

Mkmaptypesatellite: Satellite Cloud

Mkmaptypehybrid: Mixed mode (normal map overlay on satellite cloud)

Mkmaptypesatelliteflyover:3d Stereoscopic Satellite (iOS9.0)

Mkmaptypehybridflyover:3d stereo Mixing (iOS9.0)

Set other properties of the map

1. Action Item:

<1.> is scalable: zoomenabled

<2.> is scrollable: scrollenabled

<3.> can be rotated: rotateenabled

2. Display items:

<1.> whether to show Compass: Showscompass (iOS9.0)

<2.> whether to display scale bar: Showsscale (iOS9.0)

<3.> whether to show traffic: showstraffic (iOS9.0)

<4.> whether to show buildings: showsbuildings

Use note: When setting the corresponding property, be aware of which system version the property was introduced from, and do a good job of adapting the different system versions


Track User's location

Set the Usertrackingmode property of Mkmapview to track the current location of the user

Mkusertrackingmodenone: Do not track the user's location

Mkusertrackingmodefollow: Track and display the user's current location on the map

Mkusertrackingmodefollowwithheading: Tracks and displays the user's current location on the map, which rotates with the user's forward direction

Note: After iOS8.0, tracking the user location requires the user to locate the authorization

Note:

ios8.0-, the map does not automatically scroll to the user's location

ios8.0+, the map will automatically zoom in to the appropriate proportions and display the user's location

Mkmapview's agent

Mkmapview can set up a proxy object to listen to the map's related behavior

The common proxy methods are

1. Call very frequently, constantly monitor the user's current location each call, will be the user's latest location (userlocation parameter) passed in -(void) Mapview: (mkmapview*) Mapviewdidupdateuserlocation: (mkuserlocation*) Userlo cation;

2. Call-(void) Mapview when the display area of the map is about to change: (mkmapview*) MA Pviewregionwillchangeanimated: (BOOL) animated;

3. When the display area of the map has changed, call-(void) Mapview: (mkmapview*) MAPV Iewregiondidchangeanimated: (BOOL) animated;

Mkuserlocation ( Mkuserlocation is actually a pin model, including the following attributes)

@property (nonatomic,copy) Nsstring*title; the title displayed on the Pin

@property (nonatomic,copy) nsstring*subtitle; a sub-title displayed on a PIN

@property (readonly,nonatomic) cllocation*location; location information (where are the pins pinned?)

Set the display of the map

You can set the location and area of the map display by Mkmapview the following methods

1. Set the center point location of the map @property (nonatomic) cllocationcoordinate2dcentercoordinate;

-(void) Setcentercoordinate: (cllocationcoordinate2d) Coordinate animated: (BOOL) animated;

2. Set the display area of the map @property (nonatomic) mkcoordinateregionregion; -(void) Setregion: (mkcoordinateregion) region animated: (BOOL) animated;



Basic operation of the pin

Add a pin:-(void) Addannotation: (id) annotation;

Add multiple pins:-(void) Addannotations: (nsarray*) annotations;

Remove a pin:-(void) Removeannotation: (id) annotation;

Remove Multiple pins:-(void) Removeannotations: (nsarray*) annotations;

What is the

(ID) annotation parameter?                          ,         &NB Sp                          ,         &NB Sp                pin model objects: Data used to encapsulate a pin, such as the position of a pin, title, sub-title, and so on

pin model

Create a new PIN model class

#import

@interfaceMJTuangouAnnotation: NSObject

/** coordinate location */                    @property (nonatomic,assign ) Cllocationcoordinate2dcoordinate;

/** Title */                                                    Nonatom         @property ic,copy) Nsstring*title;

/** subtitle */                              &NBSP ;                       @property (nonatomic,copy) nsstring*subtitle;< /p>

@end

Add a PIN

Edtuangouannotation*anno = [[Mjtuangouannotationalloc]init];

[Email protected] "The dog who loves iOS";

[Email protected] "All courses 15 percent, members 20 percent, old students 30 percent";

Anno.coordinate=cllocationcoordinate2dmake (40,116);

[Self.mapViewaddAnnotation:anno];

Mkannotationview: The pin control on the map is Mkannotationview

Properties of the Mkannotationview

@property (nonatomic,strong) ID annotation: PIN model

@property (Nonatomic,strong) uiimage*image: Pictures displayed

@property (nonatomic) Boolcanshowcallout: Whether the callout is displayed

@property (nonatomic) Cgpointcalloutoffset: The offset of the callout

@property (strong,nonatomic) Uiview*rightcalloutaccessoryview: What controls appear to the right of the callout

@property (strong,nonatomic) Uiview*leftcalloutaccessoryview: What controls appear to the left of the callout

Mkpinannotationview

Mkpinannotationview is a subclass of Mkannotationview

Mkpinannotationview has 2 more properties than Mkannotationview

@property (nonatomic) Mkpinannotationcolorpincolor: Pin color

@property (nonatomic) Boolanimatesdrop: Does the pin appear when the first time it appears


Mkmapitem Call system app to navigate

Main methods: [Mkmapitem openmapswithitems:items LAUNCHOPTIONS:MD];

Sample code

1. Call system navigation based on two landmark objects-(void) Beginna Vwithbeginplacemark: (clplacemark*) Beginplacemark Andendplacemark: (Clplacemark *) Endplacemark

{

2. Create a starting point: Create a Mkplacemark Landmark object based on the Clplacemark landmark object Mkplacemark *itemp1 = [[Mkplacemark alloc]

Initwithplacemark:beginplacemark];

Mkmapitem *item1 = [[Mkmapitem ALLOC]INITWITHPLACEMARK:ITEMP1];

3. Create end point: Create Mkplacemark Landmark object based on Clplacemark Landmark object Mkplacemark *itemp2 = [[Mkplacemark Alloc]initwi Thplacemark:endplacemark];

Mkmapitem *item2 = [[Mkmapitem ALLOC]INITWITHPLACEMARK:ITEMP2];

Nsdictionary *launchdic = @{

4. Set the navigation mode parameter Mklaunchoptionsdirectionsmodekey:mklaunchoptions Directionsmodedriving,

5. Set the map type Mklaunchoptionsmaptypekey: @ (mkmapty Pehybridflyover),

6. Set whether traffic is displayed

Mklaunchoptionsshowstraffickey: @ (YES),

};

7. Call the system map for navigation based on the Mkmapitem array and the startup parameter dictionary

[Mkmapitem openmapswithitems:@[item1, item2]

Launchoptions:launchdic];

}

Mkmapcamera Map Street View

Main method: Self.mapView.camera = camera;

Sample code

1. Create Perspective Center Coordinates: CLLOCATIONCOORDINATE2D

Center = cllocationcoordinate2dmake (23.132931, 113.375924);

2. Create a 3D perspective:

Mkmapcamera *camera = [Mkmapcameracameralookingatcentercoordinate:center

Fromeyecoordinate:cllocationcoordinate2dmake (center.latitude + 0.001,

Center.longitude + 0.001) eyealtitude:1];

3. Set to show on map: Self.mapView.camera = camera;

Map Mkmapsnapshotter

1. Main methods

[Snapshotter startwithcompletionhandler:^ (Mkmapsnapshot

* _nullable Snapshot, Nserror * _nullable error) {}];

Sample code

Additional options: mkmapsnapshotoptions *options = [[Mkmapsnapshotoptions alloc] init];

1. Set the area (the area on the map, acting on the map):

Options.region =self.mapview.region;

Options.maprect =self.mapview.visiblemaprect;

2. Set the image size (function in the output image)

Options.size =self.mapview.frame.size;

3. Set the picture scale (default is the screen scale, acting on the output image)

Options.scale = [[UIScreen mainscreen] scale];

Mkmapsnapshotter *snapshotter = [[Mkmapsnapshotter alloc]

Initwithoptions:options];

[Snapshotter startwithcompletionhandler:^ (Mkmapsnapshot *_nullablesnapshot,

Nserror*_nullableerror) {

if (Error) {

NSLog (@ "error:%@", error.localizeddescription);

}else

{

4. Setting the screen slice display

Self.snapshootImageView.image = Snapshot.image;

Save the picture to the specified path (here is the desktop path that needs to be modified according to the PC)

NSData *data = uiimagepngrepresentation (snapshot.image);

[Data writetofile:@ "/users/wangshunzi/desktop/snap.png" atomically:yes];

}

}];

Mkdirections Getting navigation route information

Main methods

[Directions Calculatedirectionswithcompletionhandler:

^ (Mkdirectionsresponse * _nullable response,

Nserror * _nullable error) {

}];

Draw routes, add overlays, etc.

The Main method: (unlike adding a pin, as long as we add a PIN model, the system's PIN view is added by default on the map)

1. Add overlay, we need to implement the corresponding proxy method, in the proxy method to return the corresponding covering layer [self.mapViewaddOverlay:overlay];

2. When the above method is called, the following proxy method is called to obtain the corresponding render coating-(Mkoverlayrenderer *) Mapview: (Mkmapview *) Mapview Rendererforoverlay: (ID) Overlay



Wen/Baidu engineer Ah Wei (Jane book author)
Original link: http://www.jianshu.com/p/9f00911bf530
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Baidu Map Common functions

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.