Open Platform: Baidu map for iOS

Source: Internet
Author: User
Tags map class

Today, we started to use the Baidu map API to implement related geographic location functions.

In accordance with the official documentation: http://developer.baidu.com/map/sdkiosdev-2.htm

After a series of introductions, pay attention to the following two details. Otherwise, errors will occur during compilation and running:

1: Put xcode in the objective-C ++ mixed compiling mode for compilation: The simplest way is to change one of the project files at will.MChanged.Mm.

2: Because the static library containsCATEGORY entry (fourth point)To compile the project support category: Project-> build settings-> Other linker flags:
-All_load

3: AboutSetpaopaoviewThe warning temporary solution is as follows: Other linker flagsAdd
-W

4: It is recommended to merge static databases.

Next, we will record the specific functions used:

First, after the map is loaded successfully, enable the positioning function to confirm the geographical location of the currently used device. The Code is as follows:

1. Develop the positioning function to allow map applications to obtain geographical location information from time to time and trigger delegation.

_ Mapview. showsuserlocation = yes; // enable the locating Service

Delegate triggered:

- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation{  }

2: If the preceding delegate is called, it indicates that the location information has been obtained successfully. Next, you need to move the map location to the location where the location is located. The Code is as follows:

_ Mapview. userlocation // record the current location of the device

Nslog (@"! Latitude !!! % F ", userlocation. Location. Coordinate. Latitude); // longitude nslog (@"! Longtitude !!! % F ", userlocation. Location. Coordinate. longpolling); // latitude

// Input the latitude and longitude, and lock the baidumapview to the display area centered on the current latitude and longitude and the appropriate display range-(void) Adjust :( cllocationcoordinate2d) Coordinate {bmkcoordinateregion region; If (! _ Issetmapspan) // use a variable to determine the display range map region {region = bmkcoordinateregionmake (coordinate, bmkcoordinatespanmake (0.05, 0.05) only when the display area is locked for the first time )); // The smaller the value, the more detailed the map is displayed. _ issetmapspan = yes; [baidumapview setregion: Region animated: Yes]; // The display range of execution settings} _ currentselectcoordinate = coordinate; [baidumapview set: coordinate animated: Yes]; // moves the origin point to this area in an animation based on the provided longitude and latitude}

RunSetcentercoordinate: CoordinateMove later. After the move is completed, the following Commission will be executed:

-(Void) mapview :( bmkmapview *) mapview regiondidchangeanimated :( bool) animated {[baidumapview. annotations enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * Stop) {bmkpointannotation * Item = (bmkpointannotation *) OBJ; If (item. coordinate. latitude ==_currentselectcoordinate. latitude & item. coordinate. longpolling = _ currentselectcoordinate. longpolling) {[baidumapview selectannotation: OBJ animated: Yes]; // after execution, the annotation in the map is in the pop-up bubble box * Stop = Yes ;}}];}

Note: "0.05" indicates the details of the displayed area. If the set value is the minimum, the displayed map area will be more detailed. Please try it yourself. You cannot clearly describe it in a few words.

In addition, when the location information is obtained, it will be continuously obtained._ Mapview. showsuserlocationSetNo;

Second: Add a label to the specified position.

Bmkpointannotation * Item = [[bmkpointannotation alloc] init]; item. coordinate = coordinate; // latitude and longitude item. title = titlestring; // The title item. subtitle = subtitlestring; // subtitle [baidumapview addannotation: item];

Note: Add a annotation for map class reference.AddannotationLaterBaidumapviewTo trigger the following delegate, this delegate can customize the annotation view.

// The principle is similar to that of the uitableview loop delegate load sequence-(bmkannotationview *) mapview :( bmkmapview *) view viewforannotation :( id <bmkannotation>) annotation {static nsstring * annotationviewid = @ "annot "; optional * annotationview = [view comment: annotationviewid]; If (annotationview = nil) {annotationview = [[comment alloc] initwithannotation: annotation reuseidentifier: annotationviewid]; (optional *) annotationview ). animatesdrop = yes; annotationview. custom = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "icon_location.png"]; // view displayed on the left of the bubble box. You can customize uibutton * selectbutton = [uibutton buttonwithtype: uibuttontypecustom]; [selectbutton setframe :( cgrect) {260,0, 50, annotationview. help_height}]; [selectbutton settitle: @ "OK" forstate: uicontrolstatenormal]; annotationview. rightcalloutaccessoryview = selectbutton; // The View displayed on the right of the bubble box can be customized [selectbutton setbackgroundcolor: [uicolor redcolor]; [selectbutton progress: Yes]; [selectbutton addtarget: Self action: @ selector (location_selectpointannotation :) forcontrolevents: uicontroleventtouchupinside];} // you can use the following three lines of code to bind custom views and tags to one-to-one mappings. When you click a custom view on the right, the annotationview tag is clicked. rightcalloutaccessoryview. tag = _ cacheannotationtag; [_ cacheannotationmdic setobject: annotation forkey: [nsnumber numberwithinteger: _ cacheannotationtag]; _ cacheannotationtag ++; // if it is my location annotation, you can drag to change the annotation view and assign the Green style to the IF ([annotation. title isequaltostring: string_mylocation]) {(bmkpinannotationview *) annotationview ). pincolor = bmkpinannotationcolorgreen; // The annotation is green [annotationview setdraggable: Yes]; // you can drag [annotationview setselected: Yes animated: Yes]; // keep the annotation in the status of the pop-up bubble box} else {(bmkpinannotationview *) annotationview ). pincolor = bmkpinannotationcolorred;} annotationview. centeroffset = cgpointmake (0,-(annotationview. frame. size. height * 0.5); // does not know what to do with annotationview. annotation = annotation; // bind the corresponding coordinate point and latitude annotationview. canshowcallout = true; // you can click return annotationview in the pop-up window ;}

If you manually click the annotation view in the map orBmkannotationview setselect: YesThe following delegate is triggered:

- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{    _currentSelectCoordinate = view.annotation.coordinate;}

When you click the pop-up bubble box, the following delegation is triggered:

- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view;

To delete a tag:

NSMutableArray *annotationMArray = [[NSArray arrayWithArray:baiduMapView.annotations] mutableCopy];[baiduMapView removeAnnotations:annotationMArray];

The third is to start poi search.

Initialization:

Baidumapsearch = [[bmksearch alloc] init]; baidumapsearch. Delegate = self; [bmksearch setpagecapacity: 10]; // set the number of pages to be searched each time

Start search: Provides search for cities and keywords

BOOL flag = [baiduMapSearch poiSearchInCity:_currentCity withKey:_searchKeywordString pageIndex:_searchPageIndex];

Search delegate

-(Void) ongetpoiresult :( nsarray *) poiresultlist searchtype :( INT) type errorcode :( INT) error {// If (error = bmkerrorok) {bmkpoiresult * result = [poiresultlist objectatindex: 0]; // if any, data is searched for (INT I = 0; I <result. poiinfolist. count; I ++) {bmkpoiinfo * poi = [result. poiinfolist objectatindex: I] ;}}

Note: bmkpoiresult shows the current search result, as follows: total number of data records searched, number of data records searched this time, and number of pages searched currently.

Bmkpoiinfo is the result of each search, including the phone number, address, and latitude and longitude.

A maximum of related resource packages: http://www.kuaipan.cn/file/id_10716325655621670.htm

Password:Qnwnaf

Related Article

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.