Appearance
在地图上绘制
添加标注
在滴图地图中表示标注的基础数据类型是DMKPointAnnotation,先创建一个DMKPointAnnotation,设定好经纬度和一些其他信息,然后再mapView的回调方法中构造DMKPinAnnotationView,就可以在地图中加上标注
objc
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(40.043616, 116.290276);
DMKPointAnnotation* annotation = [[DMKPointAnnotation alloc]init];
[annotation setCoordinate:location];
annotation.title = @"我是标题";
annotation.subtitle = @"我是描述我是描述";
self.annotation = annotation;
[self.mapView addAnnotation:annotation];代理方法实现:
objc
#pragma mark - MapView Delegate
- (DMKAnnotationView *)mapView:(DMKMapView *)mapView viewForAnnotation:(id<DMKAnnotation>)annotation {
if([annotation isKindOfClass:[DMKPointAnnotation class]]) {
DMKPinAnnotationView *view = [[DMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];
view.imageIcon = [UIImage imageNamed:@"taxi"];
view.mDrawMode = DIMAGE_MIDDLE_CENTER_MODE;
view.canShowCallout = YES;
return view;
}
return nil;
}添加多边形
添加多边形的方法和添加标注类似,表示多边形的基础数据类型是DMKPolygon,先创建一个DMKPolygon,设定好各个顶点的经纬度,添加到mapView,然后再mapView的回调方法中构造DMKPolygonRenderer,设置颜色、线宽等参数,就可以在地图中加上多边形
objc
int count = 10;
CLLocationCoordinate2D *coord = malloc(sizeof(CLLocationCoordinate2D) * count);
coord[0] = CLLocationCoordinate2DMake(40.051879,116.295569);
coord[1] = CLLocationCoordinate2DMake(40.052388,116.297908);
coord[2] = CLLocationCoordinate2DMake(40.049940,116.298638);
coord[3] = CLLocationCoordinate2DMake(40.047789,116.299560);
coord[4] = CLLocationCoordinate2DMake(40.044323,116.301792);
coord[5] = CLLocationCoordinate2DMake(40.042171,116.296363);
coord[6] = CLLocationCoordinate2DMake(40.045128,116.295483);
coord[7] = CLLocationCoordinate2DMake(40.045456,116.296599);
coord[8] = CLLocationCoordinate2DMake(40.047394,116.296942);
coord[9] = CLLocationCoordinate2DMake(40.049530,116.296299);
DMKPolygon* polygon1 = [DMKPolygon polygonWithCoordinates:coord count:count];
[self.mapView addOverlay:polygon1];
free(coord);在回调方法中创建表示多边形的view:
objc
-(DMKOverlayRenderer*)mapView:(DMKMapView *)mapView viewForOverlay:(id<DMKOverlay>)overlay
{
if ([overlay isKindOfClass:[DMKPolygon class]])
{
DMKPolygonRenderer *polygonRenderer = [[DMKPolygonRenderer alloc] initWithPolygon:overlay];
polygonRenderer.lineWidth = 8.f;
polygonRenderer.strokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.6];
return polygonRenderer;
}
return nil;
}绘制线覆盖物(Polyline)
根据道路上的点的数据,可以在地图上绘制路线覆盖物。代码如下:
objc
//构造折线数据对象
CLLocationCoordinate2D coords[4];
coords[0].latitude = 39.832136;
coords[0].longitude = 116.34095;
coords[1].latitude = 39.832136;
coords[1].longitude = 116.42095;
coords[2].latitude = 39.902136;
coords[2].longitude = 116.42095;
coords[3].latitude = 39.902136;
coords[3].longitude = 116.44095;
DMKPolyline *polyline = [DMKPolyline polylineWithCoordinates:coords count:4];
[self.mapView addOverlay:polyline];mapView收到polyline数据后,会通过delegate方法- (DMKOverlayRenderer *)mapView:(DMKMapView *)mapView viewForOverlay:(id <DMKOverlay>)overlay请求调用方构造一个OverlayRenderer用于绘制,这里可以直接使用DMKPolylineRenderer:
objc
- (DMKOverlayRenderer *)mapView:(DMKMapView *)mapView viewForOverlay:(id <DMKOverlay>)overlay {
if ([overlay isKindOfClass:[DMKPolyline class]])
{
DMKPolylineRenderer *polylineRenderer = [[DMKPolylineRenderer alloc] initWithPolyline:overlay];
polylineRenderer.lineWidth = 8.f;
polylineRenderer.strokeColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.6];
return polylineRenderer;
}
return nil;
}绘制圆圈(Circle)
根据一个经纬度数据,设置圆圈半径,可以在地图上绘制圆圈。代码如下:
objc
DMKCircle *circle = [DMKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(40.048126, 116.2814) radius:5000];
self.circle = circle;
[self.mapView addOverlay:circle];mapView收到circle数据后,会通过delegate方法- (DMKOverlayRenderer *)mapView:(DMKMapView *)mapView viewForOverlay:(id <DMKOverlay>)overlay请求调用方构造一个OverlayRenderer用于绘制,这里可以直接使用DMKCircleRenderer:
objc
- (DMKOverlayRenderer *)mapView:(DMKMapView *)mapView viewForOverlay:(id <DMKOverlay>)overlay {
if ([overlay isKindOfClass:[DMKCircle class]])
{
DMKCircleRenderer *circleRenderer = [[DMKCircleRenderer alloc] initWithCircle:overlay];
circleRenderer.lineWidth = 5.f;
circleRenderer.strokeColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];
circleRenderer.fillColor = [UIColor colorWithRed:1.0 green:0.8 blue:0.0 alpha:0.8];
return circleRenderer;
}
return nil;
}其他更多用法请参照地图SDK API文档