折线和覆盖mkmapview问题

时间:2014-12-03 11:12:46

标签: objective-c mkmapview xcode6 mkpolyline

我遇到了这个问题,我的mapview上有折线和叠加层。也许其他人也有问题?我不能让折线工作,但其他一切都在工作。跟踪用户等,但只是添加折线到地图剂量工作。

这是我的代码。

h-file

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

@class Walk;

@interface DetailViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>


@property (strong, nonatomic) Walk *walk;                               //walk
@property (strong, nonatomic) IBOutlet MKMapView *MKMapView;            //mapview
@property (strong, nonatomic) CLLocationManager *locationManager;       //locationmanager
@property (strong, nonatomic) IBOutlet UIProgressView *myprogressView;  //progressbar
@property (strong, nonatomic) IBOutlet UILabel *distLabel;              //distancelabel
@property (strong, nonatomic) NSMutableArray *trackPointArray;          //trackpointarray
@property (strong, nonatomic) CLLocationManager *locationCoordinates;   //locationcoordinates
@property (nonatomic, assign) CLLocationDegrees *currentLocation;       //currentlocation
@property (strong, nonatomic) UIActionSheet *saveWalk;

@end

这是我的m文件

m文件

#import "DetailViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Location.h"                        //imports the locations
#import "MathController.h"
#import "Walk.h"

static NSString * const detailSegueName = @"WalkDetails";    //walkdetails

@interface DetailViewController () <MKMapViewDelegate, CLLocationManagerDelegate, UIActionSheetDelegate,MKAnnotation>

@property (strong, nonatomic) MKMapView *mapView;

@end

@implementation DetailViewController                        

@synthesize MKMapView;                                      //mapview
@synthesize trackPointArray;                                //trackpointarray
@synthesize coordinate;

#pragma mark - Managing the detail item

- (void)setWalk:(Walk *)walk                                //settings for walk
{
    if (_walk != walk) {
        _walk = walk;
        [self configureView];
    }
}

- (void)configureView
{
    self.distLabel.text = [MathController stringifyDistance:self.walk.distance.floatValue];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];

    [self loadMap];
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {      //request authorization
        [self.locationManager requestWhenInUseAuthorization];                                   //request when in use
        self.MKMapView.showsUserLocation =YES;                                                  //shows users location
        CLLocationManager* tempLocationManager = [[CLLocationManager alloc] init];
        MKCoordinateRegion tRegion =MKCoordinateRegionMakeWithDistance([tempLocationManager.location coordinate],500,500);
        [self.MKMapView setRegion:tRegion animated:animated];                                   //sets region to animated
    }
}

- (MKCoordinateRegion)mapRegion
{
    MKCoordinateRegion region;
    Location *initialLoc = self.walk.locations.firstObject;

    float minLat = initialLoc.latitude.floatValue;
    float minLng = initialLoc.longitude.floatValue;
    float maxLat = initialLoc.latitude.floatValue;
    float maxLng = initialLoc.longitude.floatValue;

    for (Location *location in self.walk.locations) {
        if (location.latitude.floatValue < minLat) {
            minLat = location.latitude.floatValue;
        }
        if (location.longitude.floatValue < minLng) {
            minLng = location.longitude.floatValue;
        }
        if (location.latitude.floatValue > maxLat) {
            maxLat = location.latitude.floatValue;
        }
        if (location.longitude.floatValue > maxLng) {
            maxLng = location.longitude.floatValue;
        }
    }

    region.center.latitude = (minLat + maxLat) / 2.0f;
    region.center.longitude = (minLng + maxLng) / 2.0f;

    region.span.latitudeDelta = (maxLat - minLat) * 1.1f; // 10% padding
    region.span.longitudeDelta = (maxLng - minLng) * 1.1f; // 10% padding

    return region;
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolyline *polyLine = (MKPolyline *)overlay;
        MKPolylineRenderer *aRenderer = [[MKPolylineRenderer alloc] initWithPolyline:polyLine];
        aRenderer.strokeColor = [UIColor blackColor];
        aRenderer.lineWidth = 3;
        return aRenderer;
    }

    return nil;


    //coordinates for the polyline
}

- (MKPolyline *)polyLine {

    CLLocationCoordinate2D coords[self.walk.locations.count];

    for (int i = 0; i < self.walk.locations.count; i++) {
        Location *location = [self.walk.locations objectAtIndex:i];
        coords[i] = CLLocationCoordinate2DMake(location.latitude.doubleValue, location.longitude.doubleValue);
    }

    return [MKPolyline polylineWithCoordinates:coords count:self.walk.locations.count];
}


//loading map

- (void)loadMap
{
    if (self.walk.locations.count > 0) {

        self.mapView.hidden = NO;

        // set the map bounds
        [self.mapView setRegion:[self mapRegion]];

        // make the line(s!) on the map
        [self.mapView addOverlay:[self polyLine]];

    } else {

        // no locations were found!
        self.mapView.hidden = YES;

        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:@"Sorry, this run has no locations saved."
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

- (IBAction)stopPressed:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""delegate:self          //sets the delgate
    cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil                                      //cancel button
    otherButtonTitles:@"Save", @"Discard", nil];                                                //button to destruct
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;                                   //actionsheet for sender
    [actionSheet showInView:self.view];                                                         //attribute to show in view
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  //action for clicked button
{

    if (buttonIndex == 0) {
        [self saveWalk];                                                        //saves the walk
        [self.navigationController popToRootViewControllerAnimated:YES];

        //[self performSegueWithIdentifier:detailSegueName sender:nil];  /check this later if it works

    } else if (buttonIndex == 1) {                                               //discard
        [self.navigationController popToRootViewControllerAnimated:YES];         //button will call rootview (homescreen)



    }
}

@end

所以,我需要有关折线的帮助,那么任何人都会错误吗?:)

0 个答案:

没有答案