在mapview iOS 6中绘制多段线

在mapview iOS 6中绘制多段线,ios,objective-c,mapkit,mkannotation,mkpolyline,Ios,Objective C,Mapkit,Mkannotation,Mkpolyline,嗨,我正在做一个应用程序,必须显示路线地图。我解析了一个Json以获得绘制多段线的点。我在网上找到了一个代码来画这条多段线。我在这个链接上找到的代码: 上面写着“创建MKPolyline注释”,我试图在我的应用程序中导入它,但在创建坐标数组时遇到了问题。我的方法是: - (void)createMKpolylineAnnotation { NSInteger numberOfSteps = self.path.count; CLLocationCoordinate2D *coords = ma

嗨,我正在做一个应用程序,必须显示路线地图。我解析了一个Json以获得绘制多段线的点。我在网上找到了一个代码来画这条多段线。我在这个链接上找到的代码:

上面写着“创建MKPolyline注释”,我试图在我的应用程序中导入它,但在创建坐标数组时遇到了问题。我的方法是:

- (void)createMKpolylineAnnotation {
NSInteger numberOfSteps = self.path.count;

CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * numberOfSteps);
for (NSInteger index = 0; index < numberOfSteps; index++) {
    CLLocation *location = [self.path objectAtIndex:index];
    CLLocationCoordinate2D coordinate = location.coordinate;
    coords[index] = coordinate;
}

MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coords count:numberOfSteps];
[self.mapView addOverlay:polyLine];
}
-(void)createMKpolylineAnnotation{
NSInteger numberOfSteps=self.path.count;
CLLocationCoordinate2D*coords=malloc(sizeof(CLLocationCoordinate2D)*步数);
对于(NSInteger index=0;index
当我试着看coords的值是第一次设定的,为什么? 你能帮我解决这个问题吗?还是我应该换一种方式

我在这里发布了处理地图视图的视图控制器的代码

MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@property (nonatomic, strong) NSString *fromCity;
@property (nonatomic, strong) NSString *toCity;

- (IBAction)chooseKindOfMap:(id)sender;
@end
#导入
#进口
@接口MapViewController:UIViewController
@属性(弱,非原子)IBMMapView*mapView;
@属性(非原子,强)NSString*fromCity;
@属性(非原子,强)NSString*toCity;
-(iAction)选择KindofMap:(id)发件人;
@结束
MapViewController.m

#import "MapViewController.h"
#import "AppDelegate.h"
#import "PlaceAnnotation.h"

@interface MapViewController ()

@property (nonatomic, strong)NSMutableArray *mapAnnotation;
@property (nonatomic) BOOL needUpdateRegion;
@property (nonatomic, strong)NSMutableArray *path;

@end

@implementation MapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self parseGoogleJsonToObtainPointsForPolyline];
    [self.mapView setDelegate:self];
    self.needUpdateRegion = YES;
    //[self centerMap];
    self.mapAnnotation = [[NSMutableArray alloc]initWithCapacity:2];
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSLog(@"%d", appDelegate.dataForMap.count);
    NSArray* coords = [self getCoords:appDelegate.dataForMap];
    NSLog(@"coords = %@", coords);
    PlaceAnnotation *fromPlace = [[PlaceAnnotation alloc] initWithCoordinateAndName:coords[0] andLong:coords[1] andName:self.fromCity];
    PlaceAnnotation *toPlace = [[PlaceAnnotation alloc] initWithCoordinateAndName:coords[2] andLong:coords[3] andName:self.toCity];

    [self.mapAnnotation insertObject:fromPlace atIndex:0];
    [self.mapAnnotation insertObject:toPlace atIndex:1];
    NSLog(@"mapAnnotation.count: %d", self.mapAnnotation.count);

    if (self.mapAnnotation) {
        [self.mapView removeAnnotations:self.mapView.annotations];
    }

    [self.mapView addAnnotation:self.mapAnnotation[0]];
    [self.mapView addAnnotation:self.mapAnnotation[1]];
    NSLog(@"MapAnnotation = %@", self.mapView.annotations);
    [self updateRegion];
    [self createMKpolylineAnnotation];
}

//- (void)viewDidAppear:(BOOL)animated {
//    [super viewDidAppear:animated];
//    if (self.needUpdateRegion) [self updateRegion];
//}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    pin.pinColor = MKPinAnnotationColorRed;
    return pin;
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    MKPinAnnotationView *ulv = [mapView viewForAnnotation:mapView.userLocation];
    ulv.hidden = YES;
}

- (NSArray*)getCoords:(NSDictionary*)data {
    NSArray *legs = [data objectForKey:@"legs"];
    NSDictionary *firstZero = [legs objectAtIndex:0];
    NSDictionary *endLocation = [firstZero objectForKey:@"end_location"];
    NSDictionary *startLocation = [firstZero objectForKey:@"start_location"];
    NSString *latFrom = [startLocation objectForKey:@"lat"];
    NSString *lngFrom = [startLocation objectForKey:@"lng"];
    NSString *latTo = [endLocation objectForKey:@"lat"];
    NSString *lngTo = [endLocation objectForKey:@"lng"];
    return @[latFrom,lngFrom,latTo,lngTo];
}

- (void)centerMap {
    MKCoordinateRegion region;
    region.center.latitude = 41.178654;
    region.center.longitude = 11.843262;
    region.span.latitudeDelta = 11.070406;
    region.span.longitudeDelta = 12.744629;

    [self.mapView setRegion:region];
}

- (IBAction)chooseKindOfMap:(id)sender {
    if ([sender tag] == 0) {
        self.mapView.mapType = MKMapTypeStandard;
    }
    if ([sender tag] == 1) {
        self.mapView.mapType = MKMapTypeSatellite;
    }
    if ([sender tag] == 2) {
        self.mapView.mapType = MKMapTypeHybrid;
    }
}

- (void)updateRegion
{
    self.needUpdateRegion = NO;
    CGRect boundingRect;
    BOOL started = NO;
    for (id <MKAnnotation> annotation in self.mapView.annotations) {
        CGRect annotationRect = CGRectMake(annotation.coordinate.latitude, annotation.coordinate.longitude, 0, 0);
        if (!started) {
            started = YES;
            boundingRect = annotationRect;
        } else {
            boundingRect = CGRectUnion(boundingRect, annotationRect);
        }
    }
    if (started) {
        boundingRect = CGRectInset(boundingRect, -0.2, -0.2);
        if ((boundingRect.size.width < 20) && (boundingRect.size.height < 20)) {
            MKCoordinateRegion region;
            region.center.latitude = boundingRect.origin.x + boundingRect.size.width / 2;
            region.center.longitude = boundingRect.origin.y + boundingRect.size.height / 2;
            region.span.latitudeDelta = boundingRect.size.width;
            region.span.longitudeDelta = boundingRect.size.height;
            [self.mapView setRegion:region animated:YES];
        }
    }
}

- (void)parseGoogleJsonToObtainPointsForPolyline {
    NSDictionary *polyline;
    NSMutableArray *points = [[NSMutableArray alloc]init];;

    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSArray *legs = [appDelegate.dataForMap objectForKey:@"legs"];
    NSDictionary *firstZero =[legs objectAtIndex:0];
    NSArray *steps = [firstZero objectForKey:@"steps"];
    for (int i = 0; i < steps.count; i++) {
        polyline = [steps[i] objectForKey:@"polyline"];
        [points addObject:polyline[@"points"]];
        NSLog(@"POINTS = %@", polyline[@"points"]);
        self.path = [self decodePolyLine:points[i]];
    }
    NSLog(@"path = %@", self.path);
}

-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr {
    NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
    [encoded appendString:encodedStr];
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                options:NSLiteralSearch
                                  range:NSMakeRange(0, [encoded length])];
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSMutableArray *array = [[NSMutableArray alloc] init];
    NSInteger lat=0;
    NSInteger lng=0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
        NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];

        CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
        [array addObject:location];
    }

    return array;
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor redColor];
    polylineView.lineWidth = 1.0;

    return polylineView;
}

