zoukankan      html  css  js  c++  java
  • [ios]MKMapView中使用MKPolyline画线

    参考:http://blog.sina.com.cn/s/blog_9e8867eb0101dt76.html

    首先在MapView.h中

    #import <MapKit/MapKit.h>
    
    @interface MapView : UIView<MKMapViewDelegate> {
    
    	MKMapView* mapView;
    
    }
    
    @property (nonatomic, retain) MKMapView* mapView;
    -(void) drawline: (NSArray*)nowRoadArrary;
    @end
    

    在MapView.m 中

    #import "MapView.h"
    
    
    @implementation MapView
    
    @synthesize mapView;
    -(void) drawline: (NSArray*)nowRoadArrary
    {
     //  将array中的信息点转换成CLLocationCoordinate2D数组
        CLLocationCoordinate2D coords[nowRoadArrary.count];
    
        int i = 0;
        for (NSDictionary *newDic in nowRoadArrary) {
            CLLocationCoordinate2D annotationCoord;
               id lat =  [newDic objectForKey:@"latitude"];
            annotationCoord.latitude = [lat doubleValue];
            annotationCoord.longitude = [[newDic objectForKey:@"longitude"] doubleValue];
            coords[i] = annotationCoord;
            i++;
        }
    
    //用MKPolyline画线并作为overlay添加进mapView
        MKPolyline *cc = [MKPolyline polylineWithCoordinates:coords count:nowRoadArrary.count];
         [mapView addOverlay:cc];
    }
    
    //完成MapView的delegate
    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
    {
        if ([overlay isKindOfClass:[MKPolyline class]])
        {
          //如果有MKPloylineView的overlay,将其画出
            MKPolylineView *lineview=[[MKPolylineView alloc] initWithOverlay:overlay] ;
            //路线颜色
            lineview.strokeColor=[UIColor colorWithRed:69.0f/255.0f green:212.0f/255.0f blue:255.0f/255.0f alpha:0.9];
            lineview.lineWidth=8.0;
            return lineview;
        }
        return nil;
    }
    

     调用MapView

    - (void)viewDidLoad
    {
    //初始化
        [super viewDidLoad];
        MapView  *mapView = [[MapView alloc] initWithFrame:
                   CGRectMake(0, 244, self.view.frame.size.width,800)] ;
    	[self.view addSubview:mapView];
    
    //调用
        NSDictionary *myplace[4];
    
        myplace[0] = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"32.0816",@"latitude",
                    @"118.892",@"longitude",
                    nil];
    
        myplace[1] = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"32.0856",@"latitude",
                      @"118.892",@"longitude",
                      nil];
        myplace[2] = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"32.0716",@"latitude",
                      @"118.92",@"longitude",
                      nil];
        myplace[3] = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"32.0766",@"latitude",
                      @"118.982",@"longitude",
                      nil];
    
        NSArray *ary = [[NSArray alloc]initWithObjects:myplace[0],myplace[1],myplace[2],myplace[3], nil];
    
        [mapView drawline:ary];
    }
    
  • 相关阅读:
    xml解析
    File
    IO操作
    Json解析
    JNI字段描述符
    Android JNI get Context
    快速排序
    Android Scroller与computeScroll的调用机制关系
    Android 更新视图函数ondraw() 和dispatchdraw()的区别
    Android Studio 两个包里的类冲突
  • 原文地址:https://www.cnblogs.com/lyggqm/p/4704875.html
Copyright © 2011-2022 走看看