这是我的代码:我创建了 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 中,我得到了要在地图视图中显示的经度和纬度
- (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;
}
在我的地图视图中,我打电话给
-(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 个点(红色)。
请帮我!