- (void)createMKpolylineAnnotation {
    NSInteger numberOfSteps = self.path.count;

    CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * numberOfSteps);
    for (NSInteger index = 0; index < numberOfSteps; index++) {
        CLLocation *location = [self.path objectAtIndex:index];
        CLLocationCoordinate2D coordinate = location.coordinate;
        coords[index] = coordinate;
    }

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coords count:numberOfSteps];
    [self.mapView addOverlay:polyLine];
}
@end
#导入“MapViewController.h”
#导入“AppDelegate.h”
#导入“PlaceAnnotation.h”
@接口MapViewController()
@属性(非原子,强)NSMutableArray*mapAnnotation;
@属性(非原子)布尔需要更新区域;
@属性(非原子,强)NSMutableArray*路径;
@结束
@MapViewController的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
[self-ParseGoogleJSOnToActainPointsForpolyLine];
[self.mapView setDelegate:self];
self.needUpdateRegion=是;
//[自我中心地图];
self.mapAnnotation=[[NSMutableArray alloc]initWithCapacity:2];
AppDelegate*AppDelegate=(AppDelegate*)[[UIApplication sharedApplication]委托];
NSLog(@“%d”,appDelegate.dataForMap.count);
NSArray*coords=[self-getCoords:appDelegate.dataForMap];
NSLog(@“coords=%@),coords);
PlaceAnnotation*fromPlace=[[PlaceAnnotation alloc]initwithCoordinatedName:coords[0]和Long:coords[1]和Name:self.fromCity];
PlaceAnnotation*toPlace=[[PlaceAnnotation alloc]initwithCoordinatedName:coords[2]和long:coords[3]和name:self.toCity];
[self.mapAnnotation insertObject:fromPlace atIndex:0];
[self.mapanotation insertObject:toPlace atIndex:1];
NSLog(@“mapAnnotation.count:%d”,self.mapAnnotation.count);
if(self.mapAnnotation){
[self.mapView removeAnnotations:self.mapView.annotations];
}
[self.mapView addAnnotation:self.mapAnnotation[0]];
[self.mapView addAnnotation:self.mapAnnotation[1]];
NSLog(@“MapAnnotation=%@”,self.mapView.annotations);
[自我更新区域];
[self createMKpolylineAnnotation];
}
//-(无效)视图显示:(BOOL)动画{
//[超级视图显示:动画];
//if(self.needUpdateRegion)[self-updateRegion];
//}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
MKPinAnnotationView*pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:nil];
pin.pinColor=MKPinAnnotationColorRed;
回位销;
}
-(void)地图视图:(MKMapView*)地图视图添加注释视图:(NSArray*)视图{
MKPinAnnotationView*ulv=[mapView视图用于注释:mapView.userLocation];
ulv.hidden=是;
}
-(NSArray*)getCoords:(NSDictionary*)数据{
NSArray*legs=[data objectForKey:@“legs”];
NSDictionary*firstZero=[legs objectAtIndex:0];
NSDictionary*endLocation=[firstZero objectForKey:@“end_location”];
NSDictionary*startLocation=[firstZero objectForKey:@“开始位置”];
NSString*latFrom=[startLocation objectForKey:@“lat”];
NSString*lngFrom=[startLocation objectForKey:@“lng”];
NSString*latTo=[endLocation objectForKey:@“lat”];
NSString*lngTo=[endLocation objectForKey:@“lng”];
返回@[latFrom,lngFrom,latTo,lngTo];
}
-(无效)中心地图{
协调区域;
region.center.lation=41.178654;
region.center.longitude=11.843262;
region.span.latitudeDelta=11.070406;
region.span.longitudeDelta=12.744629;
[self.mapView setRegion:region];
}
-(iAction)选择KindofMap:(id)发件人{
如果([sender tag]==0){
self.mapView.mapType=MKMapTypeStandard;
}
如果([发送者标签]==1){
self.mapView.mapType=MKMapTypeSatellite;
}
如果([发送者标签]==2){
self.mapView.mapType=MKMapTypeHybrid;
}
}
-(无效)更新区域
{
self.needUpdateRegion=否;
CGRect-boundingRect;
BOOL开始=否;
for(self.mapView.annotations中的id注释){
CGRect annotationRect=CGRectMake(annotation.coordinate.latitude,annotation.coordinate.longitude,0,0);
如果(!已启动){
开始=是;
boundingRect=注释Rect;
}否则{
boundingRect=CGRectUnion(boundingRect,annotationRect);
}
}
如果(启动){
boundingRect=CGRectInset(boundingRect,-0.2,-0.2);
如果((boundingRect.size.width<20)和&(boundingRect.size.height<20)){
协调区域;
region.center.latitude=boundingRect.origin.x+boundingRect.size.width/2;
瑞吉
- (IBAction)traceRoute:(UIButton *)sender {
 double latDouble = 39.7540615;
    double lngDouble = -8.8059587;

//    double latDouble = [self.sheetDetail.data.locationLat doubleValue];
//    double lngDouble = [self.sheetDetail.data.locationLng doubleValue];

    CLLocationCoordinate2D c2D = CLLocationCoordinate2DMake(latDouble, lngDouble);

    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:c2D addressDictionary:nil];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem setName:@"Mobile Edge"];


    MKPlacemark *placemark2;
    MKMapItem *mapItem2;
    if(kIS_OS_8_OR_LATER) {
        placemark2 = [[MKPlacemark alloc] initWithCoordinate:_userLoc addressDictionary:nil];
        mapItem2 = [[MKMapItem alloc] initWithPlacemark:placemark2];
        [mapItem2 setName:@"Me"];
    } else {
        placemark2 = [[MKPlacemark alloc] initWithCoordinate:_mapView.userLocation.coordinate addressDictionary:nil];
        mapItem2 = [[MKMapItem alloc] initWithPlacemark:placemark2];
        [mapItem2 setName:@"Me"];
    }

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    request.source = mapItem2;
    request.destination = mapItem;
    request.requestsAlternateRoutes = NO;
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

    [directions calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) {
         if (error) {
             // Handle error
             [[NSNotificationCenter defaultCenter] postNotificationName:@"finishedLocationRoute" object:nil];

             UIAlertView *alert = [[UIAlertView alloc] initWithTitle: NSLocalizedString(@"Route error title", nil)
                                                             message: NSLocalizedString(@"Route error", nil)
                                                            delegate:self
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil];
             [alert show];

         } else {
             for (MKRoute *route in response.routes)
             {

                 //MKMapPoint middlePoint = route.polyline.points[route.polyline.pointCount/2];
                 //[self createAndAddAnnotationForCoordinate:MKCoordinateForMapPoint(middlePoint) andRoute:route];
                 [self.mapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
             }

             //notifies parent menu
             //[[NSNotificationCenter defaultCenter] postNotificationName:@"finishedLocationRoute" object:nil];

         }
     }];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay
{

    if (![overlay isKindOfClass:[MKPolygon class]]) {

        MKPolyline *route = overlay;
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        renderer.strokeColor = [UIColor blueColor];
        renderer.lineWidth = 5.0;
        return renderer;

    } else {

        return nil;
    }

}