Как я могу соединить 2 аннотации внутри Mkmapview цветной линией?
Как соединить 2 аннотации цветной линией внутри MKMapView?
Ответы (3)
API направления Google доступен для этого.. передайте исходное и конечное местоположение
//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];
здесь создайте объект 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);
}
также см. эту ссылку линия-между-двумя-точками-на-карте
Это кажется мне сложным. Я нашел другое решение в Интернете. Где мне сначала нужно добавить наложение в мой 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;}
Но он все равно не появляется, что я делаю не так?