位置與地圖(三)給地圖添加覆蓋層

來源:互聯網
上載者:User

標籤:地圖   覆蓋層   

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];    self.mapView.mapType = MKMapTypeStandard;    self.mapView.scrollEnabled = YES;    //  設定地圖不可旋轉    self.mapView.rotateEnabled = NO;    self.mapView.zoomEnabled = YES;    self.mapView.showsUserLocation = YES;        //  設定地圖中心的經緯度    CLLocationCoordinate2D center = {39.910650,116.47030};    //  設定地圖顯示的範圍,數值越小細節越清楚    MKCoordinateSpan span = {0.01,0.01};    //  二者合一設定顯示地區    MKCoordinateRegion region = {center,span};    [self.mapView setRegion:region animated:YES];    [self.view addSubview:self.mapView];        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];    [self.mapView addGestureRecognizer:longGesture];        //  設定代理    self.mapView.delegate = self;    }- (void)longPress:(UILongPressGestureRecognizer *)longGesture{        //  擷取長按點得座標    CGPoint postion = [longGesture locationInView:self.mapView];    //  將長按點座標轉換為經緯度    CLLocationCoordinate2D coord2D = [self.mapView convertPoint:postion toCoordinateFromView:self.mapView];    //  建立一個圓形覆蓋層對象    MKCircle *circle = [MKCircle circleWithCenterCoordinate:coord2D radius:100];    //  將單個的覆蓋層添加到指定的層級    [self.mapView addOverlay:circle level:MKOverlayLevelAboveLabels];    }//  該方法返回的MKOverlayRenderer負責繪製覆蓋層控制項- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{    MKCircle *circle = (MKCircle *)overlay;    // iOS7之後,推薦使用MKXxxRenderer來負責渲染覆蓋層控制項    MKCircleRenderer *circleRend = [[MKCircleRenderer alloc] initWithCircle:circle];    circleRend.alpha = 0.3;    //  填充顏色    circleRend.fillColor = [UIColor blueColor];    //  邊框顏色    circleRend.strokeColor = [UIColor redColor];        return circleRend;}/*********iOS7新增的MKTileOverlay覆蓋層*******************************************- (void)longPress:(UILongPressGestureRecognizer *)longGesture{        //  指定本地圖片覆蓋    NSURL *url = [[NSBundle mainBundle] URLForResource:@"hmt" withExtension:@"png"];    MKTileOverlay *tileOverlay = [[MKTileOverlay alloc] initWithURLTemplate:[url description]];    [self.mapView addOverlay:tileOverlay];    }//  該方法返回的MKOverlayRenderer負責繪製覆蓋層控制項- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{        MKTileOverlayRenderer *tileRender = [[MKTileOverlayRenderer alloc] initWithOverlay:(MKTileOverlay *)overlay];    tileRender.alpha = 0.2;    return circleRend;}*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.