在Xcode中的两个坐标之间绘制一条折线 [英] Draw a polyline between two coordinates in Xcode

查看:50
本文介绍了在Xcode中的两个坐标之间绘制一条折线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在地图上的两个或多个给定坐标之间绘制一条多义线(多义线而不是路线).想象一下,我在地图上有2个掉线的引脚,我需要从第一个掉线的引脚到第二个掉线的引脚绘制一条直线.

I would like to draw a POLY-LINE between two or more given coordinates on the map(poly-line and not the route). Imagine I have 2 dropped pins on the map and I need to draw a straight line from the first dropped pin to the second dropped pin.

推荐答案

在界面文件中

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);

和覆盖代表

- (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坐标的代码.

here is the code to get NsMutableArray of coordinates.

调用此功能

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.

include RegexKitLite.h in your file.

这篇关于在Xcode中的两个坐标之间绘制一条折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