IOS high moral map using – search, path planning

Recently, the project has not been updated for a long time. Taking the time to high moral map search and path planning to join up the map function in the
project, the use of high moral map third party, want to achieve to determine a location, route search and display method. It took a lot of effort and it finally came true.

Effect

IOS high moral map using - search, path planning
WeChat_1462507820.jpeg

I. configuration work

1. apply for key

Visit http://lbs.amap.com/dev/key/ on a map third party development platform to apply for a key, registered account, new applications, this is no threshold.
gets this key

IOS high moral map using - search, path planning
screen snapshot 2016-05-06 a.m. 10.34.15.png

reminds you that the key ID corresponding to this bundle is the same as the bundle ID in the project, or every time you open the map, you will be prompted with a Invalid_user_scode.

2. import third parties

For convenience, pod import

Pod'AMap3DMap'#3D SDK #pod'AMap2DMap' #2D map SDK map (2D map and 3D map cannot be used at the same time, 2 1) pod'AMapSearch'# search service SDK
3., open the project background positioning function, change info.plist

An increase of two, a request location prompt, a HTTPS

IOS high moral map using - search, path planning
screen snapshot 2016-05-06 a.m. 11.02.06.png
IOS high moral map using - search, path planning
screen snapshot 2016-05-06 a.m. 10.48.19.png

Two, map basic display

Add a map to the view to display

- (void) viewDidLoad {[super viewDidLoad]; Key sharedServices].apiKey = [MAMapServices / configure user @ "76bb9bc3718375ad03acba7c333694c4"; / / put the map on the underlying [self.view insertSubview:self.mapView atIndex:0];} / / map - lazy loading (MAMapView * mapView) {if (! _mapView) {_mapView = [[MAMapView alloc] initWithFrame:CGRectMake (0, 0, CGRectGetWidth (self.view.bounds). CGRectGetHeight (self.view.bounds))]; _mapView.delegate = self; _mapView.showsUserLocation = YES; //YES for open location, NO [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:NO] closed position; / / location / / custom location map with the longitude circle style _mapView.customizeUserLocationA CcuracyCircleRepresentation = NO; _mapView.userTrackingMode = MAUserTrackingModeFollow / / map tracking mode; / / background localization of _mapView.pausesLocationUpdatesAutomatically = NO; _mapView.allowsBackgroundLocationUpdates = YES; //iOS9 return _mapView system must be equipped with}};

Three, map search function

Comply with protocol AMapSearchDelegate

High German provides a variety of search methods, POI search (keyword query, perimeter search, polygon query), search for real objects in reality.

Prompt search is the search based on existing characters when they are not fully entered

/ / search box is activated, the use of search tips - (void) updateSearchResultsForSearchController: (UISearchController * searchController) {/ / *tipsRequest = input prompt search AMapInputTipsSearchRequest launched [[AMapInputTipsSearchRequest alloc] init]; / / key words tipsRequest.keywords = _searchController.searchBar.text; tipsRequest.city = _currentCity / city; / / [_search AMapInputTipsSearch: tipsRequest] executive search;} / / input prompt callback function (void onInputTipsSearchDone: (AMapInputTipsSearchRequest*) request) response: (AMapInputTipsSearchResponse * response) {if (response.tips.count = = 0) {return}; / / through the AMapInputTipsSearchResponse object to process the search results / first empty array [self.searchList removeAllObjects]; for (AMapTip *p in response.tips) {/ / the search results are an array of [self.searchList addObject:p] _isSelected = NO;}; / / refresh table view [self.tableView reloadData];}

Click on POI search

/ / perimeter search (IBAction) searchAction: - (ID sender) {/ / _search = [[AMapSearchAPI alloc] initialization object retrieval init]; _search.delegate = self; / / AMapPOIAroundSearchRequest object structure, set AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest request parameters around alloc] init]; / / request.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude] current position; / / keyword request.keywords = _searchController.searchBar.text; NSLog (@ "% @" _searchController.searchBar.text, types); / / POI attribute limited search category, the default is: catering service | commercial residential life service | / / POI type is divided into 20 categories, Respectively: / / car service | car sales | | motorcycle auto repair service | catering service | shopping service | service life | leisure service | / health service | accommodation service | scenic | commercial residential | government agencies and social organizations | culture service | / traffic facilities service | financial and insurance services company | the enterprise | Road ancillary facilities | address information | public facilities / / request.types = @ "catering service | service life"; request.radius = 5000; //<! Query radius, range: 0-50000, unit: M = 3000] [default request.sortrule = 0; request.requireExtension = YES; / / [_search AMapPOIAroundSearch:request] launched a search around; / / POI} search the corresponding callback function - (void) onPOISearchDone: (AMapPOISearchBaseRequest *) request respo Nse: (AMapPOISearchResponse * response) {if (response.pois.count = = 0) {return}; / / through the AMapPOISearchResponse object to process the search results [self.dataList removeAllObjects]; for (AMapPOI *p in response.pois (NSLog) {@ "% @" [NSString "%@/nPOI:, stringWithFormat:@, p.description% @,% @", p.name, p.address]); / / there is an array of [self.dataList search results addObject:p];} _isSelected = YES; [self.tableView reloadData];}

