iOS 根据两个或多个经纬度在地图画线


#import <MapKit/MapKit.h>// 引入地图框架


<CLLocationManagerDelegate,MKMapViewDelegate>


@property (strong, nonatomic) MKPolyline *myPolyline;

@property(nonatomic, strong) MKMapView *mapView;



- (void)drowLine {

    

    

    //初始化地图视图

    

    self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    

    //地图的代理方法

    

    self.mapView.delegate = self;

    

    //是否显示当前的位置

    

    self.mapView.showsUserLocation = YES;

    

    //地图的类型, iOS开发中自带的地图

    

    //使用第三方的地图可以查找周边环境的餐馆,学校之类的

    

    /*

     

     MKMapTypeStandard 标准地图

     

     MKMapTypeSatellite 卫星地图

     

     MKMapTypeHybrid 混合地图

     

     */

    

    self.mapView.mapType = MKMapTypeStandard;

    

    [self.view addSubview:self.mapView];

    

    NSString *thePath = @"106.73293,10.79871|106.73229,10.79841|106.7318,10.79832|106.73164,10.79847|106.73156,10.7988|106.73106,10.79886|106.73057,10.79877|106.73002,10.79866|106.72959,10.79875|106.72935,10.7992|106.7293,10.79971|106.72925,10.80015|106.72942,10.80046|106.72981,10.80058|106.73037,10.8007|106.73067,10.80072|106.7311,10.80076|106.7315,10.80079|106.73194,10.80082|106.73237,10.80086|106.73265,10.80098|106.73269,10.80153|106.7327,10.80207|106.73257,10.80243|106.73718,10.79941|106.73445,10.79946|106.73144,10.79885|106.72987,10.8005|106.73192,10.79991|106.72383,10.79827|106.71543,10.80086|106.70957,10.80121|106.70507,10.79834|106.70121,10.79432|106.69603,10.79158|106.69322,10.78911|106.69196,10.78785|106.68768,10.78355|106.68539,10.7812|106.68336,10.7791|106.67048,10.78377|106.64864,10.78319|106.6499,10.77949|106.63697,10.77439|106.6447,10.77936|106.65804,10.76279|106.66792,10.76805|106.68191,10.77516|106.68336,10.77241|106.68319,10.77622|106.67482,10.78149|106.67095,10.78193|106.65217,10.78641|";

    

//    thePath = @"106.73293,10.79871|106.73229,40.79841|";

    

    NSArray *array = [thePath componentsSeparatedByString:@"|"];

    CLLocationCoordinate2D pointToUse[2];

    

    for (NSInteger i = 0; i < (array.count - 2); i++) {

        NSString *str = array[i];

        NSArray *temp = [str componentsSeparatedByString:@","];

        

        NSString *lon = temp[0];

        NSString *lat = temp[1];

        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([lat doubleValue], [lon doubleValue]);

        pointToUse[0] = coordinate;

        

        NSString *str2 = array[i + 1];

        NSArray *temp2 = [str2 componentsSeparatedByString:@","];

        NSString *lon2 = temp2[0];

        NSString *lat2 = temp2[1];

        CLLocationCoordinate2D coordinate2 = CLLocationCoordinate2DMake([lat2 doubleValue], [lon2 doubleValue]);

        pointToUse[1] = coordinate2;

        

        self.myPolyline = [MKPolyline polylineWithCoordinates:pointToUse count:2];

        [self.mapView addOverlay:self.myPolyline];

    }

    

}


- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {

    if ([overlay isKindOfClass:[MKPolyline class]]) {

        MKPolylineView *lineview = [[MKPolylineView alloc] initWithOverlay:overlay];

        lineview.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];

        lineview.lineWidth = 10;

        return lineview;

    }

    return nil;

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值