Iospoi Search Detailed Summary

Source: Internet
Author: User
Tags polyline

iOS Baidu Map path planning and POI search Detailed summary


Path planning-use of PNG Baidu maps

Baidu Map API Import Online said a lot of pits, but I met less, here put two more common bar.
Pit One:


Austrian Union Wifi_xcodeproj.png


As shown, add this field to the Infoplist, and you can also set the content of the hint. Don't add the words, hehe Hey, look forward to your try.
Pit II: AS


Pasted_graphic_jpg.png


Import Baidu Map API run after the report error about 18 to 20 around, the solution to add libstdc++.6.0.9 library.
After filling out the pit, look at the effect we're going to demonstrate today.


Route plans. gif


The realization of the content is in the positioning of the current city in the path planning, driving, bus, walk three kinds of planning methods.

Note The main points:
    • Here we use the location to the location of the path planning, so we must set up the query in the process of the city, or cannot find success (of course, can also use the way of latitude and longitude, then another said).
    • Mode switching problem, if we want to switch between different path plans as shown, we need to be aware of the existing route trajectories and labeling cleanup issues on the map.
    • We plan the path under different conditions to note that they are different query methods, as well as their different protocol methods, which we can specifically point to Baidu map Static library method to view.
Implementation of the Code

As the project is not sorted out, here we put some major code.
Because three path plans are consistent. Here we take the path planning of driving to do an example.
Because we have to consider whether there are traces and callouts before this, so we have to do the cleanup first, the code is as follows

    [_mapView removeOverlays:_mapView.overlays];    NSArray *annArray = [[NSArray alloc]initWithArray:_mapView.annotations]; [_mapView removeAnnotations: annArray];

Then we create the path planning method

_searcher = [[Bmkroutesearch alloc]init]; _searcher.delegate = self; Initiate search bmkplannode*start = [[Bmkplannode alloc]init];start. name = _startfiled. text; bmkplannode* end = [[Bmkplannode alloc]init]; end. Name = _endfiled. text; start.cityname = self.city; Span class= "Hljs-operator" >end.cityname = self.city; Bmkdrivingrouteplanoption *driveroutesearchoption =[[bmkdrivingrouteplanoption Alloc]init]; Driveroutesearchoption.from = start; driveroutesearchoption.to = end;     

It is important to note that because we are driving path planning, so we create a bmkdrivingrouteplanoption, we enter into the internal method can be seen, into our bus and walk corresponding to their own option, the launch of a successful search will be transferred to his protocol method, The premise here is to follow the Bmkroutesearchdelegate protocol.

- (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error

In the above protocol method, we set the starting point, the end point, and the total number of the segment entry information and each track point

 for (int i =0; i < size; i++) {BMkdrivingstep *tansitstep = [plan. Steps OBJECTATINDEX:I];if (i = =0) {Bmkpointannotation* annotation = [[B]Mkpointannotation Alloc]init]; Annotation. coordinate = Plan. Terminal. location; Annotation. title =@ "starting point"; [_mapview addannotation:annotation]; }Elseif (i = = Size-1) {Bmkpointannotation* annotation = [[B]Mkpointannotation Alloc]init]; Annotation. coordinate = Plan. Terminal. location; Annotation. title =@ "End point"; [_mapview addannotation:annotation]; } Bmkpointannotation* annotation = [[B]Mkpointannotation Alloc]init]; Annotation. coordinate = Tansitstep. entrace. location;Section entrance Information annotation. title = Tansitstep. instruction;The journey is replaced by a description [_mapview addannotation:annotation];Total Trace points Cumulative planpointcounts + = Tansitstep. Pointscount; }Trajectory Point BMkmappoint * temppoints = new BMkmappoint[planpointcounts];File suffix name changed to mmint i =0;for (Int J =0; J < size; J + +) {BMktransitstep *transitstep = [plan.steps objectatindex:j]; int k = 0; for (k = 0; k < Transitstep.x = Transitstep.points[k].x; Temppoints[i].y = Transitstep.points[k].Y; i++; }} //through points build bmkpolyline bmkpolyline *polyline = [Bmkpolyline polylinewithpoints:temppoints count:planpointcounts]; [_mapview Addoverlay:polyline]; //add route overlay delete []temppoints; [self mapviewfitpolyline:polyline];         

This allows us to complete the main setup of the path planning.

POI Search

Baidu Map provides a detailed POI search, such as we set an interface to select what we want to retrieve, and then according to the content of our selection to mark the map of the corresponding points, and show his details. Then we click on the specific cell, then we according to the model of the data location or latitude and longitude, we can plan our location to the destination of the route planning, like driving, bus, walking, three kinds. I have to spit it out here why so many phones are not there.


Img_0003.png
Img_0004.png


We're going to do a POI search, first to follow the Bmkpoisearchdelegate protocol and then retrieve