Four, path planning

Planning path query (driving route search, bus switching scheme query, walking path retrieval), advance to know the route of travel

Planning / line query - (IBAction) findWayAction: (ID) AMapDrivingRouteSearchRequest object structure sender {/ / set, driving path planning request parameters AMapWalkingRouteSearchRequest *request = [[AMapWalkingRouteSearchRequest alloc] init]; / / set the starting point, I chose the current position, mapView = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude] request.origin this property; / / set the end point, you can choose the hand request.destination = [AMapGeoPoint locationWithLatitude:_destinationPoint.coordinate.latitude longitude:_destinationPoint.coordinate.longitude]; / / request.strategy = 2; / / distance / / request.requireExtension = YES; / / initiate path search After the launch, will implement the proxy method used here is the walking path / / [_search AMapWalkingRouteSearch: request];} / / press gesture response method, choose the path planning of the end point, add their own gestures - (void) handleLongPress: (UILongPressGestureRecognizer * gesture) {if (gesture.state = = UIGestureRecognizerStateBegan) {/ / on the map according to the position of CGPoint P = long [gesture locationInView:_mapView]; NSLog (@ press on (%f,%f), p.x, p.y);} / / convert longitude CLLocationCoordinate2D coordinate = [_mapView convertPoint:[gesture locationInView:_mapView] toCoordinateFromView:_mapView]; / / _destinationPoint = [[MAPointAnnotation is assigned to the target point of alloc] init]; _destinationPoint.coordinate = coordi Nate;}

The agent method is executed after the path search is performed

/ / callback function to achieve the path search - (void) onRouteSearchDone: (AMapRouteSearchBaseRequest * request) response: (AMapRouteSearchResponse * response) {if (response.route = = Nil) {return}; / / the AMapNavigationSearchResponse object processing search results NSString *route = [NSString stringWithFormat:@ Navi:, response.route] AMapPath% @ "; *path = response.route.paths[0]; / / a path selection AMapStep *step = path.steps[0]; / / navigation section array NSLog on this path (@"% @ ", step.polyline); / / this section coordinates the string if (response.count > 0) {/ / remove the original map covering [_mapView removeOverlays:_pathPolylines]; _pathPolylines = nil; / / the only significant shown. Path planning _pathPolylines = [self polylinesForPath:response.route.paths[0]]; NSLog (@ "% @", response.route.paths[0]); / / add a new cover, then triggers the agent method to draw [_mapView addOverlays:_pathPolylines];}}

Every time you add routes, areas, or pins, you trigger the following proxy method

The method is executed when the cover drawing / / - (MAOverlayRenderer *) mapView: (* MAMapView) mapView rendererForOverlay: (ID < MAOverlay> overlay) custom positioning accuracy of the corresponding MACircleView. {/ * * / / / if ([overlay isKindOfClass:[MAPolyline painting line class]]) {/ / initialize a line type view *polygonView = MAPolylineRenderer [[MAPolylineRenderer alloc] / initWithPolyline:overlay]; set the line width color polygonView.lineWidth = 8.f; polygonView.strokeColor = [UIColor colorWithRed: 0.015 green:0.658 blue:0.986 alpha:1.000] [UIColor colorWithRed:0.940 green:0.771 blue:0.143; polygonView.fillColor = alpha:0.800]; polygonView.lineJoinType = kMALineJoinRound; / / connection type / / return view, were added return polygonView return nil;}};

Finally, this is demo, the best testing simulator, the location is not good with
https://github.com/DaLiWangCC/MyOpen