简体   繁体   English

在UITableView中存储当前位置

[英]Store Current Location in UITableView

There must be an easy way to do this or somewhere I am going wrong but I can't seem to save my current location as a global variable and then add it to my array to populate my UITableView. 必须有一种简单的方法来执行此操作,或者在我出了错的地方,但是我似乎无法将当前位置保存为全局变量,然后将其添加到数组中以填充UITableView。

Basically, at the moment I have an empty UITableView that is used to populate results from a local search, via a search bar. 基本上,目前我有一个空的UITableView,用于通过搜索栏填充本地搜索的结果。 This part works great. 这部分效果很好。 BUT, what I want is to always have the 1st row as the users current location, like in maps, google maps, reminders etc. I figured this would be a simple task but I cant get it to work. 但是,我想要的是始终保持第一行作为用户的当前位置,例如在地图,谷歌地图,提醒等中。我认为这将是一个简单的任务,但我无法使其正常工作。 Can someone help me please. 有人能帮助我吗。

I use the following code to get my current location, reverse geocode it, and plot it on the map when the app starts: 我使用以下代码获取我的当前位置,对其进行反向地理编码,然后在应用启动时将其绘制在地图上:

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

}];

When I run my local search I use the following code to populate my array to load the table with: 当我运行本地搜索时,我使用以下代码填充数组以用以下方式加载表:

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

I really can't figure this out. 我真的不知道这一点。 How would I add my current location? 如何添加我的当前位置? Would really appreciate some advise. 真的很感谢一些建议。 Thanks in advance guys! 在此先感谢大家!

Try this, 尝试这个,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM