【问题标题】:iOS 6 MapKit Drawing (realtime) traveled distance?iOS 6 MapKit 绘图(实时)行进距离?
【发布时间】:2012-11-14 00:49:27
【问题描述】:

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

我该怎么做?

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

提前致谢!

汤姆

【问题讨论】:

    标签: iphone objective-c xcode gps ios6


    【解决方案1】:

    第一步,我准备像这样的属性

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

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

    【讨论】:

    • 您的回答非常棒,但在 iOS 6+ 中,locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 已被弃用。相反,iOS 6+ 使用一种方法,在位置更新时只给出一组坐标。我们将如何为更新版本的 iOS 6 执行此操作?
    • 您好 RazorSharp,让我们看看我的伊迪丝回答。希望它会有所帮助。
    • 感谢您的回答!!不过看起来有些不对劲,oldLocation 坐标永远不会设置,因为if (locations.count &gt; 1) 从来都不是新 iOS 6+ 方法的真实声明。 locations 数组/对象在数组中一次只包含一个位置,而不是多个。要使此代码正常工作,需要某种逻辑来检查来自locations 的新坐标是否与newLocation 变量相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2012-06-07
    • 1970-01-01
    相关资源
    最近更新 更多