_searcher2 =[[bMkpoisearch Alloc]init]; _searcher2. Delegate =SelfInitiate search BMknearbysearchoption *option = [[B]Mknearbysearchoption Alloc]init]; Option. PageIndex =1;//Current index page option.pagecapacity = 10 ; //page volume Option.location = Cllocationcoordinate2dmake (self.lat2, self .lon2); Option.keyword = self.STR; Option.radius = 3000; bool flag = [_searcher2 poisearchnearby:option]; if (flag) {nslog (@ "Peripheral Retrieval sent successfully");} else {nslog (@ "Peripheral Retrieval send Failed");}   

After the search succeeds, it goes to the protocol method.

- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error

Here we get an array containing the details, and then we take the UID from the Bmkpoiinfo object, go through the detailed search, and convert it to a model, create our tableview, and present the resulting data to the list in the lower part. The route is planned by passing values in the detailed event of clicking TableView. Some of the properties in model are as follows:

@interfaceGsmapmodel:NSObjectPoi Name@property (Nonatomic,Strong)nsstring* name;Poiuid@property (Nonatomic,strong) nsstring* uid; ///poi address  @property (nonatomic, Span class= "Hljs-keyword" >strong) nsstring* address; POI City  @property (nonatomic, strong) nsstring* City; ///poi phone number  @property (nonatomic , strong) nsstring* phone; Poi zip  @property (nonatomic,  Strong) nsstring* postcode;          

Details to retrieve the corresponding protocol method

detailed results of//search-(void) Ongetpoidetailresult: (bmkpoisearch*) searcher Result: (bmkpoidetailresult*) Poidetailresult ErrorCode: (bmksearcherrorcode) errorcode{ if (ErrorCode = = Bmk_search_no_error) { // The normal result is handled here} else if (ErrorCode = = Bmk_search_ambiguous_keyword) { //When the result is not found in the set up city but results are found in other cities, Callback suggested retrieving city list //result.citylist; NSLog (@ "starting point ambiguous");} else { NSLog (@ "Sorry, no results found");}              
Customization of Pins

We want to define the pin shape we want to see, and we need to customize it. Here is a simple way to handle the pin style, if you want to specifically according to different situations to customize, it is necessary to judge, according to their different circumstances to give the corresponding label picture

-(BMkannotationview *) Mapview: (BMkmapview *) Mapview viewforannotation: (Id<bmkannotation>) annotation{BMkpinannotationview *newannotationview = [[B]Mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:@ "Myannotation"]; Newannotationview. Pincolor = Bmkpinannotationcolorpurple; Newannotationview. Animatesdrop = NO;  Set the callout point to animate Newannotationview. annotation=annotation; Newannotationview. image = [UIImage imagenamed: @ "Mapicon"]; //Replace the pin with another picture Newannotationview. Size = Cgsizemake ( 33); return newannotationview;}                
Postscript

Map We use more is positioning, poi search and path planning, positioning, I believe whether it is the official SDK or all kinds of information on the web is enough detailed, I will no longer repeat, here the main path planning and POI search to make a statement, I hope to help you.

Supplemental content

Today, a small partner asked to see the article want to know Baidu map of the fuzzy search is how to do, enter the place name can automatically Lenovo good appearance, here to add it, in fact, is also part of the POI search, the city search according to the name of the place to launch search, and then display the search content to TableView. Such as:


Fuzzy search. gif


When we change the contents of the input box in fact, what is the internal operation, the following we introduce him a little bit.
Here we are going to do is poi retrieval, so first to follow the Bmkpoisearchdelegate protocol, the code to create the search is as follows:

  _poisearcher = [[BMKPoiSearch alloc]init];    _poisearcher.delegate = self;    BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc]init]; citySearchOption.pageIndex = 0; citySearchOption.pageCapacity = 20; citySearchOption.city= self.city; citySearchOption.keyword = _inputFiled.text;

After the success, we'll go the following protocol method

-(void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode

Here we get the contents of the return, and then take the data inside it into the model to show it to the TableView, but, why is it, because you do not get the results you want, because you do this search is only once, And can not achieve every change in one word to re-search the display location, we have to do the input box processing

  [_inputFiled addTarget:self action:@selector(inputaction:) forControlEvents:UIControlEventEditingChanged];

Add an event to monitor the change of the input box and implement the method.

#pragma mark - 输入框的实时监听-(void)inputaction:(UITextField *)textField{    [self performSelector:@selector(delay) withObject:self afterDelay:0.5];}

Here we listen to the changes in the input box, and then write the corresponding search in the delay method. This realization of the data display, and then click the corresponding cell when the data passed to the last interface OK ah, here for the user experience we can listen to tableview scrolling, when scrolling to let the keyboard of the input box disappear, the code is as follows:

#pragma mark -滚动隐藏键盘-(void)scrollViewDidScroll:(UIScrollView *)scrollView{    [self.view endEditing:YES];}

This allows us to achieve a fuzzy search, is not still relatively simple. Let's add a different question later.

Iospoi Search Detailed Summary

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.