1

GPS (Apple Maps) を使用したユーザー トラッキング (ランニング、ウォーキング) を実装したいと考えています。ユーザーが歩いているとき、リアルタイムで地図上に線を引きたいです。

どうやってやるの?

ここで 1 つの解決策を見ました: 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 に答える