Recently developed a project similar to Mobike car rental. Mapping is a big part of it. The project uses amap. This is the first time for me to contact map development. I will briefly describe the basic development of Amap based on my own development experience and the guidance of my predecessors.
Take a look at the renderings first
A third party framework has been added
RESideMenu is a frame similar to QQ sideslip. At present, the mainstream car rental software is this sideslip effect.
All right, map access
1. Preparation
- Autonavi development platform application key corresponding
- Import the corresponding package, either manually dragging in the project or using Cocoapods. The second option is recommended.
- Configure related information. InfoPlist add
0DFB1871-DCFA-47CA-AF53-C902400EEA0D.png
The documentation is very clear, just follow the documentation.
- Register the map key in the AppDelegate
[[AMapServices sharedServices] setApiKey:mapKey]; [[AMapServices sharedServices] setEnableHTTPS:YES];Copy the code
-
RESideMenu access
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] init]]; MenuViewController *menuViewController = [[MenuViewController alloc] init]; RESideMenu *sideMenuViewController = [[RESideMenu alloc] initWithContentViewController:navigationController leftMenuViewController:menuViewController rightMenuViewController:nil]; sideMenuViewController.backgroundImage = [UIImage imageNamed:@"timg"]; self.window = [[UIWindow alloc] init]; self.window.frame = [UIScreen mainScreen].bounds; self.window.rootViewController = sideMenuViewController; [self.window makeKeyAndVisible];Copy the code
2. Map initialization, corresponding attributes can be seen in the documentation.
- (MAMapView *)mapView { if(! _mapView) { _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds]; self.mapView.centerCoordinate = CLLocationCoordinate2DMake(22.547.114.085947); _mapView.showsCompass = NO; _mapView.mapType = MAMapTypeStandard; _mapView.showsScale = NO; _mapView.showsUserLocation = YES; _mapView.userTrackingMode = MAUserTrackingModeNone; _mapView.delegate = self; _mapView.rotateEnabled = YES; _mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [_mapView setZoomLevel:(17.2f) animated:YES]; _mapView.customizeUserLocationAccuracyCircleRepresentation = YES; _mapView.centerCoordinate = CLLocationCoordinate2DMake(22.547.114.085947); } return _mapView; }Copy the code
3. Implementation of proxy method
This proxy method is called when pins are added to the map. Add a few pins and the method will be called a few times. In this method you can customize the pins you want to display. Custom pins work the same way as custom TabelViewCells. CustomAnnotation is a model that includes attributes of a pin that can be set to control the presentation of the pin.
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation { if ([annotation isKindOfClass:[CustomAnnotation class]]) { CustomAnnotationView *annoView = [CustomAnnotationView annotationViewWithMap:mapView]; annoView.canShowCallout= YES; annoView.draggable = YES; annoView.annotation = annotation; return annoView; } return nil; }Copy the code
This method is called when the location changes. In this method, requests can be made, for example, based on the latitude and longitude of the location. Or reverse geocoding the longitude and latitude of the location to obtain the corresponding city name and other information. To improve performance, location is turned off each time a certain bit succeeds.
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation { if (updatingLocation) { CLLocation *newLocation = userLocation.location; // Determine the time NSTimeInterval locationAge = [newLocation.timestamp timeIntervalSinceNow]; if (locationAge > 5.0) { return; } // Check whether the horizontal accuracy is valid if(newLocation.horizontalAccuracy >0 && newLocation.horizontalAccuracy < 150) { CLLocationCoordinate2D myCoorDinate = [newLocation coordinate]; _mapView.centerCoordinate = myCoorDinate; _mapView.showsUserLocation = NO; [self addAnnotationWithLatitude:myCoorDinate.latitude Longitude:myCoorDinate.longitude]; // Reverse geocoding, according to the latitude and longitude of the location into the city street name and other information [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *placemark = [placemarks firstObject]; NSLog(Name = %@ Locality = %@", placemark.name, placemark.locality); userLocation.title = placemark.name; userLocation.subtitle = placemark.locality; }]; }}}Copy the code
Pin click. There are two main issues mentioned here.
- Click the pin to push to a new page, and when it comes back, clicking again has no effect. My solution here is to customize the pin, add an ImageView, and click through gestures. Because when the pin is clicked, the Selected property is set to YES. Alternatively, when clicked, set the selected property to NO. If you have a better solution, please leave a comment.
- Question from a friend: Click the pin to push to a new page, when you return, the original pin has become the system pin. The problem remains to be solved.
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view { // Set the pin click effect, here is zoom [self praiseAnimateWithView:view]; NSArray *array = [NSArray arrayWithArray:_pointArr]; int j = 0; for (int i = 0; i < _pointArr.count; i++) { if (view.annotation.coordinate.latitude == ((MAPointAnnotation*)array[i]).coordinate.latitude) { NSLog(@"Click on the longitude coordinate: %f",view.annotation.coordinate.latitude); }}}Copy the code
4. To summarize
First time blogging. If there is a bad place, warmly welcome to ridicule. Think of it as a note to yourself. I’ll go further
Map navigation and retrieval functions
.