ios - 在 Xcode 中的两个坐标之间绘制多段线

标签 ios mapkit

我想在 map 上的两个或多个给定坐标之间绘制折线(折线而不是路线)。假设我在 map 上有 2 个掉落的大头针,我需要从第一个掉落的大头针到第二个掉落的大头针画一条直线。

最佳答案

在接口(interface)文件中

MKPolyline* _routeLine;
MKPolylineView* _routeLineView;

在实现文件中

将所有坐标存入

NSMutablrArray *routeLatitudes

然后

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [routeLatitudes count]); 
for(int idx = 0; idx < [routeLatitudes count]; idx++)
{
    CLLocationCoordinate2D workingCoordinate;       
    workingCoordinate.latitude=[[routeLatitudes objectAtIndex:idx] doubleValue];
    workingCoordinate.longitude=[[routeLongitudes objectAtIndex:idx] doubleValue];  
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
    pointArr[idx] = point;      
}   
// create the polyline based on the array of points. 
routeLine = [MKPolyline polylineWithPoints:pointArr count:[routeLatitudes count]];
[mapViewHome addOverlay:self.routeLine];
free(pointArr);

和覆盖委托(delegate)

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
 {
       MKOverlayView* overlayView = nil;

   if(overlay == routeLine)
   {
        routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
        routeLineView.fillColor = [UIColor colorWithRed:0.945 green:0.027 blue:0.957  alpha:1];
        routeLineView.strokeColor = [UIColor colorWithRed:0.945 green:0.027 blue:0.957 alpha:1];
        routeLineView.lineWidth = 4;

        overlayView = routeLineView;
    }
    return overlayView;
 }

希望对你有帮助

编辑代码

这里是获取坐标的 NsMutableArray 的代码。

调用这个函数

NSString * saddr = [NSString stringWithFormat:@"%f,%f",StartCoordinate.latitude, StartCoordinate.longitude];
NSString* daddr = [NSString stringWithFormat:@"%f,%f",EndCoordinate.latitude, EndCoordinate.longitude];
routeLatitudes=[[[self getDirectionRoutesFrom:[saddr copy] to:[daddr mutableCopy]] mutableCopy] retain];

函数定义

 -(NSMutableArray *)getDirectionRoutesFrom:(NSString *)saddr1 to:(NSString *)daddr
{
NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr1, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];    
//NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding   error:nil];
NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];
//NSMutableArray *temparr=[[MapViewController decodePolyLine:[encodedPoints mutableCopy]] retain];
return [[self decodePolyLine:[encodedPoints mutableCopy]] retain];
//return temparr;
}

 -(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                            options:NSLiteralSearch
                              range:NSMakeRange(0,     [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
    NSInteger b;
    NSInteger shift = 0;
    NSInteger result = 0;
    do {
        b = [encoded characterAtIndex:index++] - 63;
        result |= (b & 0x1f) << shift;
        shift += 5;
    } while (b >= 0x20);
    NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lat += dlat;
    shift = 0;
    result = 0;
    do {
        b = [encoded characterAtIndex:index++] - 63;
        result |= (b & 0x1f) << shift;
        shift += 5;
    } while (b >= 0x20);
    NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lng += dlng;
    NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
    NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
    printf("[%f,", [latitude doubleValue]);
    printf("%f]", [longitude doubleValue]);
    CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
    [array addObject:loc];
}

return array;
 }

在您的文件中包含 RegexKitLite.h。

关于ios - 在 Xcode 中的两个坐标之间绘制多段线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11824426/

相关文章:

ios - 使用 MPVolumeView slider 调整音量时隐藏设备音量 HUD View

javascript - SVG onclick 事件在 iOS 设备上未触发

iphone - 用图像覆盖 MKMapView

ios - 适用于 iOS 的离线 MapKit 解决方案

ios - 基于点的聚类计算热图权重

swift - 如何在 MKAnnotation 上显示核心数据中的多个项目

iphone - iFunbox App 安装失败 (-402620395)

iphone - 无法将应用程序上传到 App Store?

iphone - 有什么方法可以将此 map 转换为在 UIMapView 中工作吗?

ios - 绘制完成后清除 CGPath 路径