Question

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.

Basically, at the moment I have an empty UITableView that is used to populate results from a local search, via a search bar. 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!

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top