如何在mapview中绘制线条

时间:2015-10-08 04:36:22

标签: ios objective-c cllocationmanager cllocationcoordinate2d

这是我的代码: 我创建了CLLocationManager并更新了当前位置。

    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
#ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER) {
        // Use one or the other, not both. Depending on what you put in info.plist
        [locationManager requestWhenInUseAuthorization];
//        [locationManager requestAlwaysAuthorization];
    }
#endif
    [locationManager startUpdatingLocation];

在我的locationManager中,我获得了在mapview中显示的经度和纬度

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    CLLocation* location = [locations lastObject];
    longitude = location.coordinate.longitude;
    latitude = location.coordinate.latitude;
    [_lbllongtitude setText:[NSString stringWithFormat:@"%+.6f",longitude]];
    [_lbllatitude setText:[NSString stringWithFormat:@"%+.6f",latitude]];
    if ([delegate.getLocationLanDau isEqualToString:@"LanDau"]) {
        longitudeOld = longitude;
        latitudeOld = latitude;
        delegate.getLocationLanDau = @"KhongLayNua";
    }
    paramLocation = @{@"longtitude":_lbllongtitude.text,
                      @"latitude":_lbllatitude.text
                      };

    [self getLocationMapView];
}

-(void)getLocationMapView{
    [_mapView setDelegate:self];
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MKUserTrackingModeFollow;
    MKCoordinateRegion mapRegion;
    mapRegion.center.latitude = latitude;
    mapRegion.center.longitude = longitude;
    mapRegion.span.latitudeDelta = 0.005;
    mapRegion.span.longitudeDelta = 0.005;
    [self drawLineInMap_longtitude_old:longitudeOld latitude_old:latitudeOld];
    self.mapView.region = mapRegion;
}

在我的mapview中,我打电话给

-(void)drawLineInMap_longtitude_old:(float)longtitude_old latitude_old:(float)latitude_old{
    CLLocationCoordinate2D coordinateArray[2];
    coordinateArray[0] = CLLocationCoordinate2DMake(latitude_old, longtitude_old);
    coordinateArray[1] = CLLocationCoordinate2DMake(latitude, longitude);
    self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
    [self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible
    [self.mapView addOverlay:self.routeLine];
}

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    if(overlay == self.routeLine)
    {
        if(nil == self.routeLineView)
        {
            self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 5;

        }

        return self.routeLineView;
    }

    return nil;
}

但是,当我移动时,它不是画线。在第一个位置只绘制1个点(红色)。

请帮助我!

1 个答案:

答案 0 :(得分:0)

我给你更好的想法,你可以画路径

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
[storeLocation addObject:location];  //storeLocation Declare this global as NSMutableArray;
[self getLocationMapView];
}

-(void)drawLineInMap_longtitude_old { //Change this function like this 
NSInteger totalCount = storeLocation.count;
NSArray *pointsArray = [self.mapView overlays];
[self.mapView removeOverlays:pointsArray];

CLLocationCoordinate2D coordinateArray[totalCount];
for (int index = 0; index < storeLocation.count; index++) {
    CLLocation *tempLocation = [storeLocation objectAtIndex:index];
    coordinateArray[index] = CLLocationCoordinate2DMake(tempLocation.coordinate.latitude, tempLocation.coordinate.longitude);
}
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
[self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible
[self.mapView addOverlay:self.routeLine];
}

试试这个它会帮助你,如果你有任何问题然后告诉我......我会找到一个更好的选择

相关问题