http://lbs.qq.com/ios_v1/guide-2d.html
![](https://img.haomeiwen.com/i1408786/ecceb445d0902fc5.png)
//
// DrawViewController.m
// Tencent_Map_Demo_Raster
//
// Created by WangXiaokun on 16/1/12.
// Copyright © 2016年 WangXiaokun. All rights reserved.
//
#import "DrawOnMapViewController.h"
#import <QMapKit/QMapKit.h>
@interface DrawOnMapViewController ()<QMapViewDelegate>
@end
@interface DrawOnMapViewController()
@property (nonatomic, strong) QMapView *mapView;
@property (nonatomic) BOOL isGraphAdded;
@property (nonatomic, strong) NSArray *graphs;
@end
@implementation DrawOnMapViewController
#pragma mark - init
-(void)initGraph {
//polyline
CLLocationCoordinate2D lineCoords[4];
lineCoords[0] = CLLocationCoordinate2DMake(39.980672,116.308645);
lineCoords[1] = CLLocationCoordinate2DMake(39.968964,116.344185);
lineCoords[2] = CLLocationCoordinate2DMake(39.937964,116.314185);
lineCoords[3] = CLLocationCoordinate2DMake(39.915266,116.324615);
QPolyline *polyLine = [QPolyline polylineWithCoordinates:lineCoords count:4];
//polyline with texture
CLLocationCoordinate2D lineTextureCoords[4];
lineTextureCoords[0] = CLLocationCoordinate2DMake(39.880672,116.308651);
lineTextureCoords[1] = CLLocationCoordinate2DMake(39.868964,116.344185);
lineTextureCoords[2] = CLLocationCoordinate2DMake(39.837964,116.314185);
lineTextureCoords[3] = CLLocationCoordinate2DMake(39.815266,116.324615);
QPolyline *polyLineTexture = [QPolyline polylineWithCoordinates:lineTextureCoords count:4];
//polygon
CLLocationCoordinate2D polygonPeak[3];
polygonPeak[0] = CLLocationCoordinate2DMake(39.969753,116.419373);
polygonPeak[1] = CLLocationCoordinate2DMake(39.935013,116.472932);
polygonPeak[2] = CLLocationCoordinate2DMake(39.890772,116.358948);
QPolygon *polygon = [QPolygon polygonWithCoordinates:polygonPeak count:3];
//circle
QCircle *circle = [QCircle
circleWithCenterCoordinate:CLLocationCoordinate2DMake(39.770749,116.445466)
radius:3000];
_graphs = [NSArray arrayWithObjects:polyLine, polyLineTexture, polygon, circle, nil];
}
-(void)initView {
_mapView = [[QMapView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_mapView];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc]
initWithTitle:@"Add Graph"
style:UIBarButtonItemStylePlain
target:self
action:@selector(barButtonAction:)];
self.navigationItem.rightBarButtonItem = barItem;
_mapView.delegate = self;
}
-(void)barButtonAction:(UIBarButtonItem *) barButtonItem {
_isGraphAdded = !_isGraphAdded;
if (_isGraphAdded) {
barButtonItem.title = @"Rm Graph";
[_mapView addOverlays:_graphs];
} else {
barButtonItem.title = @"Add Graph";
[_mapView removeOverlays:_graphs];
}
}
-(QOverlayView *)mapView:(QMapView *)mapView
viewForOverlay:(id<QOverlay>)overlay {
if ([overlay isKindOfClass:[QPolyline class]]) {
if (overlay.coordinate.latitude == ((id<QOverlay>)(_graphs[0])).coordinate.latitude &&
overlay.coordinate.longitude == ((id<QOverlay>)(_graphs[0])).coordinate.longitude) {
QPolylineView *polylineView = [[QPolylineView alloc] initWithPolyline:overlay];
polylineView.lineWidth = 5.0f;
polylineView.strokeColor = [UIColor redColor];
//虚线
polylineView.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:8] , [NSNumber numberWithInt:8], nil];
return polylineView;
} else {
QStyledPolylineView *polylineView = [[QStyledPolylineView alloc] initWithPolyline:overlay];
polylineView.lineWidth = 2*6.f;
polylineView.symbolGap = 3;
polylineView.borderColor = [UIColor colorWithRed:0x2d/255.0 green:0x71/255.0 blue:0xc4/255.0 alpha:1.0];
[polylineView setStrokeColor:[UIColor colorWithRed:0x57/255.0 green:0xa1/255.0 blue:0xfc/255.0 alpha:1.f]];
polylineView.borderWidth = 2;
{
int width = 32;
int height = 32;
int h = height, w = width;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *data = NULL;//calloc(height * width, 4);
CGContextRef bmpContext = CGBitmapContextCreate(data, width, height, 8, width*4, colorSpace, (CGBitmapInfo)(kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big));
CGContextTranslateCTM(bmpContext, 0.0, height);
CGContextScaleCTM(bmpContext, 1.0, -1.0);
CGContextSetRGBFillColor(bmpContext, 251/255.0, 255/255.0f, 250/255.0, 1.0f);
CGContextSetRGBStrokeColor(bmpContext, 1/255.0, 255/255.0f, 2/255.0, 1.0f);
CGMutablePathRef path = CGPathCreateMutable();
CGPoint spnts[] = {CGPointMake(0.8*w, 0.5*h),
CGPointMake(0.4*w, 0.1*h), CGPointMake(0.3*w, 0.1*h),
CGPointMake(0.6*w, 0.5*h),
CGPointMake(0.3*w, 0.9*h), CGPointMake(0.4*w, 0.9*h)};
CGPathAddLines(path, NULL, spnts, sizeof(spnts)/sizeof(spnts[0]));
CGPathCloseSubpath(path);
CGContextAddPath(bmpContext, path);
CGContextDrawPath(bmpContext, kCGPathFillStroke);
CGPathRelease(path);
CGImageRef cgImage = CGBitmapContextCreateImage(bmpContext);
UIImage *arrow = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
CGColorSpaceRelease(colorSpace);
CGContextRelease(bmpContext);
polylineView.symbolImage = arrow;
if ([[overlay title] isEqualToString:@"lineCap_Butt"]){
polylineView.lineCap = kCGLineCapButt;
}
else if ([[overlay title] isEqualToString:@"lineCap_Round"]){
polylineView.lineCap = kCGLineCapRound;
polylineView.lineDashPhase = 35;
polylineView.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:20], [NSNumber numberWithInt:10], [NSNumber numberWithInt:10], [NSNumber numberWithInt:2], nil];
}
else if ([[overlay title] isEqualToString:@"lineCap_Square"]){
polylineView.lineCap = kCGLineCapSquare;
polylineView.lineDashPhase = 15;
polylineView.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:20], [NSNumber numberWithInt:10], [NSNumber numberWithInt:10], [NSNumber numberWithInt:2], nil];
}
polylineView.isAccessibilityElement = YES;
polylineView.accessibilityLabel = @"polylineview";
return polylineView;
}
if ([[overlay title] isEqualToString:@"lineCap_Butt"]){
polylineView.lineCap = kCGLineCapButt;
}
else if ([[overlay title] isEqualToString:@"lineCap_Round"]){
polylineView.lineCap = kCGLineCapRound;
polylineView.lineDashPhase = 35;
polylineView.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:20], [NSNumber numberWithInt:10], [NSNumber numberWithInt:10], [NSNumber numberWithInt:2], nil];
}
else if ([[overlay title] isEqualToString:@"lineCap_Square"]){
polylineView.lineCap = kCGLineCapSquare;
polylineView.lineDashPhase = 15;
polylineView.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:20], [NSNumber numberWithInt:10], [NSNumber numberWithInt:10], [NSNumber numberWithInt:2], nil];
}
polylineView.isAccessibilityElement = YES;
polylineView.accessibilityLabel = @"polylineview";
return polylineView;
}
}
if ([overlay isKindOfClass:[QPolygon class]]) {
QPolygonView *polygonView = [[QPolygonView alloc] initWithPolygon:overlay];
polygonView.strokeColor = [UIColor colorWithRed:1.0f
green:0.0f
blue:0.0f
alpha:0.5f];
polygonView.fillColor = [UIColor colorWithRed:0.0f
green:1.0f
blue:0.0f
alpha:0.3f];
polygonView.lineWidth = 5.0f;
polygonView.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:8] , [NSNumber numberWithInt:8], nil];
return polygonView;
}
if ([overlay isKindOfClass:[QCircle class]]) {
QCircleView *circleView = [[QCircleView alloc] initWithCircle:overlay];
circleView.strokeColor = [UIColor colorWithRed:1.0f
green:0.0f
blue:0.0f
alpha:0.5f];
circleView.fillColor = [UIColor colorWithRed:0.0f
green:0.0f
blue:0.5f
alpha:0.3f];
circleView.lineWidth = 5.0f;
circleView.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:8] , [NSNumber numberWithInt:8], nil];
return circleView;
}
return nil;
}
#pragma mark - Life circle
-(instancetype)init {
self = [super init];
if (self != nil) {
[self initGraph];
_isGraphAdded = NO;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
网友评论