作者:无敌lili699_461 | 来源:互联网 | 2023-05-19 16:28
地图应用经常会涉及到线路的绘制问题,ios下可以使用MKMapView进行地图开发,使用MKOverlayView进行线路的绘制。
使用MKMapView添加MKMap.framework 和CoreLocation.framework并导入MapKit.h头文件。
新建一个基于视图的工程,修改头文件:
-
-
-
-
-
-
-
-
- #import
- #import
- #import "CloMKAnnotation.h"
- @interface CloViewController : UIViewController{
- MKMapView *cloMapView;
- MKPolyline *routeLine;
- }
-
- @property (nonatomic, strong) NSMutableArray *locations;
- @end
修改实现代码,在.m中添加如下代码:
-
-
-
-
-
-
-
-
- #import "CloViewController.h"
-
- @interface CloViewController ()
-
- @end
-
- @implementation CloViewController
- @synthesize locations;
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- cloMapView = [[MKMapView alloc] initWithFrame:[self.view bounds]];
- [cloMapView setMapType:MKMapTypeHybrid];
- [cloMapView setShowSUSErLocation:YES];
- [cloMapView setDelegate:self];
-
- CLLocationManager *locationManager = [[CLLocationManager alloc] init];
-
- [locationManager setDelegate:self];
- [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
-
- [locationManager setDistanceFilter:100.f];
-
-
-
- locations = [[NSMutableArray alloc] init];
- float latitude = 39.8127;
- float longitude = 116.2967;
- for (int i = 0; i < 10; i++) {
-
- [locations addObject:[NSString stringWithFormat:@"%f,%f", latitude + 0.01*i, longitude + 0.01*i]];
-
- }
-
-
- CLLocationCoordinate2D coords;
- coords.latitude = 39.9127;
- coords.longitude = 116.3967;
- float zoomlevel = 0.22;
- MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomlevel, zoomlevel));
- [cloMapView setRegion:[cloMapView regionThatFits:region] animated:YES];
-
- [cloMapView addOverlay:[self makePolylineWithLocations:locations]];
- [self.view addSubview:cloMapView];
-
- }
-
- - (void)viewDidUnload
- {
- [super viewDidUnload];
-
- cloMapView = nil;
- }
-
- - (void)dealloc{
- [cloMapView release];
- [super dealloc];
- }
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- }
-
-
-
- - (void)showActionSheet :(id)sender{
- UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil
- delegate:self
- cancelButtonTitle:@"取消"
- destructiveButtonTitle:@"添加足迹"
- otherButtonTitles:@"分享",@"详细",@"删除", nil];
- [actionSheet setDelegate:self];
- [actionSheet showInView:self.view];
- [actionSheet release];
- }
-
-
- - (MKPolyline *)makePolylineWithLocations:(NSMutableArray *)newLocations{
- MKMapPoint *pointArray = malloc(sizeof(CLLocationCoordinate2D)* newLocations.count);
- for(int i = 0; i < newLocations.count; i++)
- {
-
- NSString* currentPointString = [newLocations objectAtIndex:i];
- NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
- CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];
-
- CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];
- CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
-
-
- if (i == 0 || i == locations.count - 1) {
- CloMKAnnotation *ann = [[CloMKAnnotation alloc] init];
- [ann setCoordinate:coordinate];
- [ann setTitle:[NSString stringWithFormat:@"纬度:%f", latitude]];
- [ann setSubtitle:[NSString stringWithFormat:@"经度:%f", longitude]];
- [cloMapView addAnnotation:ann];
- }
- pointArray[i] = MKMapPointForCoordinate(coordinate);
- }
-
- routeLine = [MKPolyline polylineWithPoints:pointArray count:newLocations.count];
- free(pointArray);
- return routeLine;
- }
- #pragma mark-
- #pragma CLLocationManager delegate method
-
- - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
-
-
- NSLog(@"oldLocation:%@", [oldLocation description]);
- NSLog(@"newLocation:%@", [newLocation description]);
- NSLog(@"distance:%@", [newLocation distanceFromLocation:oldLocation]);
-
- [locations addObject:[NSString stringWithFormat:@"%f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude]];
-
- [cloMapView removeOverlay:routeLine];
- [cloMapView addOverlay:[self makePolylineWithLocations:locations]];
-
- }
-
- #pragma MKMapView delegate method
-
- - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{
- if (![annotation isKindOfClass:[CloMKAnnotation class]]) {
- return nil;
- }
- static NSString *identifier = @"Annotation";
- MKPinAnnotationView *pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
- if (pinAnnotationView == nil) {
- pinAnnotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
- }
- pinAnnotationView.animatesDrop = YES;
- pinAnnotationView.canShowCallout = YES;
- pinAnnotationView.draggable = YES;
- UIButton *detailBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
- [detailBtn addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
- pinAnnotationView.rightCalloutAccessoryView = detailBtn;
-
- return pinAnnotationView;
- }
-
- - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState{
-
- }
-
-
- - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay{
- NSLog(@"return overLayView...");
- if ([overlay isKindOfClass:[MKPolyline class]]) {
- MKPolylineView *routeLineView = [[[MKPolylineView alloc] initWithPolyline:routeLine] autorelease];
- routeLineView.strokeColor = [UIColor blueColor];
- routeLineView.lineWidth = 3;
- return routeLineView;
- }
- return nil;
- }
-
- @end
这里主要是为了测试,初始时 locations坐标点自定义的,实际中是根据用户的位置动态生成的一系列坐标点。具体可在
- - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
函数中实现,如上代码。
另外用户的实时位置可用
- cloMapView.userLocation = newLocation进行设置,然后显示在地图上。
效果图如下: