iOS成長之路 百度地圖覆蓋物

來源:互聯網
上載者:User

標籤:

匯入百度地圖

初始化百度地圖

    mapView  = [[BMKMapView alloc]initWithFrame:(CGRect){0,0,ViewWith,ViewHight}];

    [self.view addSubview:mapView];

mapView.delegate = self;

添加線和座標物

allarray  裡面是 經緯度 

 if (allarray.count > 2) {

        CLLocationCoordinate2D coords[2];

        

        for (NSInteger i = 0; i < (allarray.count - 1); i++) {

            NSString *str = allarray[i];

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

            NSString *lon = temp[0];

            NSString *lat = temp[1];

            BMKPointAnnotation* annotation1 = [[BMKPointAnnotation alloc]init];

            CLLocationCoordinate2D coor1;

            coor1.latitude = [lat doubleValue];

            coor1.longitude = [lon doubleValue];

            annotation1.coordinate = coor1;

            annotation1.title =[NSString stringWithFormat:@"%@",placename[i]];

            [mapView addAnnotation:annotation1];

       

            NSLog(@"%@--%@",lon,lat);

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

            coords[0].latitude = [lat doubleValue];

            coords[0].longitude = [lon doubleValue];

 

            _number = i;

            NSString *str2 = allarray[i + 1];

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

            NSString *lon2 = temp2[0];

            NSString *lat2 = temp2[1];

            NSLog(@"%@--%@",lon2,lat2);

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

            coords[1].latitude = [lat2 doubleValue];

            coords[1].longitude = [lon2 doubleValue];

            

            BMKPolyline *myPolyline = [BMKPolyline polylineWithCoordinates:coords count:2];

            [mapView addOverlay:myPolyline];

            

        }

    }

自訂座標物

這個代理是線的

- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{

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

        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay] ;

        polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];

        polylineView.lineWidth = 5.0;

        

        return polylineView;

    }

    return nil;

}

 

這個代理是座標物的

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation

{

    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {

    

//         UIView *viewForImage=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 132, 64)];

  BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;

//        newAnnotationView.animatesDrop = YES;// 設定該標註點動畫顯示

//        

    

       

   

        UIView *viewForImage=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 45, 45)];

        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 45, 45)];

        [imageview setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",_number-1]]];

        [viewForImage addSubview:imageview];

        

        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(32, 0, 100, 64)];

        [email protected]"陳雙超";

        label.backgroundColor=[UIColor clearColor];

        [viewForImage addSubview:label];

        newAnnotationView.image=[self getImageFromView:viewForImage];

        

        return newAnnotationView;

    }

    return nil;

}

這個是把你定義的view變為圖片 

-(UIImage *)getImageFromView:(UIView *)view{

    UIGraphicsBeginImageContext(view.bounds.size);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}

 

iOS成長之路 百度地圖覆蓋物

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.