导入百度地图
初始化百度地图
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)];
label.text=@"陈双超";
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;
}