如何用彩色线连接 Mkmapview 内的 2 个注释?
问问题
2021 次
3 回答
1
谷歌方向api可用于那个..传递起点和目的地位置
//http://maps.googleapis.com/maps/api/directions/jsonorigin=origin_place&destination=destination_place&waypoints=Charlestown,MA|Lexington,MA&sensor=false
它将给出您解析和拉取坐标的 JSON 响应。划一条线
self.routeLine = [MKPolyline polylineWithPoints:arrPoints count:routeArray.count];
于 2012-11-07T12:35:38.450 回答
0
这里创建创建对象 routeView 作为UIImageView
类和 lineColor 作为UIColor
.h 文件中的类,如下所示
UIImageView* routeView;
UIColor* lineColor;
在viewDidLoad:
方法中编写此代码后..
routeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
routeView.userInteractionEnabled = NO;
lineColor = [UIColor magentaColor];
[mapView addSubview:routeView];
然后在你想画线时更新..也是routes
我们NSMutableArray
存储CLLocation
的地方(经纬度)
-(void) updateRouteView
{
CGContextRef context = CGBitmapContextCreate(nil,
routeView.frame.size.width,
routeView.frame.size.height,
8,
4 * routeView.frame.size.width,
CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedLast);
CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 3.0);
for(int i = 0; i < routes.count; i++)
{
CLLocation* location1 = [routes objectAtIndex:i];
CGPoint point = [mapView convertCoordinate:location1.coordinate toPointToView:routeView];
if(i == 0)
{
CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
}
else
{
CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
}
}
CGContextStrokePath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
UIImage* img = [UIImage imageWithCGImage:image];
routeView.image = img;
CGContextRelease(context);
}
于 2012-11-07T13:03:20.477 回答
0
这对我来说看起来很复杂。我在网上找到了另一个解决方案。我首先必须将叠加层添加到我的 mapView
CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] = start_location;
coordinateArray[1] = end_location;
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
/***********************************************/
[_mapView addOverlay:self.routeLine];
然后我将我的视图控制器设置为 mkmapviewdelegate 并实现委托方法:
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
if(overlay == self.routeLine)
{
if(nil == self.routeLineView)
{
self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 5;
}
return self.routeLineView;
}
return nil;}
但它仍然没有出现,我做错了什么?
于 2012-11-07T13:15:04.377 回答