简体   繁体   中英

iOS 6 MapKit Drawing (realtime) traveled distance?

i want to implement user tracking (running, walking) with GPS (Apple Maps). So when user is walking i want to draw a line on map - realtime.

How can I do that?

I saw one solution here: http://www.raywenderlich.com/21365/introduction-to-mapkit-in-ios-6-tutorial but it works only if you have already point A and B.

Thanks in advance!

Tom

For the first step i prepare the property like this

ViewController.h

    #import <MapKit/MapKit.h>

    @interface ViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate>
    @property (nonatomic, strong) MKMapView *mapView;
    @property (nonatomic, strong) MKPolyline* routeLine;
    @property (nonatomic, strong) MKPolylineView* routeLineView;
    @property (nonatomic, strong) NSMutableArray *trackPointArray;
    @property (nonatomic, strong) CLLocationManager *locationManager;
    @property (nonatomic, readwrite) MKMapRect routeRect;

    @end

then i'm implement like this inside ViewController.m

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation 
{
    MKMapPoint * pointsArray = malloc(sizeof(CLLocationCoordinate2D)*2);
    pointsArray[0]= MKMapPointForCoordinate(oldLocation.coordinate);
    pointsArray[1]= MKMapPointForCoordinate(tempNewLocation.coordinate);

    routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
    free(pointsArray);

     if (tempNewLocation.coordinate.latitude - oldLocation.coordinate.latitude < 1)
     {
          [[self mapView] addOverlay:routeLine];
     }

}

For iOS6 you could try this way instead of the code above :

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = [locations objectAtIndex:locations.count - 1];
    CLLocation *oldLocation = nil;
    if (locations.count > 1)
    {
        oldLocation = [locations objectAtIndex:locations.count - 2];
    }

    MKMapPoint * pointsArray = malloc(sizeof(CLLocationCoordinate2D)*2);
    pointsArray[0]= MKMapPointForCoordinate(oldLocation.coordinate);
    pointsArray[1]= MKMapPointForCoordinate(tempNewLocation.coordinate);

    routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
    free(pointsArray);

    if (tempNewLocation.coordinate.latitude - oldLocation.coordinate.latitude < 1)
    {
        [[self mapView] addOverlay:routeLine];
    }
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayView* overlayView = nil;
    self.routeLineView = [[MKPolylineView alloc] initWithPolyline:[self routeLine]];
    [[self routeLineView] setFillColor:[UIColor colorWithRed:167/255.0f green:210/255.0f blue:244/255.0f alpha:1.0]];
    [[self routeLineView] setStrokeColor:[UIColor colorWithRed:106/255.0f green:151/255.0f blue:232/255.0f alpha:1.0]];
    [[self routeLineView] setLineWidth:15.0];
    [[self routeLineView] setLineCap:kCGLineCapRound];
    overlayView = [self routeLineView];
    return overlayView;
}

i hope my answer will help you, Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM