必须有一种简单的方法来执行此操作,否则我将在某个地方出错,但是我似乎无法将当前位置保存为全局变量,然后将其添加到数组中以填充UITableView。

基本上,目前我有一个空的UITableView,用于通过搜索栏填充本地搜索的结果。这部分效果很好。但是,我想要的是始终保持第一行作为用户的当前位置,例如在地图,谷歌地图,提醒等中。我认为这将是一个简单的任务,但我无法使其正常工作。有谁可以帮助我吗。

我使用以下代码获取我的当前位置,对其进行反向地理编码,然后在应用启动时将其绘制在地图上:

self.locationManager = [[CLLocationManager alloc]init];
if ([CLLocationManager locationServicesEnabled]) {
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
}

CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = _locationManager.location.coordinate.latitude;
zoomLocation.longitude= _locationManager.location.coordinate.longitude;

currentCoord.latitude = _locationManager.location.coordinate.latitude;
currentCoord.longitude= _locationManager.location.coordinate.longitude;

//reverse geocoder
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:zoomLocation.latitude longitude:zoomLocation.longitude];
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    for (CLPlacemark *placemark in placemarks) {

        MKPointAnnotation *annotation =
        [[MKPointAnnotation alloc]init];
        annotation.coordinate = zoomLocation;
        annotation.title = @"Current Location";
        annotation.subtitle = placemark.name;

        // 2
        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);

        // 3
        [_mapView setRegion:viewRegion animated:YES];
        [_mapView addAnnotation:annotation];
    }

}];


当我运行本地搜索时,我使用以下代码填充数组以用以下方式加载表:

MKLocalSearchRequest *request =
[[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = _searchBar.text;
request.region = _mapView.region;

MKLocalSearch *search =
[[MKLocalSearch alloc]initWithRequest:request];

[search startWithCompletionHandler:^(MKLocalSearchResponse
                                     *response, NSError *error) {
    if (response.mapItems.count == 0)
        NSLog(@"No Matches");
    else
        for (MKMapItem *item in response.mapItems)
        {
            [tableData addObject:item];
            MKPointAnnotation *annotation =
            [[MKPointAnnotation alloc]init];
            annotation.coordinate = item.placemark.coordinate;
            annotation.title = item.placemark.name;
            annotation.subtitle = item.placemark.title;
            [annotations addObject:annotation];
            [_tableResults reloadData];
        }
}];


我真的不知道这一点。如何添加我的当前位置?真的很感谢一些建议。在此先感谢大家!

最佳答案

尝试这个,

[search startWithCompletionHandler:^(MKLocalSearchResponse
                                     *response, NSError *error) {
    if (response.mapItems.count == 0)
        NSLog(@"No Matches");
    else
        for (MKMapItem *item in response.mapItems)
        {
            [tableData addObject:item];
            [annotations addObject:[self annotationFromMapItem:item]];
        }
     double latitude = self.locationManager.location.coordinate.latitude;
     double longitude = self.locationManager.location.coordinate.longitude;
     MKPlacemark *placemark = [[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil] autorelease];
     MKMapItem *mapItem = [[[MKMapItem alloc] initWithPlacemark:placemark] autorelease];
    [mapItem setName:Current Location];
    [tableData insertObject:mapItem atIndex:0];
    [annotations insertObject:[self annotationFromMapItem:mapItem] atIndex:0];
    [_tableResults reloadData];
}];

- (MKPointAnnotation) annotationFromMapItem:(MKMapItem *)item {
   MKPointAnnotation *annotation =
            [[MKPointAnnotation alloc]init];
   annotation.coordinate = item.placemark.coordinate;
   annotation.title = item.placemark.name;
   annotation.subtitle = item.placemark.title;
   return annotation;
}

关于ios - 在UITableView中存储当前位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23424268/

10-13 03:50