1

我想用 GPS(Apple Maps)实现用户跟踪(跑步、步行)。所以当用户走路时,我想在地图上画一条线 - 实时。

我怎样才能做到这一点?

我在这里看到了一个解决方案:http ://www.raywenderlich.com/21365/introduction-to-mapkit-in-ios-6-tutorial但它只有在你已经有了 A 点和 B 点时才有效。

提前致谢!

汤姆

4

1 回答 1

3

第一步,我准备这样的财产

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

然后我在里面这样实现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];
     }

}

对于 iOS6,您可以尝试这种方式而不是上面的代码:

    - (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;
}

希望我的回答能帮到你,干杯。

于 2012-11-14T03:21:28.613 回答